-
Notifications
You must be signed in to change notification settings - Fork 31.1k
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: better error for named exports from cjs #33256
Closed
MylesBorins
wants to merge
1
commit into
nodejs:master
from
MylesBorins:better-cjs-named-export-error
Closed
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,64 @@ | ||
import '../common/index.mjs'; | ||
import { rejects } from 'assert'; | ||
|
||
const fixtureBase = '../fixtures/es-modules/package-cjs-named-error'; | ||
|
||
const expectedRelative = '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' + | ||
'For example:\n' + | ||
'import pkg from \'./fail.cjs\';\n' + | ||
'const { comeOn } = pkg;'; | ||
|
||
const expectedPackageHack = 'The requested module \'./json-hack/fail.js\' 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 \'./json-hack/fail.js\';\n' + | ||
'const { comeOn } = pkg;'; | ||
|
||
const expectedBare = 'The requested module \'deep-fail\' 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 \'deep-fail\';\n' + | ||
'const { comeOn } = pkg;'; | ||
|
||
rejects(async () => { | ||
await import(`${fixtureBase}/single-quote.mjs`); | ||
}, { | ||
name: 'SyntaxError', | ||
message: expectedRelative | ||
}, 'should support relative specifiers with single quotes'); | ||
|
||
rejects(async () => { | ||
await import(`${fixtureBase}/double-quote.mjs`); | ||
}, { | ||
name: 'SyntaxError', | ||
message: expectedRelative | ||
}, 'should support relative specifiers with double quotes'); | ||
|
||
rejects(async () => { | ||
await import(`${fixtureBase}/json-hack.mjs`); | ||
}, { | ||
name: 'SyntaxError', | ||
message: expectedPackageHack | ||
}, 'should respect recursive package.json for module type'); | ||
|
||
rejects(async () => { | ||
await import(`${fixtureBase}/bare-import-single.mjs`); | ||
}, { | ||
name: 'SyntaxError', | ||
message: expectedBare | ||
}, 'should support bare specifiers with single quotes'); | ||
|
||
rejects(async () => { | ||
await import(`${fixtureBase}/bare-import-double.mjs`); | ||
}, { | ||
name: 'SyntaxError', | ||
message: expectedBare | ||
}, 'should support bare specifiers with double quotes'); | ||
|
||
rejects(async () => { | ||
await import(`${fixtureBase}/escaped-single-quote.mjs`); | ||
}, /import pkg from '\.\/oh'no\.cjs'/, 'should support relative specifiers with escaped single quote'); |
1 change: 1 addition & 0 deletions
1
test/fixtures/es-modules/package-cjs-named-error/bare-import-double.mjs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
import { comeOn } from "deep-fail"; |
1 change: 1 addition & 0 deletions
1
test/fixtures/es-modules/package-cjs-named-error/bare-import-single.mjs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
import { comeOn } from 'deep-fail'; |
1 change: 1 addition & 0 deletions
1
test/fixtures/es-modules/package-cjs-named-error/double-quote.mjs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
import { comeOn } from "./fail.cjs"; |
1 change: 1 addition & 0 deletions
1
test/fixtures/es-modules/package-cjs-named-error/escaped-single-quote.mjs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
import { value } from "./oh'no.cjs"; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
module.exports = { | ||
comeOn: 'fhqwhgads' | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
import { comeOn } from './json-hack/fail.js'; |
3 changes: 3 additions & 0 deletions
3
test/fixtures/es-modules/package-cjs-named-error/json-hack/fail.js
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
module.exports = { | ||
comeOn: 'fhqwhgads' | ||
}; |
3 changes: 3 additions & 0 deletions
3
test/fixtures/es-modules/package-cjs-named-error/json-hack/package.json
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
{ | ||
"type": "commonjs" | ||
} |
3 changes: 3 additions & 0 deletions
3
test/fixtures/es-modules/package-cjs-named-error/node_modules/deep-fail/index.js
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
4 changes: 4 additions & 0 deletions
4
test/fixtures/es-modules/package-cjs-named-error/node_modules/deep-fail/package.json
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
module.exports = { | ||
value: 123 | ||
}; |
5 changes: 5 additions & 0 deletions
5
test/fixtures/es-modules/package-cjs-named-error/package.json
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
{ | ||
"name": "package-cjs-named-error", | ||
"main": "index.mjs", | ||
"type": "module" | ||
} |
1 change: 1 addition & 0 deletions
1
test/fixtures/es-modules/package-cjs-named-error/single-quote.mjs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
import { comeOn } from './fail.cjs'; |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
everybody to the limit
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Maybe
foo
would be more appropriate here?There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
i don't see how anything could possibly be more appropriate than this particular reference