Skip to content
Closed
Show file tree
Hide file tree
Changes from 6 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
15 changes: 14 additions & 1 deletion lib/internal/repl/utils.js
Original file line number Diff line number Diff line change
Expand Up @@ -299,7 +299,9 @@ function setupPreview(repl, contextSymbol, bufferSymbol, active) {
// For similar reasons as `defaultEval`, wrap expressions starting with a
// curly brace with parenthesis.
if (StringPrototypeStartsWith(input, '{') &&
!StringPrototypeEndsWith(input, ';') && !wrapped) {
!StringPrototypeEndsWith(input, ';') &&
isValidSyntax(input) &&
!wrapped) {
input = `(${input})`;
wrapped = true;
}
Expand Down Expand Up @@ -740,11 +742,22 @@ function setupReverseSearch(repl) {
return { reverseSearch };
}

function isValidSyntax(input) {
const Parser = require('internal/deps/acorn/acorn/dist/acorn').Parser;
try {
Parser.parse(input, { ecmaVersion: 'latest' });
return true;
} catch {
return false;
}
}

module.exports = {
REPL_MODE_SLOPPY: Symbol('repl-sloppy'),
REPL_MODE_STRICT,
isRecoverableError,
kStandaloneREPL: Symbol('kStandaloneREPL'),
setupPreview,
setupReverseSearch,
isValidSyntax,
};
5 changes: 4 additions & 1 deletion lib/repl.js
Original file line number Diff line number Diff line change
Expand Up @@ -163,6 +163,7 @@ const {
kStandaloneREPL,
setupPreview,
setupReverseSearch,
isValidSyntax,
} = require('internal/repl/utils');
const {
constants: {
Expand Down Expand Up @@ -420,7 +421,8 @@ function REPLServer(prompt,
// an expression. Note that if the above condition changes,
// lib/internal/repl/utils.js needs to be changed to match.
if (RegExpPrototypeExec(/^\s*{/, code) !== null &&
RegExpPrototypeExec(/;\s*$/, code) === null) {
RegExpPrototypeExec(/;\s*$/, code) === null &&
isValidSyntax(code)) {
code = `(${StringPrototypeTrim(code)})\n`;
wrappedCmd = true;
}
Expand Down Expand Up @@ -1819,6 +1821,7 @@ module.exports = {
REPL_MODE_SLOPPY,
REPL_MODE_STRICT,
Recoverable,
isValidSyntax,
};

ObjectDefineProperty(module.exports, 'builtinModules', {
Expand Down
43 changes: 41 additions & 2 deletions test/parallel/test-repl-preview.js
Original file line number Diff line number Diff line change
Expand Up @@ -157,6 +157,40 @@ async function tests(options) {
'\x1B[90m1\x1B[39m\x1B[12G\x1B[1A\x1B[1B\x1B[2K\x1B[1A\r',
'\x1B[33m1\x1B[39m',
]
}, {
input: 'aaaa',
noPreview: 'Uncaught ReferenceError: aaaa is not defined',
preview: [
'aaaa\r',
'Uncaught ReferenceError: aaaa is not defined',
]
}, {
input: '/0',
noPreview: '/0',
preview: [
'/0\r',
'/0',
'^',
'',
'Uncaught SyntaxError: Invalid regular expression: missing /',
]
}, {
input: '{})',
noPreview: '{})',
preview: [
'{})\r',
'{})',
' ^',
'',
"Uncaught SyntaxError: Unexpected token ')'",
],
}, {
input: "{ a: '{' }",
noPreview: "{ a: \x1B[32m'{'\x1B[39m }",
preview: [
"{ a: '{' }\r",
"{ a: \x1B[32m'{'\x1B[39m }",
],
}];

const hasPreview = repl.terminal &&
Expand All @@ -177,8 +211,13 @@ async function tests(options) {
assert.deepStrictEqual(lines, preview);
} else {
assert.ok(lines[0].includes(noPreview), lines.map(inspect));
if (preview.length !== 1 || preview[0] !== `${input}\r`)
assert.strictEqual(lines.length, 2);
if (preview.length !== 1 || preview[0] !== `${input}\r`) {
if (preview[preview.length - 1].includes('Uncaught SyntaxError')) {
assert.strictEqual(lines.length, 5);
} else {
assert.strictEqual(lines.length, 2);
}
}
}
}
}
Expand Down