From 4b1e9ca82100f2aae537488ae14827e7f817fd02 Mon Sep 17 00:00:00 2001 From: islandryu Date: Sun, 8 Sep 2024 20:36:32 +0900 Subject: [PATCH 1/4] 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 --- lib/repl.js | 4 +++- test/parallel/test-repl.js | 22 ++++++++++++++++++++++ 2 files changed, 25 insertions(+), 1 deletion(-) diff --git a/lib/repl.js b/lib/repl.js index 6e2d8120ad2167..a7aa4c717837dd 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 4d406a8a36caaf..c9b1952b996c5f 100644 --- a/test/parallel/test-repl.js +++ b/test/parallel/test-repl.js @@ -129,6 +129,18 @@ const strictModeTests = [ }, ]; +const possibleTokensAfterIdentifier = [ + '()', + '[0]', + '+ 1', '- 1', '* 1', '/ 1', '% 1', '** 1', + '== 1', '=== 1', '!= 1', '!== 1', '< 1', '> 1', '<= 1', '>= 1', + '&& 1', '|| 1', '?? 1', + '= 1', '+= 1', '-= 1', '*= 1', '/= 1', '%= 1', + '++', '--', + ':', + '? 1: 1', +]; + const errorTests = [ // Uncaught error throws and prints out { @@ -386,6 +398,16 @@ const errorTests = [ '(Press Ctrl+D to exit.)', ] }, + { + send: 'let npm = () => {};', + expect: 'undefined' + }, + ...possibleTokensAfterIdentifier.map((token) => ( + { + send: `npm ${token}; undefined`, + expect: 'undefined' + } + )), { send: '(function() {\n\nreturn 1;\n})()', expect: '... ... ... 1' From 8892202f8e04b4d0c10b50102413f7fadfaafc26 Mon Sep 17 00:00:00 2001 From: islandryu Date: Mon, 9 Sep 2024 13:55:47 +0900 Subject: [PATCH 2/4] repl: Add test for recoverable errors --- test/parallel/test-repl.js | 21 +++++++++++++++++++++ 1 file changed, 21 insertions(+) diff --git a/test/parallel/test-repl.js b/test/parallel/test-repl.js index c9b1952b996c5f..30bb1d1122a44a 100644 --- a/test/parallel/test-repl.js +++ b/test/parallel/test-repl.js @@ -141,6 +141,17 @@ const possibleTokensAfterIdentifier = [ '? 1: 1', ]; +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 { @@ -408,6 +419,16 @@ const errorTests = [ expect: 'undefined' } )), + { + send: 'npm = () => {};', + expect: '[Function: npm]' + }, + ...possibleTokensAfterIdentifierWithLineBreak.map((token) => ( + { + send: `npm ${token}; undefined`, + expect: '... undefined' + } + )), { send: '(function() {\n\nreturn 1;\n})()', expect: '... ... ... 1' From c7203ddc1b4538fd05545f6a657e1e5d54963513 Mon Sep 17 00:00:00 2001 From: islandryu Date: Tue, 10 Sep 2024 00:10:03 +0900 Subject: [PATCH 3/4] repl: remove valid js test including npm --- test/parallel/test-repl.js | 22 ---------------------- 1 file changed, 22 deletions(-) diff --git a/test/parallel/test-repl.js b/test/parallel/test-repl.js index 30bb1d1122a44a..3d073d521ef235 100644 --- a/test/parallel/test-repl.js +++ b/test/parallel/test-repl.js @@ -129,18 +129,6 @@ const strictModeTests = [ }, ]; -const possibleTokensAfterIdentifier = [ - '()', - '[0]', - '+ 1', '- 1', '* 1', '/ 1', '% 1', '** 1', - '== 1', '=== 1', '!= 1', '!== 1', '< 1', '> 1', '<= 1', '>= 1', - '&& 1', '|| 1', '?? 1', - '= 1', '+= 1', '-= 1', '*= 1', '/= 1', '%= 1', - '++', '--', - ':', - '? 1: 1', -]; - const possibleTokensAfterIdentifierWithLineBreak = [ '(\n)', '[\n0]', @@ -413,16 +401,6 @@ const errorTests = [ send: 'let npm = () => {};', expect: 'undefined' }, - ...possibleTokensAfterIdentifier.map((token) => ( - { - send: `npm ${token}; undefined`, - expect: 'undefined' - } - )), - { - send: 'npm = () => {};', - expect: '[Function: npm]' - }, ...possibleTokensAfterIdentifierWithLineBreak.map((token) => ( { send: `npm ${token}; undefined`, From 2bdc4a4fd942d5d304c18c322f325ab9a60dc6e9 Mon Sep 17 00:00:00 2001 From: islandryu Date: Thu, 12 Sep 2024 00:18:05 +0900 Subject: [PATCH 4/4] repl: fix lint --- test/parallel/test-repl.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/test/parallel/test-repl.js b/test/parallel/test-repl.js index 3d073d521ef235..3b885936496f0a 100644 --- a/test/parallel/test-repl.js +++ b/test/parallel/test-repl.js @@ -138,7 +138,7 @@ const possibleTokensAfterIdentifierWithLineBreak = [ '= \n1', '+= \n1', '-= \n1', '*= \n1', '/= \n1', '%= \n1', ': \n', '? \n1: 1', -] +]; const errorTests = [ // Uncaught error throws and prints out