Repository navigation
repl hates comments with quotes in indented code #3421
Description
Activity
- addedreplIssues and PRs related to the REPL subsystem.Issues and PRs related to the REPL subsystem.
on Oct 18, 2015 I played around with this code today and found there are two other scenarios where we can get this same error.
$ node > function foo() { //' SyntaxError: Unexpected end of input at Object.exports.createScript (vm.js:24:10) at REPLServer.defaultEval (repl.js:137:25) at bound (domain.js:280:14) at REPLServer.runBound [as eval] (domain.js:293:12) at REPLServer.<anonymous> (repl.js:393:12) at emitOne (events.js:82:20) at REPLServer.emit (events.js:169:7) at REPLServer.Interface._onLine (readline.js:210:10) at REPLServer.Interface._line (readline.js:549:8) at REPLServer.Interface._ttyWrite (readline.js:826:14)The other scenario:
$ node > function foo() { ... var i = "'"; SyntaxError: Unexpected end of input at Object.exports.createScript (vm.js:24:10) at REPLServer.defaultEval (repl.js:137:25) at bound (domain.js:280:14) at REPLServer.runBound [as eval] (domain.js:293:12) at REPLServer.<anonymous> (repl.js:393:12) at emitOne (events.js:82:20) at REPLServer.emit (events.js:169:7) at REPLServer.Interface._onLine (readline.js:210:10) at REPLServer.Interface._line (readline.js:549:8) at REPLServer.Interface._ttyWrite (readline.js:826:14)- addedconfirmed-bugIssues and PRs for confirmed bugs.Issues and PRs for confirmed bugs.
on Oct 19, 2015 After reviewing the repl code, I think I have a fix. I've included repl's parseLine function with my annotated changes below. Is this ready for a pull request @thefourtheye ?
function parseLine(line, currentStringLiteral) { var previous = null, current = null; for (var i = 0; i < line.length; i += 1) { if (previous === '\\') { // if it is a valid escaping, then skip processing and the previous // character doesn't matter anymore. previous = null; continue; } current = line.charAt(i); // Issue 3421: If a comment is detected and there is no string // literal, then it's safe to stop parsing the rest of the line if (previous === '/' && current === '/' && currentStringLiteral === null) { break; } else if (current === currentStringLiteral) { currentStringLiteral = null; } // Issue 3421: Operator precedence caused a double quote string to fail // when a single quote was inside the double quote string. else if ((current === '\'' || current === '"') && currentStringLiteral === null) { currentStringLiteral = current; } previous = current; } return currentStringLiteral; }@ChuckLangford I think it's going to be more complicated than that? For example, this fails also, but with a different exception:
> function foo(){ ... /*' SyntaxError: Unexpected token ILLEGAL at Object.exports.createScript (vm.js:24:10) at REPLServer.defaultEval (repl.js:137:25) at bound (domain.js:280:14) at REPLServer.runBound [as eval] (domain.js:293:12) at REPLServer.<anonymous> (repl.js:393:12) at emitOne (events.js:82:20) at REPLServer.emit (events.js:169:7) at REPLServer.Interface._onLine (readline.js:210:10) at REPLServer.Interface._line (readline.js:549:8) at REPLServer.Interface._ttyWrite (readline.js:826:14) >@ChuckLangford Yes, what @mscdex said is true. I am working on a fix with a little of bit of refactoring as well. I think I'll send a PR today or tomorrow.
Awesome. Thanks for the feedback.
As a coding exercise, I'm going to continue working on this problem. @mscdex @thefourtheye have either of you found any other examples of comment related code that will crash repl?
@ChuckLangford I haven't had time to do any exhaustive testing yet, so it's possible there may be some other cases lurking...
PTAL at the proposed fix #3515
- added a commit that references this issue
on Oct 28, 2015 - added a commit that references this issue
on Oct 29, 2015 - added a commit that references this issue
on Jul 27, 2026
Same happens with
//"