Another solution is to use a JS parser, such as Acorn [1] to parse the JS. It can be used as a command line tool (passing the --silent) option to it, which makes it only return the exit code which we can use to check whether the syntax was valid or not.
The bigger question, probably, is that neither this not my previous solution prints out the errors in the syntax, so they're not very useful to locate errors.
A relevant discussion happened on an issue filed on the JSHint GitHub repo [2] which asked for a syntax check only mode. It was declared a wont-fix [3]. However, we still might be able to disable most of the rules and try for a minimal configuration, which might just get us what we want.
[1] https://github.com/marijnh/acorn#command-line-interface [2] https://github.com/jshint/jshint/issues/808 [3] https://github.com/jshint/jshint/issues/808#issuecomment-20763023
Vivek Ghaisas (polybuildr)
On Tue, Sep 8, 2015 at 2:47 PM, Vivek Ghaisas v.a.ghaisas@gmail.com wrote:
I Googled for npm packages that check JS syntax and came across a package called syntax-checker [1] which checks several languages. I looked at the source and found the lines where it checks the syntax of each language [2]. For languages other than JS, it just uses the interpreter/compiler with a flag passed to it. However, for JS, it passes the file to UglifyJS [3] and redirects its output to /dev/null. Presumably, UglifyJS complains when it sees invalid JS.
Probably not the best way to do it, but certainly a solution.
[1] https://github.com/xianhuazhou/syntax-checker/ [2] https://github.com/xianhuazhou/syntax-checker/blob/c68c562d8/lib/syntax-chec... [3] https://github.com/mishoo/UglifyJS2
-- Vivek Ghaisas polybuildr