Skip to content

Commit 5f0cdb6

Browse files
codebyteretargos
authored andcommitted
repl: eager-evaluate input in parens
PR-URL: nodejs#31943 Reviewed-By: Gus Caplan <me@gus.host> Reviewed-By: James M Snell <jasnell@gmail.com> Reviewed-By: Ruben Bridgewater <ruben@bridgewater.de>
1 parent 1ce305f commit 5f0cdb6

File tree

2 files changed

+23
-6
lines changed

2 files changed

+23
-6
lines changed

lib/repl.js

+6-6
Original file line numberDiff line numberDiff line change
@@ -323,12 +323,12 @@ function REPLServer(prompt,
323323
let awaitPromise = false;
324324
const input = code;
325325

326-
if (/^\s*{/.test(code) && /}\s*$/.test(code)) {
327-
// It's confusing for `{ a : 1 }` to be interpreted as a block
328-
// statement rather than an object literal. So, we first try
329-
// to wrap it in parentheses, so that it will be interpreted as
330-
// an expression. Note that if the above condition changes,
331-
// lib/internal/repl/utils.js needs to be changed to match.
326+
// It's confusing for `{ a : 1 }` to be interpreted as a block
327+
// statement rather than an object literal. So, we first try
328+
// to wrap it in parentheses, so that it will be interpreted as
329+
// an expression. Note that if the above condition changes,
330+
// lib/internal/repl/utils.js needs to be changed to match.
331+
if (/^\s*{/.test(code) && !/;\s*$/.test(code)) {
332332
code = `(${code.trim()})\n`;
333333
wrappedCmd = true;
334334
}

test/parallel/test-repl-preview.js

+17
Original file line numberDiff line numberDiff line change
@@ -88,6 +88,23 @@ async function tests(options) {
8888
'\x1B[36m[Function: koo]\x1B[39m',
8989
'\x1B[1G\x1B[0Jrepl > \x1B[8G'],
9090
['a', [1, 2], undefined],
91+
[" { b: 1 }['b'] === 1", [2, 6], '\x1B[33mtrue\x1B[39m',
92+
" { b: 1 }['b']",
93+
'\x1B[90m1\x1B[39m\x1B[22G\x1B[1A\x1B[1B\x1B[2K\x1B[1A ',
94+
'\x1B[90m1\x1B[39m\x1B[23G\x1B[1A\x1B[1B\x1B[2K\x1B[1A=== 1',
95+
'\x1B[90mtrue\x1B[39m\x1B[28G\x1B[1A\x1B[1B\x1B[2K\x1B[1A\r',
96+
'\x1B[33mtrue\x1B[39m',
97+
'\x1B[1G\x1B[0Jrepl > \x1B[8G'
98+
],
99+
["{ b: 1 }['b'] === 1;", [2, 7], '\x1B[33mfalse\x1B[39m',
100+
"{ b: 1 }['b']",
101+
'\x1B[90m1\x1B[39m\x1B[21G\x1B[1A\x1B[1B\x1B[2K\x1B[1A ',
102+
'\x1B[90m1\x1B[39m\x1B[22G\x1B[1A\x1B[1B\x1B[2K\x1B[1A=== 1',
103+
'\x1B[90mtrue\x1B[39m\x1B[27G\x1B[1A\x1B[1B\x1B[2K\x1B[1A;',
104+
'\x1B[90mfalse\x1B[39m\x1B[28G\x1B[1A\x1B[1B\x1B[2K\x1B[1A\r',
105+
'\x1B[33mfalse\x1B[39m',
106+
'\x1B[1G\x1B[0Jrepl > \x1B[8G'
107+
],
91108
['{ a: true }', [2, 3], '{ a: \x1B[33mtrue\x1B[39m }',
92109
'{ a: tru\x1B[90me\x1B[39m\x1B[16G\x1B[0Ke }\r',
93110
'{ a: \x1B[33mtrue\x1B[39m }',

0 commit comments

Comments
 (0)