Simple way to check/validate javascript syntax

14,540

Solution 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).

Solution 2

You could try JSHint, which is less verbose.

Solution 3

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

It only checks syntax, nothing else.

Share:
14,540
filimonov
Author by

filimonov

Updated on June 15, 2022

Comments

  • filimonov
    filimonov almost 2 years

    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;
    
  • filimonov
    filimonov about 11 years
    Looks quite similar to JSLint.
  • ruffin
    ruffin about 11 years
    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.
  • filimonov
    filimonov about 11 years
    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.
  • filimonov
    filimonov about 11 years
    see my add in initial post.
  • filimonov
    filimonov about 11 years
    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
  • ruffin
    ruffin about 11 years
    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.
  • filimonov
    filimonov about 11 years
    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' :\ )
  • Ira Baxter
    Ira Baxter almost 7 years
    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.
  • timmacp
    timmacp almost 6 years
    Esprima seems more useful than JsLint, at least it detects eg a missing bracket. Doesn't integrate with an editor though does it ?