Skip to content

Commit

Permalink
repl: eager-evaluate input in parens
Browse files Browse the repository at this point in the history
PR-URL: #31943
Reviewed-By: Gus Caplan <me@gus.host>
Reviewed-By: James M Snell <jasnell@gmail.com>
Reviewed-By: Ruben Bridgewater <ruben@bridgewater.de>
  • Loading branch information
codebytere committed Feb 27, 2020
1 parent e3258fd commit 6adbfac
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 6 deletions.
12 changes: 6 additions & 6 deletions lib/repl.js
Original file line number Diff line number Diff line change
Expand Up @@ -322,12 +322,12 @@ function REPLServer(prompt,
let awaitPromise = false;
const input = code;

if (/^\s*{/.test(code) && /}\s*$/.test(code)) {
// 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. Note that if the above condition changes,
// lib/internal/repl/utils.js needs to be changed to match.
// 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. Note that if the above condition changes,
// lib/internal/repl/utils.js needs to be changed to match.
if (/^\s*{/.test(code) && !/;\s*$/.test(code)) {
code = `(${code.trim()})\n`;
wrappedCmd = true;
}
Expand Down
17 changes: 17 additions & 0 deletions test/parallel/test-repl-preview.js
Original file line number Diff line number Diff line change
Expand Up @@ -88,6 +88,23 @@ async function tests(options) {
'\x1B[36m[Function: koo]\x1B[39m',
'\x1B[1G\x1B[0Jrepl > \x1B[8G'],
['a', [1, 2], undefined],
[" { b: 1 }['b'] === 1", [2, 6], '\x1B[33mtrue\x1B[39m',
" { b: 1 }['b']",
'\x1B[90m1\x1B[39m\x1B[22G\x1B[1A\x1B[1B\x1B[2K\x1B[1A ',
'\x1B[90m1\x1B[39m\x1B[23G\x1B[1A\x1B[1B\x1B[2K\x1B[1A=== 1',
'\x1B[90mtrue\x1B[39m\x1B[28G\x1B[1A\x1B[1B\x1B[2K\x1B[1A\r',
'\x1B[33mtrue\x1B[39m',
'\x1B[1G\x1B[0Jrepl > \x1B[8G'
],
["{ b: 1 }['b'] === 1;", [2, 7], '\x1B[33mfalse\x1B[39m',
"{ b: 1 }['b']",
'\x1B[90m1\x1B[39m\x1B[21G\x1B[1A\x1B[1B\x1B[2K\x1B[1A ',
'\x1B[90m1\x1B[39m\x1B[22G\x1B[1A\x1B[1B\x1B[2K\x1B[1A=== 1',
'\x1B[90mtrue\x1B[39m\x1B[27G\x1B[1A\x1B[1B\x1B[2K\x1B[1A;',
'\x1B[90mfalse\x1B[39m\x1B[28G\x1B[1A\x1B[1B\x1B[2K\x1B[1A\r',
'\x1B[33mfalse\x1B[39m',
'\x1B[1G\x1B[0Jrepl > \x1B[8G'
],
['{ a: true }', [2, 3], '{ a: \x1B[33mtrue\x1B[39m }',
'{ a: tru\x1B[90me\x1B[39m\x1B[16G\x1B[0Ke }\r',
'{ a: \x1B[33mtrue\x1B[39m }',
Expand Down

0 comments on commit 6adbfac

Please sign in to comment.