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

Add support for printing ImportAttribute #11641

Merged
merged 1 commit into from May 29, 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: 15 additions & 0 deletions packages/babel-generator/src/generators/modules.js
Expand Up @@ -179,9 +179,24 @@ export function ImportDeclaration(node: Object) {
}

this.print(node.source, node);

if (node.attributes?.length) {
this.space();
this.word("with");
this.space();
this.printList(node.attributes, node);
}

this.semicolon();
}

export function ImportAttribute(node: Object) {
this.print(node.key);
this.token(":");
this.space();
this.print(node.value);
}

export function ImportNamespaceSpecifier(node: Object) {
this.token("*");
this.space();
Expand Down
@@ -0,0 +1 @@
import("foo.json", { with: { type: "json" } },);
11 changes: 11 additions & 0 deletions packages/babel-generator/test/fixtures/types/Import2/options.json
@@ -0,0 +1,11 @@
{
"plugins": [
[
"moduleAttributes",
{
"version": "may-2020"
}
]
],
"sourceType": "module"
}
@@ -0,0 +1,5 @@
import("foo.json", {
with: {
type: "json"
}
});
@@ -0,0 +1 @@
import foo from "foo.json" with type: "json";
Copy link
Contributor

Choose a reason for hiding this comment

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

Can you also add a test case for dynamic import? Though currently it should be supported as well.

import("foo.json", { with: { type: "json" } });

@@ -0,0 +1,11 @@
{
"plugins": [
[
"moduleAttributes",
{
"version": "may-2020"
}
]
],
"sourceType": "module"
}
@@ -0,0 +1 @@
import foo from "foo.json" with type: "json";
3 changes: 3 additions & 0 deletions packages/babel-types/src/asserts/generated/index.js
Expand Up @@ -767,6 +767,9 @@ export function assertClassPrivateMethod(
export function assertImport(node: Object, opts?: Object = {}): void {
assert("Import", node, opts);
}
export function assertImportAttribute(node: Object, opts?: Object = {}): void {
assert("ImportAttribute", node, opts);
}
export function assertDecorator(node: Object, opts?: Object = {}): void {
assert("Decorator", node, opts);
}
Expand Down
4 changes: 4 additions & 0 deletions packages/babel-types/src/builders/generated/index.js
Expand Up @@ -692,6 +692,10 @@ export function Import(...args: Array<any>): Object {
return builder("Import", ...args);
}
export { Import as import };
export function ImportAttribute(...args: Array<any>): Object {
return builder("ImportAttribute", ...args);
}
export { ImportAttribute as importAttribute };
export function Decorator(...args: Array<any>): Object {
return builder("Decorator", ...args);
}
Expand Down
4 changes: 4 additions & 0 deletions packages/babel-types/src/definitions/experimental.js
Expand Up @@ -224,6 +224,10 @@ defineType("Import", {
aliases: ["Expression"],
});

defineType("ImportAttribute", {
visitor: ["key", "value"],
});

defineType("Decorator", {
visitor: ["expression"],
fields: {
Expand Down
14 changes: 14 additions & 0 deletions packages/babel-types/src/validators/generated/index.js
Expand Up @@ -2441,6 +2441,20 @@ export function isImport(node: ?Object, opts?: Object): boolean {

return false;
}
export function isImportAttribute(node: ?Object, opts?: Object): boolean {
if (!node) return false;

const nodeType = node.type;
if (nodeType === "ImportAttribute") {
if (typeof opts === "undefined") {
return true;
} else {
return shallowEqual(node, opts);
}
}

return false;
}
export function isDecorator(node: ?Object, opts?: Object): boolean {
if (!node) return false;

Expand Down