Any tool to automatically fix simple JSLint issues? [closed]

北城余情 提交于 2019-11-28 19:50:01

问题


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?

(I have /*jslint white: false */ in my file, I develop in Netbeans and 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.)


回答1:


While it checks for different things than JSLint, the fixjsstyle mode of the Google closure linter may do what you want.

It automatically fixes code to (more closely) fit with the Google Javascript style guide which is well worth a read.

As others have pointed out, the Javascript beautifier is the way to go for spacing issues.




回答2:


There's an npm module called fixmyjs.

In "legacy mode" with 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.




回答3:


http://jsbeautifier.org/ should fix all your problems




回答4:


jsfmt formats javascript and allows AST searching and rewriting. Analogous to gofmt.

In some ide's like netbeans you can automatically format code with alt+shift+f.

There are also online ones. http://jsbeautifier.org/




回答5:


Simply use IDE which supports custom code formatting. Like NetBeans, WebStorm or Visual Studio.




回答6:


There's a bunch of tools around for doing things like this. I use JS Beautifier which will at least fix indentation errors and also the spaces-around-functions-part (I've tested it, yay!)




回答7:


If you use/have Visual Studio it does formatting of JavaScript too. You may need to configure formatting options from defaults.




回答8:


Damon, Prettier is 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.

Here is a video from ReactConf that explains it pretty well



来源:https://stackoverflow.com/questions/7564139/any-tool-to-automatically-fix-simple-jslint-issues

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!