Skip to content

Commit

Permalink
Add support for printing ImportAttribute
Browse files Browse the repository at this point in the history
  • Loading branch information
existentialism committed May 29, 2020
1 parent 8e41f26 commit 0eb8e6d
Show file tree
Hide file tree
Showing 8 changed files with 53 additions and 0 deletions.
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 from "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

0 comments on commit 0eb8e6d

Please sign in to comment.