debugger: fix behavior of plain object exec in debugger repl · nodejs/node@caf81ca · GitHub
Skip to content

Commit caf81ca

Browse files
dario-piotrowiczmeixg
authored andcommitted
debugger: fix behavior of plain object exec in debugger repl
Co-authored-by: Xuguang Mei <meixuguang@gmail.com> PR-URL: #57498 Fixes: #46808 Reviewed-By: Yagiz Nizipli <yagiz@nizipli.com> Reviewed-By: James M Snell <jasnell@gmail.com> Reviewed-By: Xuguang Mei <meixuguang@gmail.com> Reviewed-By: Ruben Bridgewater <ruben@bridgewater.de>
1 parent 30d4a43 commit caf81ca

4 files changed

Lines changed: 34 additions & 8 deletions

File tree

lib/internal/debugger/inspect_repl.js

Lines changed: 9 additions & 1 deletion

lib/internal/repl/utils.js

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -739,11 +739,28 @@ function setupReverseSearch(repl) {
739739
return { reverseSearch };
740740
}
741741

742+
const startsWithBraceRegExp = /^\s*{/;
743+
const endsWithSemicolonRegExp = /;\s*$/;
744+
745+
/**
746+
* Checks if some provided code represents an object literal.
747+
* This is helpful to prevent confusing repl code evaluations where
748+
* strings such as `{ a : 1 }` would get interpreted as block statements
749+
* rather than object literals.
750+
* @param {string} code the code to check
751+
* @returns {boolean} true if the code represents an object literal, false otherwise
752+
*/
753+
function isObjectLiteral(code) {
754+
return RegExpPrototypeExec(startsWithBraceRegExp, code) !== null &&
755+
RegExpPrototypeExec(endsWithSemicolonRegExp, code) === null;
756+
}
757+
742758
module.exports = {
743759
REPL_MODE_SLOPPY: Symbol('repl-sloppy'),
744760
REPL_MODE_STRICT,
745761
isRecoverableError,
746762
kStandaloneREPL: Symbol('kStandaloneREPL'),
747763
setupPreview,
748764
setupReverseSearch,
765+
isObjectLiteral,
749766
};

lib/repl.js

Lines changed: 3 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -171,6 +171,7 @@ const {
171171
kStandaloneREPL,
172172
setupPreview,
173173
setupReverseSearch,
174+
isObjectLiteral,
174175
} = require('internal/repl/utils');
175176
const {
176177
constants: {
@@ -440,13 +441,8 @@ function REPLServer(prompt,
440441
let awaitPromise = false;
441442
const input = code;
442443

443-
// It's confusing for `{ a : 1 }` to be interpreted as a block
444-
// statement rather than an object literal. So, we first try
445-
// to wrap it in parentheses, so that it will be interpreted as
446-
// an expression. Note that if the above condition changes,
447-
// lib/internal/repl/utils.js needs to be changed to match.
448-
if (RegExpPrototypeExec(/^\s*{/, code) !== null &&
449-
RegExpPrototypeExec(/;\s*$/, code) === null) {
444+
if (isObjectLiteral(code)) {
445+
// Add parentheses to make sure `code` is parsed as an expression
450446
code = `(${StringPrototypeTrim(code)})\n`;
451447
wrappedCmd = true;
452448
}

test/parallel/test-debugger-exec.js

Lines changed: 5 additions & 0 deletions

0 commit comments

Comments
 (0)