From cdc9cba0cfa79ac8113fd06e90507e690777533b Mon Sep 17 00:00:00 2001 From: Shima Ryuhei <65934663+islandryu@users.noreply.github.com> Date: Sun, 15 Sep 2024 22:25:19 +0900 Subject: [PATCH] repl: avoid interpreting 'npm' as a command when errors are recoverable This change ensures that 'npm' within JavaScript code is not mistakenly interpreted as an npm command when the error is recoverable. This allows 'npm' to be treated as expected in such scenarios. Fixes: https://github.com/nodejs/node/issues/54830 PR-URL: https://github.com/nodejs/node/pull/54848 Reviewed-By: Antoine du Hamel Reviewed-By: James M Snell Reviewed-By: Kohei Ueno --- lib/repl.js | 4 +++- test/parallel/test-repl.js | 21 +++++++++++++++++++++ 2 files changed, 24 insertions(+), 1 deletion(-) diff --git a/lib/repl.js b/lib/repl.js index a06ca0dd990f29..8796b4a0cea72c 100644 --- a/lib/repl.js +++ b/lib/repl.js @@ -931,7 +931,9 @@ function REPLServer(prompt, ReflectApply(_memory, self, [cmd]); if (e && !self[kBufferedCommandSymbol] && - StringPrototypeStartsWith(StringPrototypeTrim(cmd), 'npm ')) { + StringPrototypeStartsWith(StringPrototypeTrim(cmd), 'npm ') && + !(e instanceof Recoverable) + ) { self.output.write('npm should be run outside of the ' + 'Node.js REPL, in your normal shell.\n' + '(Press Ctrl+D to exit.)\n'); diff --git a/test/parallel/test-repl.js b/test/parallel/test-repl.js index 44454127dcc5b8..22d1c19f1d58ab 100644 --- a/test/parallel/test-repl.js +++ b/test/parallel/test-repl.js @@ -129,6 +129,17 @@ const strictModeTests = [ }, ]; +const possibleTokensAfterIdentifierWithLineBreak = [ + '(\n)', + '[\n0]', + '+\n1', '- \n1', '* \n1', '/ \n1', '% \n1', '** \n1', + '== \n1', '=== \n1', '!= \n1', '!== \n1', '< \n1', '> \n1', '<= \n1', '>= \n1', + '&& \n1', '|| \n1', '?? \n1', + '= \n1', '+= \n1', '-= \n1', '*= \n1', '/= \n1', '%= \n1', + ': \n', + '? \n1: 1', +]; + const errorTests = [ // Uncaught error throws and prints out { @@ -380,6 +391,16 @@ const errorTests = [ '(Press Ctrl+D to exit.)', ] }, + { + send: 'let npm = () => {};', + expect: 'undefined' + }, + ...possibleTokensAfterIdentifierWithLineBreak.map((token) => ( + { + send: `npm ${token}; undefined`, + expect: '... undefined' + } + )), { send: '(function() {\n\nreturn 1;\n})()', expect: '... ... ... 1'