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 <duhamelantoine1995@gmail.com>
Reviewed-By: James M Snell <jasnell@gmail.com>
Reviewed-By: Kohei Ueno <kohei.ueno119@gmail.com>
This commit is contained in:
Shima Ryuhei 2024-09-15 22:25:19 +09:00 committed by GitHub
parent 4c0ad1fef1
commit 86bdca9772
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
2 changed files with 24 additions and 1 deletions

View File

@ -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');

View File

@ -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
{
@ -386,6 +397,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'