Skip to content
Prev Previous commit
Next Next commit
test: add open bracket case
  • Loading branch information
jenthone authored and meixg committed Aug 24, 2025
commit 94cad394fd494ad98e7a6d2a76bcc0ce38129cd1
78 changes: 43 additions & 35 deletions lib/internal/repl/utils.js
Original file line number Diff line number Diff line change
Expand Up @@ -769,44 +769,52 @@ const endsWithSemicolonRegExp = /;\s*$/;
function isObjectLiteral(code) {
return RegExpPrototypeExec(startsWithBraceRegExp, code) !== null &&
RegExpPrototypeExec(endsWithSemicolonRegExp, code) === null;
}
function isValidSyntax(input) {
const Parser = require('internal/deps/acorn/acorn/dist/acorn').Parser;
try {
Parser.parse(input, { ecmaVersion: 'latest' });
return true;
} catch {
return false;
}
}

function isValidParentheses(input) {
const stack = [];
const pairs = {
'(': ')',
'[': ']',
'{': '}',
};
function isValidParentheses(input) {
const stack = [];
const pairs = {
'(': ')',
'[': ']',
'{': '}',
};

for (let i = 0; i < input.length; i++) {
const char = input[i];
for (let i = 0; i < input.length; i++) {
const char = input[i];

if (pairs[char]) {
stack.push(char);
} else if (char === ')' || char === ']' || char === '}') {
if (pairs[stack.pop()] !== char) {
return false;
if (pairs[char]) {
stack.push(char);
} else if (char === ')' || char === ']' || char === '}') {
if (pairs[stack.pop()] !== char) {
return false;
}
}
}
function isValidSyntax(input) {
const Parser = require('internal/deps/acorn/acorn/dist/acorn').Parser;
try {
let result = Parser.parse(input, { ecmaVersion: 'latest' });
return true;
} catch (e) {
return false;
function isValidSyntax(input) {
const Parser = require('internal/deps/acorn/acorn/dist/acorn').Parser;
try {
let result = Parser.parse(input, { ecmaVersion: 'latest' });
return true;
} catch (e) {
return false;
}
}
}

module.exports = {
REPL_MODE_SLOPPY: Symbol('repl-sloppy'),
REPL_MODE_STRICT,
isRecoverableError,
kStandaloneREPL: Symbol('kStandaloneREPL'),
setupPreview,
setupReverseSearch,
isObjectLiteral,
isValidParentheses,
isValidSyntax,
};
module.exports = {
REPL_MODE_SLOPPY: Symbol('repl-sloppy'),
REPL_MODE_STRICT,
isRecoverableError,
kStandaloneREPL: Symbol('kStandaloneREPL'),
setupPreview,
setupReverseSearch,
isObjectLiteral,
isValidParentheses,
isValidSyntax,
};
7 changes: 7 additions & 0 deletions test/parallel/test-repl-preview.js
Original file line number Diff line number Diff line change
Expand Up @@ -184,6 +184,13 @@ async function tests(options) {
'',
"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 Down