We don’t allow questions seeking recommendations for books, tools, software libraries, and more. You can edit the question so it can be answered with facts and citations.
Closed 5 years ago.
Improve this questionI've run JSLint for the first time on a rather lengthy file, and I have a lot of开发者_StackOverflow社区 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.)
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.
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.
http://jsbeautifier.org/ should fix all your problems
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/
Simply use IDE which supports custom code formatting. Like NetBeans, WebStorm or Visual Studio.
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!)
If you use/have Visual Studio it does formatting of JavaScript too. You may need to configure formatting options from defaults.
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
精彩评论