File tree Expand file tree Collapse file tree 4 files changed +34
-8
lines changed Expand file tree Collapse file tree 4 files changed +34
-8
lines changed Original file line number Diff line number Diff line change @@ -56,6 +56,7 @@ const { fileURLToPath } = require('internal/url');
5656
5757const { customInspectSymbol, SideEffectFreeRegExpPrototypeSymbolReplace } = require ( 'internal/util' ) ;
5858const { inspect : utilInspect } = require ( 'internal/util/inspect' ) ;
59+ const { isObjectLiteral } = require ( 'internal/repl/utils' ) ;
5960const debuglog = require ( 'internal/util/debuglog' ) . debuglog ( 'inspect' ) ;
6061
6162const SHORTCUTS = {
@@ -573,8 +574,15 @@ function createRepl(inspector) {
573574 if ( input === '\n' ) return lastCommand ;
574575 // Add parentheses: exec process.title => exec("process.title");
575576 const match = RegExpPrototypeExec ( / ^ \s * (?: e x e c | p ) \s + ( [ ^ \n ] * ) / , input ) ;
577+ input = match ? match [ 1 ] : input ;
578+
579+ if ( isObjectLiteral ( input ) ) {
580+ // Add parentheses to make sure `input` is parsed as an expression
581+ input = `(${ StringPrototypeTrim ( input ) } )\n` ;
582+ }
583+
576584 if ( match ) {
577- lastCommand = `exec(${ JSONStringify ( match [ 1 ] ) } )` ;
585+ lastCommand = `exec(${ JSONStringify ( input ) } )` ;
578586 } else {
579587 lastCommand = input ;
580588 }
Original file line number Diff line number Diff 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+
742758module . 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} ;
Original file line number Diff line number Diff line change @@ -172,6 +172,7 @@ const {
172172 kStandaloneREPL,
173173 setupPreview,
174174 setupReverseSearch,
175+ isObjectLiteral,
175176} = require ( 'internal/repl/utils' ) ;
176177const {
177178 constants : {
@@ -436,13 +437,8 @@ function REPLServer(prompt,
436437 let awaitPromise = false ;
437438 const input = code ;
438439
439- // It's confusing for `{ a : 1 }` to be interpreted as a block
440- // statement rather than an object literal. So, we first try
441- // to wrap it in parentheses, so that it will be interpreted as
442- // an expression. Note that if the above condition changes,
443- // lib/internal/repl/utils.js needs to be changed to match.
444- if ( RegExpPrototypeExec ( / ^ \s * { / , code ) !== null &&
445- RegExpPrototypeExec ( / ; \s * $ / , code ) === null ) {
440+ if ( isObjectLiteral ( code ) ) {
441+ // Add parentheses to make sure `code` is parsed as an expression
446442 code = `(${ StringPrototypeTrim ( code ) } )\n` ;
447443 wrappedCmd = true ;
448444 }
Original file line number Diff line number Diff line change @@ -60,6 +60,11 @@ async function waitInitialBreak() {
6060 / \[ ' u n d e f i n e d ' , ' f u n c t i o n ' \] / ,
6161 'non-paused exec can see global but not module-scope values'
6262 ) ;
63+
64+ // Ref: https://github.com/nodejs/node/issues/46808
65+ await cli . waitForPrompt ( ) ;
66+ await cli . command ( 'exec { a: 1 }' ) ;
67+ assert . match ( cli . output , / \{ a : 1 \} / ) ;
6368 } finally {
6469 await cli . quit ( ) ;
6570 }
You can’t perform that action at this time.
0 commit comments