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

fix: disallow import assertions in export without from #12264

Merged
merged 1 commit into from Oct 28, 2020
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.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
9 changes: 4 additions & 5 deletions packages/babel-parser/src/parser/statement.js
Expand Up @@ -1906,6 +1906,10 @@ export default class StatementParser extends ExpressionParser {
if (this.eatContextual("from")) {
node.source = this.parseImportSource();
this.checkExport(node);
const assertions = this.maybeParseImportAssertions();
if (assertions) {
node.assertions = assertions;
}
} else {
if (expect) {
this.unexpected();
Expand All @@ -1914,11 +1918,6 @@ export default class StatementParser extends ExpressionParser {
}
}

const assertions = this.maybeParseImportAssertions();
if (assertions) {
node.assertions = assertions;
}

this.semicolon();
}

Expand Down
@@ -0,0 +1,2 @@
const foo = 1;
export { foo } assert { type: "json" };
@@ -0,0 +1,9 @@
{
"plugins": [
[
"importAssertions"
]
],
"sourceType": "module",
"throws": "Unexpected token, expected \";\" (2:15)"
Copy link
Member

Choose a reason for hiding this comment

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

Wonder if we should point out a missing from in the error message given assertions? (Might be better DX)

Copy link
Contributor Author

@JLHwung JLHwung Oct 27, 2020

Choose a reason for hiding this comment

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

I think it is okay here because when from is missing

export { someExport } asserts { type: "json" }

it would first throw

"someExport" is not defined

If users have already provided someExport, it is unlikely someExport could be exported from another module and thus they should consider remove the assertions.

}