Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

repl: avoid interpreting 'npm' as a command when errors are recoverable #54848

Merged
merged 4 commits into from
Sep 15, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
21 changes: 21 additions & 0 deletions test/parallel/test-repl.js
Original file line number Diff line number Diff line change
Expand Up @@ -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
{
Expand Down Expand Up @@ -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'
Expand Down
Loading