GeezerVison is showing github.com — repl: support mult-line string-keyed objects by rubys · Pull Request #21805 · nodejs/node
repl: support mult-line string-keyed objects
#21805
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and
privacy statement . We’ll occasionally send you account related emails.
Already on GitHub?
Sign in
to your account
Uh oh!
There was an error while loading. Please reload this page.
Show file tree
Hide file tree
Changes from all commits
Commits
File filter
Filter by extension
.gyp
(1)
.js
(3)
All 2 file types selected
Viewed files
Conversations
Failed to load comments.
Loading
Uh oh!
There was an error while loading. Please reload this page.
Jump to
Jump to file
Failed to load files.
Loading
Uh oh!
There was an error while loading. Please reload this page.
Diff view
Hide whitespace
Diff view
Hide whitespace
There are no files selected for viewing
Original file line number
Diff line number
Diff line change
@@ -0,0 +1,79 @@
'use strict';
const acorn = require('internal/deps/acorn/dist/acorn');
const { tokTypes: tt } = acorn;
// If the error is that we've unexpectedly ended the input,
// then let the user try to recover by adding more input.
// Note: `e` (the original exception) is not used by the current implemention,
// but may be needed in the future.
function isRecoverableError(e, code) {
let recoverable = false;
// Determine if the point of the any error raised is at the end of the input.
// There are two cases to consider:
//
// 1. Any error raised after we have encountered the 'eof' token.
// This prevents us from declaring partial tokens (like '2e') as
// recoverable.
//
// 2. Three cases where tokens can legally span lines. This is
// template, comment, and strings with a backslash at the end of
// the line, indicating a continuation. Note that we need to look
// for the specific errors of 'unterminated' kind (not, for example,
// a syntax error in a ${} expression in a template), and the only
// way to do that currently is to look at the message. Should Acorn
// change these messages in the future, this will lead to a test
// failure, indicating that this code needs to be updated.
//
acorn.plugins.replRecoverable = (parser) => {
parser.extend('nextToken', (nextToken) => {
return function() {
Reflect.apply(nextToken, this, []);
if (this.type === tt.eof) recoverable = true;
};
});
parser.extend('raise', (raise) => {
return function(pos, message) {
switch (message) {
case 'Unterminated template':
case 'Unterminated comment':
recoverable = true;
break;
case 'Unterminated string constant':
const token = this.input.slice(this.lastTokStart, this.pos);
// see https://www.ecma-international.org/ecma-262/#sec-line-terminators
recoverable = /\\(?:\r\n?|\n|\u2028|\u2029)$/.test(token);
}
Reflect.apply(raise, this, [pos, message]);
};
});
};
// For similar reasons as `defaultEval`, wrap expressions starting with a
// curly brace with parenthesis. Note: only the open parenthesis is added
// here as the point is to test for potentially valid but incomplete
// expressions.
if (/^\s*\{/.test(code) && isRecoverableError(e, `(${code}`)) return true;
// Try to parse the code with acorn. If the parse fails, ignore the acorn
// error and return the recoverable status.
try {
acorn.parse(code, { plugins: { replRecoverable: true } });
// Odd case: the underlying JS engine (V8, Chakra) rejected this input
// but Acorn detected no issue. Presume that additional text won't
// address this issue.
return false;
} catch {
return recoverable;
}
}
module.exports = {
isRecoverableError
};
Original file line number
Diff line number
Diff line change
Expand Up
@@ -73,6 +73,7 @@ const {
} = require('internal/errors').codes;
const { sendInspectorCommand } = require('internal/util/inspector');
const { experimentalREPLAwait } = process.binding('config');
const { isRecoverableError } = require('internal/repl/recoverable');
// Lazy-loaded.
let processTopLevelAwait;
Expand Down
Expand Up
@@ -227,7 +228,8 @@ function REPLServer(prompt,
// It's confusing for `{ a : 1 }` to be interpreted as a block
// statement rather than an object literal. So, we first try
// to wrap it in parentheses, so that it will be interpreted as
// an expression.
// an expression. Note that if the above condition changes,
// lib/internal/repl/recoverable.js needs to be changed to match.
code = `(${code.trim()})\n`;
wrappedCmd = true;
}
Expand Down
Expand Up
@@ -1505,76 +1507,6 @@ function regexpEscape(s) {
return s.replace(/[-[\]{}()*+?.,\\^$|#\s]/g, '\\$&');
}
// If the error is that we've unexpectedly ended the input,
// then let the user try to recover by adding more input.
function isRecoverableError(e, code) {
if (e && e.name === 'SyntaxError') {
var message = e.message;
if (message === 'Unterminated template literal' ||
message === 'Unexpected end of input') {
return true;
}
if (message === 'missing ) after argument list') {
const frames = e.stack.split(/\r?\n/);
const pos = frames.findIndex((f) => f.match(/^\s*\^+$/));
return pos > 0 && frames[pos - 1].length === frames[pos].length;
}
if (message === 'Invalid or unexpected token')
return isCodeRecoverable(code);
}
return false;
}
// Check whether a code snippet should be forced to fail in the REPL.
function isCodeRecoverable(code) {
var current, previous, stringLiteral;
var isBlockComment = false;
var isSingleComment = false;
var isRegExpLiteral = false;
var lastChar = code.charAt(code.length - 2);
var prevTokenChar = null;
for (var i = 0; i < code.length; i++) {
previous = current;
current = code[i];
if (previous === '\\' && (stringLiteral || isRegExpLiteral)) {
current = null;
} else if (stringLiteral) {
if (stringLiteral === current) {
stringLiteral = null;
}
} else if (isRegExpLiteral && current === '/') {
isRegExpLiteral = false;
} else if (isBlockComment && previous === '*' && current === '/') {
isBlockComment = false;
} else if (isSingleComment && current === '\n') {
isSingleComment = false;
} else if (!isBlockComment && !isRegExpLiteral && !isSingleComment) {
if (current === '/' && previous === '/') {
isSingleComment = true;
} else if (previous === '/') {
if (current === '*') {
isBlockComment = true;
// Distinguish between a division operator and the start of a regex
// by examining the non-whitespace character that precedes the /
} else if ([null, '(', '[', '{', '}', ';'].includes(prevTokenChar)) {
isRegExpLiteral = true;
}
} else {
if (current.trim()) prevTokenChar = current;
if (current === '\'' || current === '"') {
stringLiteral = current;
}
}
}
}
return stringLiteral ? lastChar === '\\' : isBlockComment;
}
function Recoverable(err) {
this.err = err;
}
Expand Down
1 change: 1 addition & 0 deletions
1
node.gyp
Original file line number
Diff line number
Diff line change
Expand Up
@@ -147,6 +147,7 @@
'lib/internal/readline.js',
'lib/internal/repl.js',
'lib/internal/repl/await.js',
'lib/internal/repl/recoverable.js',
'lib/internal/socket_list.js',
'lib/internal/test/binding.js',
'lib/internal/test/heap.js',
Expand Down
Original file line number
Diff line number
Diff line change
Expand Up
@@ -162,13 +162,11 @@ const errorTests = [
// Template expressions
{
send: '`io.js ${"1.0"',
expect: [
kSource,
kArrow,
'',
/^SyntaxError: /,
''
]
expect: '... '
},
{
send: '+ ".2"}`',
expect: '\'io.js 1.0.2\''
},
{
send: '`io.js ${',
Expand Down
Expand Up
@@ -315,6 +313,15 @@ const errorTests = [
send: '1 }',
expect: '{ a: 1 }'
},
// Multiline string-keyed object (e.g. JSON)
{
send: '{ "a": ',
expect: '... '
},
{
send: '1 }',
expect: '{ a: 1 }'
},
// Multiline anonymous function with comment
{
send: '(function() {',
Expand Down
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.