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

module: fix crash on multiline named cjs imports #35275

Closed
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.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
21 changes: 14 additions & 7 deletions lib/internal/modules/esm/module_job.js
Original file line number Diff line number Diff line change
Expand Up @@ -108,16 +108,23 @@ class ModuleJob {
await this.loader.resolve(childSpecifier, parentFileUrl);
const format = await this.loader.getFormat(childFileURL);
if (format === 'commonjs') {
const importStatement = splitStack[1];
const namedImports = StringPrototypeMatch(importStatement, /{.*}/)[0];
const destructuringAssignment = StringPrototypeReplace(namedImports, /\s+as\s+/g, ': ');
e.message = `The requested module '${childSpecifier}' is expected ` +
'to be of type CommonJS, which does not support named exports. ' +
'CommonJS modules can be imported by importing the default ' +
'export.\n' +
'For example:\n' +
`import pkg from '${childSpecifier}';\n` +
`const ${destructuringAssignment} = pkg;`;
'export.';
// TODO(@ctavan): The original error stack only provides the single
// line which causes the error. For multi-line import statements we
// cannot generate an equivalent object descructuring assignment by
// just parsing the error stack.
const importStatement = splitStack[1];
const oneLineNamedImports = StringPrototypeMatch(importStatement, /{.*}/);
if (oneLineNamedImports) {
const destructuringAssignment =
StringPrototypeReplace(oneLineNamedImports[0], /\s+as\s+/g, ': ');
e.message += '\nFor example:\n' +
`import pkg from '${childSpecifier}';\n` +
`const ${destructuringAssignment} = pkg;`;
}
const newStack = StringPrototypeSplit(e.stack, '\n');
newStack[3] = `SyntaxError: ${e.message}`;
e.stack = ArrayPrototypeJoin(newStack, '\n');
Expand Down
11 changes: 11 additions & 0 deletions test/es-module/test-esm-cjs-named-error.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,10 @@ const expectedRelative = 'The requested module \'./fail.cjs\' is expected to ' +
'import pkg from \'./fail.cjs\';\n' +
'const { comeOn } = pkg;';

const expectedWithoutExample = 'The requested module \'./fail.cjs\' is ' +
'expected to be of type CommonJS, which does not support named exports. ' +
'CommonJS modules can be imported by importing the default export.';

const expectedRenamed = 'The requested module \'./fail.cjs\' is expected to ' +
'be of type CommonJS, which does not support named exports. CommonJS ' +
'modules can be imported by importing the default export.\n' +
Expand Down Expand Up @@ -52,6 +56,13 @@ rejects(async () => {
message: expectedRenamed
}, 'should correctly format named imports with renames');

rejects(async () => {
await import(`${fixtureBase}/multi-line.mjs`);
}, {
name: 'SyntaxError',
message: expectedWithoutExample,
}, 'should correctly format named imports across multiple lines');

rejects(async () => {
await import(`${fixtureBase}/json-hack.mjs`);
}, {
Expand Down
3 changes: 2 additions & 1 deletion test/fixtures/es-modules/package-cjs-named-error/fail.cjs
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
module.exports = {
comeOn: 'fhqwhgads'
comeOn: 'fhqwhgads',
everybody: 'to the limit',
};
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
import {
comeOn,
everybody,
} from './fail.cjs';