google / closure-linter

Automatically exported from code.google.com/p/closure-linter
Apache License 2.0
109 stars 37 forks source link

Parse error on anonymous blocks #26

Open GoogleCodeExporter opened 9 years ago

GoogleCodeExporter commented 9 years ago
A javascript file with a non-empty anonymous block will fail to be parsed by 
the closure-linter v2.2.7.

The following snippet demonstrates the problem:

var foo = function() {
    {
        var bar = 42;
    };
};

The closure-linter v2.2.7 will report the following error:
Line 5, E:-002: Error parsing file at token "}". Unable to check the rest of 
file.

What I expect would rather be a warning/error that anonymous blocks should be 
avoided to prevent the illusion of block-scope, whereas javascript only 
provides function-scope.

This bug makes it impossible to run the closure-linter on a large file that 
contains one or more anonymous blocks.

Original issue reported on code.google.com by oseem...@gmail.com on 12 Mar 2011 at 9:53

GoogleCodeExporter commented 9 years ago
I've never even seen someone try to use an anonymous block in JS before, 
precisely because there is no block scope. Is it even technically valid JS?

Either way, we probably shouldn't crash on this, and it would be a useful lint 
error though not a high priority to implement since I don't think many people 
make the mistake in the first place.

Checking the Closure Compiler, it seems to automatically remove these anonymous 
blocks but doesn't error or warn on them, at least with the default settings on 
http://closure-compiler.appspot.com/home.

Original comment by a...@google.com on 8 Apr 2011 at 9:00

GoogleCodeExporter commented 9 years ago
I am affected by this bug, too. It happened to me while linting a long, complex 
minified file. (jquery.dataTables.min.js from the popular datatables plugin for 
jQuery)

Original comment by knb.fin...@gmail.com on 30 Sep 2014 at 1:10

GoogleCodeExporter commented 9 years ago
forgot to say that I was using  gjslint v2.3.13

gjslint --disable 131,1,2,5,10,11,300,-002 --nojsdoc --max_line_length 12000 
jquery.dataTables.min.js

Line 154, E:-002: Error parsing file at token "<JavaScriptToken: 154, }, "}", 
{}, MetaData(None)>". Unable to check the rest of file.
Error "None"
Found 1 errors, including 0 new errors, in 1 files (0 files OK).

Original comment by knb.fin...@gmail.com on 30 Sep 2014 at 1:17