当前位置: 首页 > 面试题库 >

是否有JSX格式化程序可用于升华文本?

锺高翰
2023-03-14
问题内容

我正在使用Sublime Text作为文本编辑器。

有一种用于格式化JavaScript文件的jsFormat,但我找不到用于JSX的格式。

你们如何处理JSX格式?


问题答案:

更新4

检查更漂亮,不是可配置为esformatter,但当前用于格式化一些大型项目(例如React本身)

更新3

检查sublime jsfmt。如果将esformatter-
jsx
添加到配置中,然后将软件包安装在forlime-
jsfmt的forlder中。您将能够直接从Sublime格式化JSX文件。这是为此的指南

更新2

在命令行中,您也可以使用esbeautifier。它是esformatter的包装,它接受要格式化的glob列表

# install the dependencies globally
npm i -g esbeautifier

# beautify the files under src/ and specs/ both .js and .jsx
esbeautifier src/**/*.js* specs/**/*.js*

更新资料

因此,我最终为esformatter做了一个插件来启用JSX文件的格式设置:

https://www.npmjs.com/package/esformatter-
jsx

这是在requirebin上的现场演示

从Sublime
调用esformatter并传递当前文件作为参数应该是可行的。无论如何从命令行使用它,都可以按照以下说明进行:

在命令行中,可以这样使用它:

# install the dependencies globally
npm i -g esformatter esformatter-jsx

# call it (this will print to stdout)
esformatter --plugins=esformatter-jsx ./path/to/your/file

# to actually modify the file
esformatter --plugins=esformatter-jsx ./path/to/your/file > ./path/to/your/file

# to specify a config file (where you can also specify the plugins)
# check esformatter for more info about the configuration options
esformatter -c ./path/to/.esformatter ./path/to/your/file > ./path/to/your/file

====下面的旧答案===

因此,如果您只是想在允许jsx语法的情况下格式化jsx文件(基本上是美化所有javascript语法并忽略jsx标签,这意味着将它们保留原样),这就是我正在使用的方法esformatter

// needed for grunt.file.expand
var grunt = require('grunt');

// use it with care, I haven't check if there
// isn't any side effect from using proxyquire to 
// inject esprima-fb into the esformatter
// but this type of dependency replacement
// seems to be very fragile... if rocambole deps change 
// this will certainly break, same is true for esformatter
// use it with care
var proxyquire = require('proxyquire');
var rocambole = proxyquire('rocambole', {
  'esprima': require('esprima-fb')
});

var esformatter = proxyquire('esformatter', {
  rocambole: rocambole
});

// path to your esformatter configuration
var cfg = grunt.file.readJSON('./esformatter.json');

// expand the files from the glob
var files = grunt.file.expand('./js/**/*.jsx');

// do the actual formatting
files.forEach(function (fIn) {
  console.log('formatting', fIn);
  var output = esformatter.format(grunt.file.read(fIn), cfg);
  grunt.file.write(fIn, output);
});

我实际上希望esformatter使用使用esprima-fb而不是esprima的rocambole版本,以避免proxyquire。



 类似资料:
  • 我想格式化一个字符串,准备输出到终端窗口。终端窗口本身不会自动换行,所以我想在字符串中插入换行符。 有没有办法使用方法来实现这一点? 编辑:只是为了澄清。我不想将换行符硬编码到字符串中。 我想从这样的输入出发: Lorem ipsum dolor sit amet,consectetur adipisicing elit,sed do eusmod tempor incidunt ut labor

  • 问题内容: 我在网上找不到任何标准的开源sql formatter eclipse插件。我正在使用日食日光灯。我可以找到 Edit- > FormatSQL, 但这似乎不起作用。在http://ventralnet.blogspot.in/2010/11/sql-beautifier- eclipse-plugin.html上 找到了一个, 但是格式不好。任何建议都会有所帮助。有关信息,我正在使用

  • 问题内容: 例如:对象A包含对象B,对象B包含对象C,对象C包含对象A。 对象A是否可以正确序列化? 这里的#9注释表明它不起作用。 相反,XStream指示它确实处理循环引用。 问题答案: 是的,默认的Java序列化可用于循环引用。序列化对象C时,该字段将包含对已序列化的对象A的反向引用,而不是再次对其进行序列化。

  • 我正在使用Xamarin.forms将我的iOS和Android应用程序整合到一个代码库中。我正在使用Firebase来处理生产中应用程序的远程通知。我希望在我的新Xamarin项目中使用来自我的生产应用程序的相同的google-service.json和googleService-info.plist文件。这可能吗?或者我需要在Firebase中创建和注册一个全新的应用程序? 我已经尝试将现有的

  • 我有一个关于格式化程序的问题。 在SWAPI(https://SWAPI.co/documentation#people)中,您可以读取和日期,其格式如下: 但是在预定义格式化程序中的类DateTimeFormatter的文档一节中,我看不到任何与SWAPI日期示例匹配的格式化程序。

  • 问题内容: 在eclipse之外,有没有办法使用eclipse附带的格式化程序?我想使用我使用eclipse配置的formatter.xml文件来格式化一些Java文件。有没有人有任何代码示例可以让我做到这一点?我也想使用这个独立的,所以使用的特定罐子会很好。 问题答案: 显然,您可以从命令行直接调用Eclipse的代码格式化程序。