Skip to content

Commit 67b1d4c

Browse files
islandryuRafaelGSS
authored andcommitted
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: #54830 PR-URL: #54848 Reviewed-By: Antoine du Hamel <[email protected]> Reviewed-By: James M Snell <[email protected]> Reviewed-By: Kohei Ueno <[email protected]>
1 parent 8f62f19 commit 67b1d4c

File tree

2 files changed

+24
-1
lines changed

2 files changed

+24
-1
lines changed

lib/repl.js

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -931,7 +931,9 @@ function REPLServer(prompt,
931931
ReflectApply(_memory, self, [cmd]);
932932

933933
if (e && !self[kBufferedCommandSymbol] &&
934-
StringPrototypeStartsWith(StringPrototypeTrim(cmd), 'npm ')) {
934+
StringPrototypeStartsWith(StringPrototypeTrim(cmd), 'npm ') &&
935+
!(e instanceof Recoverable)
936+
) {
935937
self.output.write('npm should be run outside of the ' +
936938
'Node.js REPL, in your normal shell.\n' +
937939
'(Press Ctrl+D to exit.)\n');

test/parallel/test-repl.js

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -129,6 +129,17 @@ const strictModeTests = [
129129
},
130130
];
131131

132+
const possibleTokensAfterIdentifierWithLineBreak = [
133+
'(\n)',
134+
'[\n0]',
135+
'+\n1', '- \n1', '* \n1', '/ \n1', '% \n1', '** \n1',
136+
'== \n1', '=== \n1', '!= \n1', '!== \n1', '< \n1', '> \n1', '<= \n1', '>= \n1',
137+
'&& \n1', '|| \n1', '?? \n1',
138+
'= \n1', '+= \n1', '-= \n1', '*= \n1', '/= \n1', '%= \n1',
139+
': \n',
140+
'? \n1: 1',
141+
];
142+
132143
const errorTests = [
133144
// Uncaught error throws and prints out
134145
{
@@ -386,6 +397,16 @@ const errorTests = [
386397
'(Press Ctrl+D to exit.)',
387398
]
388399
},
400+
{
401+
send: 'let npm = () => {};',
402+
expect: 'undefined'
403+
},
404+
...possibleTokensAfterIdentifierWithLineBreak.map((token) => (
405+
{
406+
send: `npm ${token}; undefined`,
407+
expect: '... undefined'
408+
}
409+
)),
389410
{
390411
send: '(function() {\n\nreturn 1;\n})()',
391412
expect: '... ... ... 1'

0 commit comments

Comments
 (0)