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: improve static import error message in repl #33588

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
9 changes: 9 additions & 0 deletions lib/repl.js
Expand Up @@ -58,6 +58,7 @@ const {
PromiseRace,
RegExp,
Set,
StringPrototypeIncludes,
Symbol,
WeakSet,
} = primordials;
Expand Down Expand Up @@ -570,6 +571,14 @@ function REPLServer(prompt,
e.stack = e.stack
.replace(/^repl:\d+\r?\n/, '')
.replace(/^\s+at\s.*\n?/gm, '');
const importErrorStr = 'Cannot use import statement outside a ' +
'module';
if (StringPrototypeIncludes(e.message, importErrorStr)) {
e.message = 'Cannot use import statement inside the Node.js ' +
'repl, alternatively use dynamic import';
e.stack = e.stack.replace(/SyntaxError:.*\n/,
`SyntaxError: ${e.message}\n`);
}
} else if (self.replMode === module.exports.REPL_MODE_STRICT) {
e.stack = e.stack.replace(/(\s+at\s+repl:)(\d+)/,
(_, pre, line) => pre + (line - 1));
Expand Down
10 changes: 10 additions & 0 deletions test/parallel/test-repl.js
Expand Up @@ -805,6 +805,16 @@ const tcpTests = [
{
send: `require(${JSON.stringify(moduleFilename)}).number`,
expect: '42'
},
{
send: 'import comeOn from \'fhqwhgads\'',
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
send: 'import comeOn from \'fhqwhgads\'',
send: 'import everybody from \'the limit\'',

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

On second thought...

Suggested change
send: 'import comeOn from \'fhqwhgads\'',
send: 'import theLimit from \'everybody\'',

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Long descriptive names are preferable.

Suggested change
send: 'import comeOn from \'fhqwhgads\'',
send: 'import comeOn from \'fhqwhgadshgnsdhjsdbkhsdabkfabkveybvf\'',

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LOLOLOL

guess this will need to come in a followup

expect: [
kSource,
kArrow,
'',
'Uncaught:',
/^SyntaxError: .* dynamic import/
]
}
];

Expand Down