Parse error using esprima for file when optimizing js files with r.js

I optimize several js files into one using r.js It works great. I recently changed the js code, add the code as:

 var x = 08; 

then he shows

ERROR: parse error using esprima for file D: //webroot/js/a.js

ERROR: line 45: Unexpected ILLEGAL token.

Line 45 is where I add var x = 08 , and 09 will also show an error. It seemed that numbers starting with 0, containing 8 or 9 , were illegal. Maybe they were considered as the 8th number.?

How can I let r.js ignore this point and still optimize js files?

+8
javascript requirejs
source share
4 answers

i had the same problem

It turned out that this is just a double comma requiring files. Ex

 define([ 'file1', 'file2', 'file3',, 'file4' 

Hope this helps

+13
source share

The error is due to Esprima, which r.js uses internally. To reproduce the problem, you can go to this page and enter var x = 08;

Generally speaking, it seems that JavaScript interpreters will process a number with a leading zero, which can be interpreted as an octal number as an octal number, but if it cannot be interpreted as an octal number (for example, 08), then they will consider it as a decimal.

I ran a test with Node.js and got the following:

 $ node > 07 7 > 08 8 > 09 9 > 010 8 > 

And even more fun:

 > (function () {'use strict'; var x = 08;})() undefined > (function () {'use strict'; var x = 012;})() SyntaxError: Octal literals are not allowed in strict mode. [ ... traceback deleted ...] 

When strict mode is enabled, octal values ​​are illegal.

I would avoid octal and never prefix numbers with zeros in JavaScript.

+4
source share

I had the same error when I forgot the question mark in my single-line if statement, inside the js require module.

Invalid line:

 var check = self.currentScroll() > self.lastScroll() ? 1 : self.currentScroll() < self.lastScroll() -1 : 0 

Fixed:

 var check = self.currentScroll() > self.lastScroll() ? 1 : self.currentScroll() < self.lastScroll() ? -1 : 0 
0
source share

I had a problem too, and parsing a very long line:

 var styleHTML = "<style>....</style>" 

It turns out that in the line I also included some comments like this:

 /* ... */ 

When I remove all css comments in a line, it works!

Hope this helps.

-one
source share

All Articles