Skip to content

Commit

Permalink
repl: ensure 'npm' is not interpreted as an npm command
Browse files Browse the repository at this point in the history
This change fixes an issue where 'npm' within JavaScript code was mistakenly interpreted as an npm command. This ensures that 'npm' is treated as expected in all relevant code contexts.

Fixes: nodejs#54830
  • Loading branch information
islandryu committed Sep 8, 2024
1 parent 67357ba commit 9ada2c9
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 1 deletion.
4 changes: 3 additions & 1 deletion lib/repl.js
Original file line number Diff line number Diff line change
Expand Up @@ -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');
Expand Down
22 changes: 22 additions & 0 deletions test/parallel/test-repl.js
Original file line number Diff line number Diff line change
Expand Up @@ -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
{
Expand Down Expand Up @@ -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'
Expand Down

0 comments on commit 9ada2c9

Please sign in to comment.