njs
njs copied to clipboard
for-in parser issue
// "in" <Expression> ")" <Statement>
>> var a = [1,2,3]
undefined
>> for (var x in a);
undefined
>> for (var x in 1,a);
undefined
>> for (x in a);
undefined
>> for (x in 1,a);
Thrown:
SyntaxError: Unexpected token ")" in shell:1
@drsm
- The
inpriority is higher than,. - Take a look at the reference about
for-instatement. https://tc39.es/ecma262/#sec-for-in-and-for-of-statements-static-semantics-early-errors - Another example. It works well in v8.
var x, a, b
a = [1, 2, 3]
b = ['a', 'b']
for (x in a in b) console.log(b[x])
But throw error in njs.
ReferenceError: Invalid left-hand side "x" in for-in statement in 1.js:4
Deep into the source code in the case of for-in.
njs_parser_iteration_statement_for_map(...)
{
...
default:
njs_parser_next(parser, njs_parser_expression);
break;
}
...
return njs_parser_after(parser, current, text, 1,
njs_parser_for_var_in_of_expression);
}
What I mean is that, the keyword in should be checked before parsing expression. But now it does not.
@hongzhidao
found another weird case:
>> for (var x = x in [1,2]; !x; x = 1) console.log(x);
false
undefined
vs.
> for (var x = x in [1,2]; !x; x = 1) console.log(x);
for (var x = x in [1,2]; !x; x = 1) console.log(x);
^
Uncaught SyntaxError: Unexpected token ';'