简体   繁体   English

什么是一个很好的独立JavaScript格式化程序来修复丢失的分号?

[英]What is a good stand-alone JavaScript formatter for fixing missing semicolons?

I'm trying to retrofit/fix lots of legacy web code and unfortunately most of it is poorly formatted JavaScript. 我正在尝试改进/修复大量遗留的Web代码,不幸的是大部分都是格式不良的JavaScript。 I'm looking for a batch/scriptable utility that can fix JavaScript that is missing simicolons at the end of executable statements. 我正在寻找一个批处理/脚本化实用程序,可以修复在可执行语句结束时缺少simicolons的JavaScript。

I've tried the beautify-cl.js script with Rhino but that does not does not add semicolons. 我已经尝试过使用Rhino的beautify-cl.js脚本,但这并不会添加分号。 In addition, I have tried JSTidy thinking I could modify it to be scriptable, but it strips all comments. 另外,我已经尝试过JSTidy认为我可以将其修改为可编写脚本,但它会删除所有注释。 Considering we have something like 2000-3000 files, any solution has to be scriptable. 考虑到我们有类似2000-3000文件的东西,任何解决方案都必须是可编写脚本的。

The following topics were referenced, however none of the solutions were sufficient for various reasons: Javascript Beautifier - Doesn't handle semicolon Best source code formatter for Javascript? 引用了以下主题,但是由于各种原因,没有一个解决方案是足够的: Javascript Beautifier - 不处理分号Javascript的最佳源代码格式化程序? - Not scriptable - 不可编写脚本

Any ideas/solutions? 任何想法/解决方案? Thanks in advance. 提前致谢。

Obviously you'll need to do this if you want to minify the files on deployment. 显然,如果要在部署时缩小文件,则需要执行此操作。 Missing semicolons are probably the #1 reason JS files don't minify properly, so I understand your motivation. 缺少分号可能是JS文件没有正确缩小的第一个原因,所以我理解你的动机。

Write a little Python (or whatever) script to run the file through jslint, then use the output from jslint to see which lines need semicolons, then spin through the js source and add them. 编写一些Python(或其他)脚本来通过jslint运行文件,然后使用jslint的输出来查看哪些行需要分号,然后旋转js源并添加它们。

I think you can be fairly fearless here, because JavaScript implicitly adds the semicolons anyway. 我认为你在这里可能相当无所畏惧,因为JavaScript无论如何都隐含地添加了分号。


Update: This set of tools may be what you are looking for . 更新: 这组工具可能是您正在寻找的 The "format" tab offers missing semicolon insertion. “格式”选项卡提供缺少分号插入。

I've found a winning combination in js-beautify and Google's Closure Linter : 我在js-beautify和Google的Closure Linter中找到了一个成功的组合:

# legacy.js is your poorly formatted JavaScript file, and will be modified in-place
js-beautify -s 2 -n -b end-expand -x -r legacy.js && fixjsstyle legacy.js

Explanation of js-beautify options: js-beautify选项的说明:

  • -s 2 : indent with two spaces -s 2 :缩进两个空格
  • -n : ensure newline at end of file -n :确保文件末尾的换行符
  • -b end-expand : puts { braces at the end of the line, but always gives } braces their own line. -b end-expand :将{ braces放在行的末尾,但始终给出}括起自己的行。
  • -x : unescape \\xNN -escaped characters in strings -x :字符串中的unescape \\xNN -escaped字符
  • -r : make changes in-place -r :就地进行更改

fixjsstyle , which is installed with the Closure Linter suite, makes changes in-place by default. fixjsstyle ,其安装与闭锁短绒套件,使得在默认情况下,发生的变化。

This pipeline retains comments (!), indents everything (mostly) how I like, adds semicolons where appropriate, and even changes double quotes to single quotes where feasible. 此管道保留注释 (!),缩小所有内容(主要是)我喜欢的内容,在适当的位置添加分号,甚至在可行的情况下将双引号更改为单引号。 Both commands can be given a list of files (eg, **/*.js ), instead of just one. 两个命令都可以给出一个文件列表(例如, **/*.js ),而不是一个。

To install the required packages on Mac OS X: 要在Mac OS X上安装所需的软件包:

npm install -g js-beautify
brew install closure-linter

If you use JavaScript Utlity V2 at http://jsutility.pjoneil.net and use the formatting function, it will automatically replace missing semicolons. 如果您在http://jsutility.pjoneil.net上使用JavaScript Utlity V2并使用格式化功能,它将自动替换丢失的分号。

In addition, if you use the compaction function, it will also replace missing semicolons so that the compaction will not cause any errors. 此外,如果使用压缩函数,它还将替换缺少的分号,以便压缩不会导致任何错误。

You shouldn't be worried about doing a mass update on a lot of legacy code for the sole purpose of inserting semi colons. 您不应该担心为了插入半冒号而对大量遗留代码进行大规模更新。 That's a classic case of "doing it wrong". 这是“做错了”的经典案例。

How would you test the results? 你会如何测试结果? How would you ensure no "functionality" (as a side effect of a bug caused by a semi colon being missing) isn't lost? 您如何确保没有“功能”(由于缺少半冒号导致的错误的副作用)不会丢失?

What do you think adding semi colons to all these files will get you? 您认为将半冒号添加到所有这些文件中会得到什么? Beside larger files (I'm not knocking the use of semicolons) and massive amounts of untested code changes? 除了更大的文件(我不是在使用分号)和大量未经测试的代码更改?

As gumbo said, use jslint . 正如gumbo所说,使用jslint I would use it on the files as you edit them in your day to day work. 当你在日常工作中编辑文件时,我会在文件上使用它。 As you edit these files, presumably you will be testing changes to the file at that time. 在编辑这些文件时,可能您将在那时测试对文件的更改。 That would be the most ideal time to go crazy with semi colon insertion. 这将是半结肠插入疯狂的最理想时间。

Also, if you're concerned about keeping 2000-3000 legacy javascript files alive and supported, you've got far bigger problems than semi colons 此外,如果你担心保持2000-3000传统的javascript文件存活和支持,你有比半冒号更大的问题

如果http://jsutility.pjoneil.net丢失了太多错误(并且无法对其进行格式化),您可以尝试使用以下命令对其进行压缩: http//refresh-sf.com/yui/ (这将添加缺少的分号)然后返回到pjoneil.net格式化程序,以分号获取漂亮的代码。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM