Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

parse error using esprima for file while optimizing js files with r.js

I am optimizing several js files into one using r.js. It works fine before. Recently,I modified some js code, add the code as:

var x = 08;

then it shows

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

ERROR:line 45: Unexpected token ILLEGAL.

Line 45 is where I add var x = 08, and 09 will show error too. It seemed that numbers begining with 0 meanwhile containing 8 or 9 is illegal. Maybe they were treated as bese 8 number .. ?

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

like image 457
florence Avatar asked Nov 01 '13 01:11

florence


1 Answers

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

Generally speaking, it seems that JavaScript interpreters will treat a number with a leading zero that can be interpreted as an octal number as an octal number but if it cannot be interpreted as an octal number (e.g. 08), then they will treat it as decimal.

I've done a test with Node.js and got this:

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

And for 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 on, octals are illegal.

I would avoid octals and never prefix any number with zeros in JavaScript.

like image 117
Louis Avatar answered Nov 04 '22 01:11

Louis