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

Support Import Assertions for re-export statement #12249

Merged
merged 8 commits into from Oct 26, 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
15 changes: 4 additions & 11 deletions packages/babel-generator/src/generators/modules.js
Expand Up @@ -53,6 +53,7 @@ export function ExportAllDeclaration(node: Object) {
this.word("from");
this.space();
this.print(node.source, node);
this.printAssertions(node);
this.semicolon();
}

Expand Down Expand Up @@ -131,6 +132,7 @@ function ExportDeclaration(node: Object) {
this.word("from");
this.space();
this.print(node.source, node);
this.printAssertions(node);
}

this.semicolon();
Expand Down Expand Up @@ -180,19 +182,10 @@ export function ImportDeclaration(node: Object) {

this.print(node.source, node);

if (node.assertions?.length) {
this.space();
this.word("assert");
this.space();
this.token("{");
this.space();
this.printList(node.assertions, node);
this.space();
this.token("}");
}
this.printAssertions(node);
// todo(Babel 8): remove this if branch
// `module-attributes` support is discontinued, use `import-assertions` instead.
else if (node.attributes?.length) {
if (node.attributes?.length) {
this.space();
this.word("with");
this.space();
Expand Down
13 changes: 13 additions & 0 deletions packages/babel-generator/src/printer.js
Expand Up @@ -646,6 +646,19 @@ export default class Printer {
}
}
}

printAssertions(node: Node) {
if (node.assertions?.length) {
this.space();
this.word("assert");
this.space();
this.token("{");
this.space();
this.printList(node.assertions, node);
this.space();
this.token("}");
}
}
}

// Expose the node type functions and helpers on the prototype for easy usage.
Expand Down
@@ -1 +1,4 @@
import foo from "foo.json" assert { type: "json" };
import foo1 from "foo.json" assert { type: "json" };
export { foo2 } from "foo.json" assert { type: "json" };
export * from "foo.json" assert { type: "json" };
export * as foo3 from "foo.json" assert { type: "json" };
@@ -1 +1,4 @@
import foo from "foo.json" assert { type: "json" };
import foo1 from "foo.json" assert { type: "json" };
export { foo2 } from "foo.json" assert { type: "json" };
export * from "foo.json" assert { type: "json" };
export * as foo3 from "foo.json" assert { type: "json" };
1 change: 1 addition & 0 deletions packages/babel-parser/ast/spec.md
Expand Up @@ -1364,6 +1364,7 @@ interface ExportNamedDeclaration <: ModuleDeclaration {
declaration: Declaration | null;
specifiers: [ ExportSpecifier ];
source: StringLiteral | null;
assertions?: [ ImportAttribute ];
}
```

Expand Down
5 changes: 5 additions & 0 deletions packages/babel-parser/src/parser/statement.js
Expand Up @@ -1924,6 +1924,11 @@ export default class StatementParser extends ExpressionParser {
}
}

const assertions = this.maybeParseImportAssertions();
if (assertions) {
node.assertions = assertions;
Copy link
Contributor

Choose a reason for hiding this comment

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

Please add type information to packages/babel-parser/src/types.js.

Copy link
Member Author

Choose a reason for hiding this comment

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

fa83ccb 👍

}

this.semicolon();
}

Expand Down
8 changes: 8 additions & 0 deletions packages/babel-parser/src/types.js
Expand Up @@ -360,6 +360,12 @@ export type Directive = NodeBase & {

export type DirectiveLiteral = StringLiteral & { type: "DirectiveLiteral" };

export type ImportAttribute = NodeBase & {
type: "ImportAttribute",
key: Identifier | StringLiteral,
value: StringLiteral,
};

// Expressions

export type Super = NodeBase & { type: "Super" };
Expand Down Expand Up @@ -867,6 +873,8 @@ export type ExportNamedDeclaration = NodeBase & {
source: ?Literal,

exportKind?: "type" | "value", // TODO: Not in spec

assertions?: $ReadOnlyArray<ImportAttribute>,
};

export type ExportSpecifier = NodeBase & {
Expand Down
@@ -0,0 +1,2 @@
export { x } from "foo" assert
{ type: "json" }
@@ -0,0 +1,63 @@
{
"type": "File",
"start":0,"end":47,"loc":{"start":{"line":1,"column":0},"end":{"line":2,"column":16}},
"program": {
"type": "Program",
"start":0,"end":47,"loc":{"start":{"line":1,"column":0},"end":{"line":2,"column":16}},
"sourceType": "module",
"interpreter": null,
"body": [
{
"type": "ExportNamedDeclaration",
"start":0,"end":47,"loc":{"start":{"line":1,"column":0},"end":{"line":2,"column":16}},
"specifiers": [
{
"type": "ExportSpecifier",
"start":9,"end":10,"loc":{"start":{"line":1,"column":9},"end":{"line":1,"column":10}},
"local": {
"type": "Identifier",
"start":9,"end":10,"loc":{"start":{"line":1,"column":9},"end":{"line":1,"column":10},"identifierName":"x"},
"name": "x"
},
"exported": {
"type": "Identifier",
"start":9,"end":10,"loc":{"start":{"line":1,"column":9},"end":{"line":1,"column":10},"identifierName":"x"},
"name": "x"
}
}
],
"source": {
"type": "StringLiteral",
"start":18,"end":23,"loc":{"start":{"line":1,"column":18},"end":{"line":1,"column":23}},
"extra": {
"rawValue": "foo",
"raw": "\"foo\""
},
"value": "foo"
},
"declaration": null,
"assertions": [
{
"type": "ImportAttribute",
"start":33,"end":45,"loc":{"start":{"line":2,"column":2},"end":{"line":2,"column":14}},
"key": {
"type": "Identifier",
"start":33,"end":37,"loc":{"start":{"line":2,"column":2},"end":{"line":2,"column":6},"identifierName":"type"},
"name": "type"
},
"value": {
"type": "StringLiteral",
"start":39,"end":45,"loc":{"start":{"line":2,"column":8},"end":{"line":2,"column":14}},
"extra": {
"rawValue": "json",
"raw": "\"json\""
},
"value": "json"
}
}
]
}
],
"directives": []
}
}
@@ -0,0 +1 @@
export { x } from "foo" assert;
@@ -0,0 +1,5 @@
{
"plugins": [["importAssertions"]],
"sourceType": "module",
"throws": "Unexpected token (1:30)"
}
@@ -1 +1,2 @@
import foo from "foo.json" assert { for: "for" }
export { foo } from "foo.json" assert { for: "for" }
@@ -1,12 +1,13 @@
{
"type": "File",
"start":0,"end":48,"loc":{"start":{"line":1,"column":0},"end":{"line":1,"column":48}},
"start":0,"end":101,"loc":{"start":{"line":1,"column":0},"end":{"line":2,"column":52}},
"errors": [
"SyntaxError: The only accepted module attribute is `type` (1:36)"
"SyntaxError: The only accepted module attribute is `type` (1:36)",
"SyntaxError: The only accepted module attribute is `type` (2:40)"
],
"program": {
"type": "Program",
"start":0,"end":48,"loc":{"start":{"line":1,"column":0},"end":{"line":1,"column":48}},
"start":0,"end":101,"loc":{"start":{"line":1,"column":0},"end":{"line":2,"column":52}},
"sourceType": "module",
"interpreter": null,
"body": [
Expand Down Expand Up @@ -53,6 +54,56 @@
}
}
]
},
{
"type": "ExportNamedDeclaration",
"start":49,"end":101,"loc":{"start":{"line":2,"column":0},"end":{"line":2,"column":52}},
"specifiers": [
{
"type": "ExportSpecifier",
"start":58,"end":61,"loc":{"start":{"line":2,"column":9},"end":{"line":2,"column":12}},
"local": {
"type": "Identifier",
"start":58,"end":61,"loc":{"start":{"line":2,"column":9},"end":{"line":2,"column":12},"identifierName":"foo"},
"name": "foo"
},
"exported": {
"type": "Identifier",
"start":58,"end":61,"loc":{"start":{"line":2,"column":9},"end":{"line":2,"column":12},"identifierName":"foo"},
"name": "foo"
}
}
],
"source": {
"type": "StringLiteral",
"start":69,"end":79,"loc":{"start":{"line":2,"column":20},"end":{"line":2,"column":30}},
"extra": {
"rawValue": "foo.json",
"raw": "\"foo.json\""
},
"value": "foo.json"
},
"declaration": null,
"assertions": [
{
"type": "ImportAttribute",
"start":89,"end":99,"loc":{"start":{"line":2,"column":40},"end":{"line":2,"column":50}},
"key": {
"type": "Identifier",
"start":89,"end":92,"loc":{"start":{"line":2,"column":40},"end":{"line":2,"column":43},"identifierName":"for"},
"name": "for"
},
"value": {
"type": "StringLiteral",
"start":94,"end":99,"loc":{"start":{"line":2,"column":45},"end":{"line":2,"column":50}},
"extra": {
"rawValue": "for",
"raw": "\"for\""
},
"value": "for"
}
}
]
}
],
"directives": []
Expand Down
@@ -1 +1,2 @@
import foo from "foo" assert { type: "json", }
export { foo } from "foo" assert { type: "json", }
@@ -1,9 +1,9 @@
{
"type": "File",
"start":0,"end":46,"loc":{"start":{"line":1,"column":0},"end":{"line":1,"column":46}},
"start":0,"end":97,"loc":{"start":{"line":1,"column":0},"end":{"line":2,"column":50}},
"program": {
"type": "Program",
"start":0,"end":46,"loc":{"start":{"line":1,"column":0},"end":{"line":1,"column":46}},
"start":0,"end":97,"loc":{"start":{"line":1,"column":0},"end":{"line":2,"column":50}},
"sourceType": "module",
"interpreter": null,
"body": [
Expand Down Expand Up @@ -50,6 +50,56 @@
}
}
]
},
{
"type": "ExportNamedDeclaration",
"start":47,"end":97,"loc":{"start":{"line":2,"column":0},"end":{"line":2,"column":50}},
"specifiers": [
{
"type": "ExportSpecifier",
"start":56,"end":59,"loc":{"start":{"line":2,"column":9},"end":{"line":2,"column":12}},
"local": {
"type": "Identifier",
"start":56,"end":59,"loc":{"start":{"line":2,"column":9},"end":{"line":2,"column":12},"identifierName":"foo"},
"name": "foo"
},
"exported": {
"type": "Identifier",
"start":56,"end":59,"loc":{"start":{"line":2,"column":9},"end":{"line":2,"column":12},"identifierName":"foo"},
"name": "foo"
}
}
],
"source": {
"type": "StringLiteral",
"start":67,"end":72,"loc":{"start":{"line":2,"column":20},"end":{"line":2,"column":25}},
"extra": {
"rawValue": "foo",
"raw": "\"foo\""
},
"value": "foo"
},
"declaration": null,
"assertions": [
{
"type": "ImportAttribute",
"start":82,"end":94,"loc":{"start":{"line":2,"column":35},"end":{"line":2,"column":47}},
"key": {
"type": "Identifier",
"start":82,"end":86,"loc":{"start":{"line":2,"column":35},"end":{"line":2,"column":39},"identifierName":"type"},
"name": "type"
},
"value": {
"type": "StringLiteral",
"start":88,"end":94,"loc":{"start":{"line":2,"column":41},"end":{"line":2,"column":47}},
"extra": {
"rawValue": "json",
"raw": "\"json\""
},
"value": "json"
}
}
]
}
],
"directives": []
Expand Down
@@ -0,0 +1 @@
export * as foo from "foo.json" assert { type: "json" };
@@ -0,0 +1,57 @@
{
"type": "File",
"start":0,"end":56,"loc":{"start":{"line":1,"column":0},"end":{"line":1,"column":56}},
"program": {
"type": "Program",
"start":0,"end":56,"loc":{"start":{"line":1,"column":0},"end":{"line":1,"column":56}},
"sourceType": "module",
"interpreter": null,
"body": [
{
"type": "ExportNamedDeclaration",
"start":0,"end":56,"loc":{"start":{"line":1,"column":0},"end":{"line":1,"column":56}},
"specifiers": [
{
"type": "ExportNamespaceSpecifier",
"start":7,"end":15,"loc":{"start":{"line":1,"column":7},"end":{"line":1,"column":15}},
"exported": {
"type": "Identifier",
"start":12,"end":15,"loc":{"start":{"line":1,"column":12},"end":{"line":1,"column":15},"identifierName":"foo"},
"name": "foo"
}
}
],
"source": {
"type": "StringLiteral",
"start":21,"end":31,"loc":{"start":{"line":1,"column":21},"end":{"line":1,"column":31}},
"extra": {
"rawValue": "foo.json",
"raw": "\"foo.json\""
},
"value": "foo.json"
},
"assertions": [
{
"type": "ImportAttribute",
"start":41,"end":53,"loc":{"start":{"line":1,"column":41},"end":{"line":1,"column":53}},
"key": {
"type": "Identifier",
"start":41,"end":45,"loc":{"start":{"line":1,"column":41},"end":{"line":1,"column":45},"identifierName":"type"},
"name": "type"
},
"value": {
"type": "StringLiteral",
"start":47,"end":53,"loc":{"start":{"line":1,"column":47},"end":{"line":1,"column":53}},
"extra": {
"rawValue": "json",
"raw": "\"json\""
},
"value": "json"
}
}
]
}
],
"directives": []
}
}