Javascript 有什么工具可以自动修复简单的 JSLint 问题?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/7564139/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Any tool to automatically fix simple JSLint issues?
提问by Damon
I've run JSLint for the first time on a rather lengthy file, and I have a lot of errors like expected exactly 1 space between "function" and "("
or unexpected ' '
. I didn't realize this was important at all anywhere I learned about javascript and now fixing each one of these rather simple things by hand seems frustrating. Some I can figure out with simple find and replaces, but I wondered if there's any tools online that will automatically make these changes for me since they seem to be pretty straightforward?
我已经运行的JSLint首次在一个相当长的文件,我有很多类似的错误expected exactly 1 space between "function" and "("
或unexpected ' '
。我没有意识到这在我学习 javascript 的任何地方都很重要,现在手动修复这些相当简单的事情似乎令人沮丧。有些我可以通过简单的查找和替换来解决,但我想知道是否有任何在线工具可以自动为我进行这些更改,因为它们似乎非常简单?
(I have /*jslint white: false */
in my file, I develop in Netbeansand auto-format (except then I have to correct hanging jQuery chainings because it doesn't do it right), and my code still ends up with a huge number of things that jslint complains about as far as unexpected numbers of spaces.)
(我/*jslint white: false */
在我的文件中有,我在Netbeans和自动格式中开发(除非我必须纠正挂起的 jQuery 链接,因为它做的不对),我的代码仍然以大量 jslint 抱怨的事情告终大约达到意外数量的空格。)
采纳答案by Damon
Damon, Prettieris probably going to do everything you want wrt painless javascript code formatting. It will convert your code to an AST and then pretty print it back into your file so it auto-formats as you go. You can even add it as a precommit hook or run it on a folder full of files (pretty quickly, too!) so that your entire codebase will be immediately pretty.
Damon,Prettier可能会做你想做的一切,用无痛的 javascript 代码格式化。它会将您的代码转换为 AST,然后将其打印回您的文件中,以便在您进行时自动格式化。您甚至可以将其添加为预提交挂钩或在一个充满文件的文件夹上运行它(也非常快!),这样您的整个代码库就会立即变得漂亮。
回答by Timothy Jones
While it checks for different things than JSLint, the fixjsstyle
mode of the Google closure lintermay do what you want.
虽然它检查与 JSLint 不同的东西fixjsstyle
,但Google 闭包linter 的模式可能会做你想要的。
It automatically fixes code to (more closely) fit with the Google Javascript style guidewhich is well worth a read.
它会自动修复代码以(更紧密地)符合Google Javascript 样式指南,这是非常值得一读的。
As others have pointed out, the Javascript beautifieris the way to go for spacing issues.
正如其他人指出的那样,Javascript 美化器是解决间距问题的方法。
回答by timelf123
There's an npm module called fixmyjs.
有一个名为fixmyjs的 npm 模块。
In "legacy mode" with JSHint:
在 JSHint 的“传统模式”中:
var jshint = require('jshint').JSHINT
var fixmyjs = require('fixmyjs')
jshint(stringOfCode, objectOfOptions)
var stringFixedCode = fixmyjs(jshint.data(), stringOfCode, objectOfOptions).run()
Works great!
效果很好!
There is also a Sublime Text 2/3 Package.
还有一个Sublime Text 2/3 包。
回答by albert
http://jsbeautifier.org/should fix all your problems
http://jsbeautifier.org/应该能解决你所有的问题
回答by Ilia Choly
jsfmtformats javascript and allows AST searching and rewriting. Analogous to gofmt.
jsfmt格式化 javascript 并允许 AST 搜索和重写。类似于 gofmt。
In some ide's like netbeans you can automatically format code with alt+shift+f
.
在某些 ide 中,例如 netbeans,您可以使用alt+shift+f
.
There are also online ones. http://jsbeautifier.org/
回答by c69
Simply use IDE which supports custom code formatting. Like NetBeans, WebStorm or Visual Studio.
只需使用支持自定义代码格式的 IDE。像 NetBeans、WebStorm 或 Visual Studio。
回答by goto-bus-stop
There's a bunch of tools around for doing things like this. I use JS Beautifierwhich will at least fix indentation errors and also the spaces-around-functions-part (I've tested it, yay!)
有很多工具可以做这样的事情。我使用JS Beautifier,它至少可以修复缩进错误以及函数周围的空格(我已经测试过了,是的!)
回答by Alexei Levenkov
If you use/have Visual Studio it does formatting of JavaScript too. You may need to configure formatting options from defaults.
如果您使用/拥有 Visual Studio,它也会对 JavaScript 进行格式化。您可能需要从默认值配置格式选项。