jquery / esprima

ECMAScript parsing infrastructure for multipurpose analysis
http://esprima.org
BSD 2-Clause "Simplified" License
7.04k stars 785 forks source link

Unexpected token ILLEGAL on /^ #2030

Open tolkushkin opened 4 years ago

tolkushkin commented 4 years ago

Unexpected token ILLEGAL on /^

I try parse following js

isImage=function(e){return/^image\//.test(e.type)
and get ParserExeption Line 1: Unexpected token ILLEGAL This code works in chrome without any errors. Code to parse

var s = new Scanner(@"isImage=function(e){return/^image\//.test(e.type)}");
var tokens = new List();
Token token;
do
{
    s.ScanComments();
    token = s.Lex();
    tokens.Add(token);
} while (token.Type != TokenType.EOF);
ariya commented 4 years ago

Hi @tolkushkin, thanks for the report.

Did you try it on https://esprima.org/demo/parse.html to see if it works there? When I did that and use the following (note that I added the closing curly brace), everything works as expected.

isImage=function(e){return/^image\//.test(e.type)}

Also, for using the pure tokenizer, the same thing happens (working as expected) from the command line (try it with https://npm.runkit.com/esprima):

esprima.tokenize('isImage=function(e){return/^image\//.test(e.type)}')
TejaswiniU commented 3 years ago

Hi @ariya , It would be better to know which branch was being used in web https://esprima.org/demo/parse.html

Certain code is being parsed in web but throws errors while using npm or direct source code

Eg: esprima/dist/esprima.js:3781 throw e; ^

Error: Line 2: Unexpected reserved word