Following function fails parsing:
function foo(){
var bar = 1 / 1; // '/'
}
SyntaxError: Unexpected end of input
tested with:
This code parses correctly in some node versions 5.X
Interesting... I'm able to reproduce only in the REPL and only when the line var bar = 1 / 1; // '/' occurs within a function.
Yes, that's what I found out too. It also does not fail if you use more than one slash. However, it fails even if the comment is not in the same line.
It works for me with node v6.9.1, even with strict mode.
It looks like this is indeed REPL-specific.
/cc @princejwesley ?
It's a LineParser bug. Looking into it.
_Update_: With our ad-hoc parser, first /(division operator) in var bar = 1 / 1; // '/' is treated as start of regex literal
Without proper state machine, Its not easy to deduce whether '/' is a division operator or start of regex literal.
@dlangerenken Worst case, type/paste in .editor mode.
I'm investigating this issue now. I think it should be possible to correctly distinguish between division and regex literals in almost all cases, with only a single lookbehind. A regex literal is almost always1 preceded by (, {, [, ;, }, or the beginning of input. A division operator is almost always2 preceded by another character.
1 An exception is code like this:
function foo() {
return
/regex/ // not preceded by ;
}
2 An exception is code like this:
({ ok: true } / 3)
Both of these exceptional cases are sort of obselete (they either require dead code, or dividing an object literal by something), so I think using a single-character lookbehind would be an improvement over the current behavior.
Most helpful comment
It's a
LineParserbug. Looking into it._Update_: With our ad-hoc parser, first
/(division operator) invar bar = 1 / 1; // '/'is treated as start of regex literal