8

I have some big set of different javascript-snippets (several thousands), and some of them have some stupid errors in syntax (like unmatching braces/quotes, HTML inside javascript, typos in variable names).

I need a simple way to check JS syntax. I've tried JSLint but it send too many warnings about style, way of variable definitions, etc. (even if i turn off all flags). I don't need to find out style problems, or improve javascript quality, i just need to find obvious syntax errors. Of course i can simply check it in browser/browser console, but i need to do it automatically as the number of that snippets is big.

Add:
JSLint/JSHint reports a lot of problems in the lines that are not 'beauty' but working (i.e. have some potential problems), and can't see the real problems, where the normal compiler will simply report syntax error and stop execution. For example, try to JSLint that code, which has syntax errors on line 4 (unmatched quotes), line 6 (comma required), and line 9 (unexpected <script>).

document.write('something');
a = 0;
if (window.location == 'http://google.com')  a = 1;
document.write("aaa='andh"+a+"eded"');
a = {
  something: ['a']
  something2: ['a']
};
<script>
a = 1;
2
  • 2
    try this javascriptlint.com Commented Mar 29, 2013 at 11:18
  • hmmm... Thanks, i'll try. I thought it's some clone of JSLint, but it works different. Commented Mar 29, 2013 at 11:35

6 Answers 6

2

You could try JSHint, which is less verbose.

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

1 Comment

Looks quite similar to JSLint.
2

Just in case anyone is still looking you could try Esprima,

It only checks syntax, nothing else.

2 Comments

Esprima seems more useful than JsLint, at least it detects eg a missing bracket. Doesn't integrate with an editor though does it ?
Appears to reject constructs like record?.field??null Even after deleting all of these it told me there were 11 errors, but no line numbers for them. This is code that fails to import and I'm trying to find out why
1

I've found that SpiderMonkey has ability to compile script without executing it, and if compilation failed - it prints error.

So i just created small wrapper for SpiderMonkey

sub checkjs {
    my $js = shift;
    my ( $js_fh, $js_tmpfile ) = File::Temp::tempfile( 'XXXXXXXXXXXX', EXLOCK => 0, UNLINK => 1, TMPDIR => 1 );
    $| = 1;
    print $js_fh $js;
    close $js_fh;
    return qx(js -C -f $js_tmpfile 2>&1);
}

And javascriptlint.com also deals very good in my case. (Thanks to @rajeshkakawat).

4 Comments

I think the problem there is that he's interested not in whether it compiles, necessarily (does SpiderMonkey insert semi-colons, for instance? And certainly it doesn't require you declare vars, right?), but is interested in finding some logical-though-not-compilation errors (like var name typos), that JSLint is particularly good at catching.
JSLint is not good for syntax checking, it reports some small problems, but don't see really bad and dangerous things. Try this: document.write('something'); a = 0; if (window.location == 'google.com') a = 1; document.write("aaa='andh"+a+"eded"'); a = { something: ['a'] something2: ['a'] }; <script> a = 1; As i understand SpiderMonkey is the JS-engine used in Firefox, so if will add/not add semicolons exactly as Firefox do.
didn't realize this was your own answer! Compilation's not going to find typos in names, etc. Creating a subset of JSLint that matches what you want to have reported would give you exactly what you want, I think, and would also report missing semi-colons, which can bork, among other things, multi-file minified code.
in practice, and in my case JSLint error-report has several megabytes of text - reading/fixing that will take years :) Some of the scripts depend on 3rd party code, so it's really hard to understand if some variable is used once because of typo, of because it was declared somethere else. Generally i founded several dozens of syntax problems using SpiderMonkey, and i almost safisfied with it. I'll try also javascriptlint, and may be will try to filter out JSLint results to ignore not critical problems (it reports everything as 'error' :\ )
0

Lots of options if you have an exhaustive list of the JSLint errors you do want to capture.

JSLint's code is actually quite good and fairly easy to understand (I'm assuming you already know JavaScript fairly well from your question). You could hack it to only check what you want and to continue no matter how many errors it finds.

You could also write something quickly in Node.js to use JSLint as-is to check every file/snippet quickly and output only those errors you care about.

1 Comment

see my add in initial post.
0

Just use node --check filename

Comments

-1

Semantic Designs' (my company) JavaScript formatter read JS files and formats them. You don't want the formatting part.

To read the files it will format, it uses a full JavaScript parser, which does a complete syntax check (even inside regular expressions). If you run it and simply ignore the formatted result, you get a syntax checker.

You can give it big list of files and it will format all of them. You could use this to batch-check your large set. (If there are any syntax errors, it returns a nonzero error status to a shell).

2 Comments

1) Forgot to say. Platform is Linux, freeware tools preferred. 2) Your site thinks that only one platform exists, and it's hard to find that your soft runs on Windows
1. Hard to provide answer to requirements you didn't initially provide. 2) Not so hard, see Purchase page links. 3) Discusses running software under Wine on Linux, which works quite well.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.