4

I am aware of tools like JSLint, but I'm not looking for style correctness, I need a tool or utility (preferably one that runs on Linux, bonus points for being in the ubuntu package manager) that can verify the syntactic correctness of a JavaScript file.

I just need to know that there are no syntax errors.

(Full disclosure, was going to put this check in a git commit hook, to ensure that syntactically incorrect JavaScript would not be committed. So it can be a style tool, but needs to be able to produce a "YES SYNTAX GOOD", or "NO SYNTAX BAD" result.)

1
  • I never used them to that end but minifiers normally return errors when applied on bad js files. I know Google's Closure compiler does that and you may call it from the command line. Commented Mar 29, 2013 at 18:12

2 Answers 2

5

I believe you think JSLint doesn't verify syntax, but I think it does exactly what you want. Now, for my bonus points! :-) Here is a quick example that should do everything you want, despite using JSLint.

sudo apt-get install spidermonkey-bin
wget http://www.jslint.com/fulljslint.js
mv fulljslint.js /home/admin/bin/js

then create /home/admin/bin/js/runjslint.js:

load('/home/admin/bin/js/fulljslint.js');
var body = arguments[0];
var result = JSLINT(body);
if (result) {
 print('YES SYNTAX GOOD');
} else {
 print('NO SYNTAX BAD');
}
print(JSLINT.report());

This file, when run using Spidermonkey, will check the syntax of Javascript data passed as a command line argument. You might not want that report at the end, thought it might be worth having for you to play with though.

Sign up to request clarification or add additional context in comments.

2 Comments

I didn't mean literally YES SYNTAX GOOD, NO SYNTAX BAD, but amusing anyway :P
I figured the more complete the answer, the more likely I could get a little green check mark next to it :-)
0

acorn.js is a really small javascript parser. It throws a javascript error when it encounters a problem.

Download acorn

curl https://raw.github.com/marijnh/acorn/master/acorn.js > acorn.js

Create a runner called acornIt.js with the following contents (requires node to be installed)

var acorn = require('./acorn');
var fs = require('fs');
var fileName = process.argv[2];
var contents = fs.readFileSync(fileName);

try {
   acorn.parse(contents);
   console.log("YES SYNTAX GOOD");
} catch (e) {
   console.log("NO SYNTAX BAD");
   console.log(e); // See where the error occured
}

And run it

node acornIt.js someTestFile.js

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.