From d3529beb10bd1eb49f9e70dd4288df4383d676be Mon Sep 17 00:00:00 2001 From: coderaiser Date: Tue, 27 Oct 2020 11:06:57 +0200 Subject: [PATCH 1/2] fix: babel-types: ImportDeclaration: add assertions (#12262) --- packages/babel-template/test/index.js | 11 +++++++++++ packages/babel-types/src/definitions/core.js | 6 ++++++ 2 files changed, 17 insertions(+) diff --git a/packages/babel-template/test/index.js b/packages/babel-template/test/index.js index 117254bc6fb4..7eae1c221940 100644 --- a/packages/babel-template/test/index.js +++ b/packages/babel-template/test/index.js @@ -215,6 +215,17 @@ describe("@babel/template", function () { expect(result.test.left).toBe(value); }); + it("should return assertions when using .ast", () => { + const result = template.ast( + `import json from "./foo.json" assert { type: "json" };`, + { + plugins: ["importAssertions"], + }, + ); + + expect(result.assertions[0].type).toBe("ImportAttribute"); + }); + it("should replace JSX placeholder", () => { const result = template.expression( ` diff --git a/packages/babel-types/src/definitions/core.js b/packages/babel-types/src/definitions/core.js index 51be45bfd766..ccfda45a0a6f 100644 --- a/packages/babel-types/src/definitions/core.js +++ b/packages/babel-types/src/definitions/core.js @@ -1559,6 +1559,12 @@ defineType("ImportDeclaration", { visitor: ["specifiers", "source"], aliases: ["Statement", "Declaration", "ModuleDeclaration"], fields: { + assertions: { + validate: chain( + assertValueType("array"), + assertNodeType("ImportAttribute"), + ), + }, specifiers: { validate: chain( assertValueType("array"), From c2918df0aa0ccb9a7955f1a8e665bff5c736034d Mon Sep 17 00:00:00 2001 From: coderaiser Date: Tue, 27 Oct 2020 15:23:14 +0200 Subject: [PATCH 2/2] fix: babel-types: ExportDeclaration: add assertion --- packages/babel-template/test/index.js | 24 +++++++++++++++++++- packages/babel-types/src/definitions/core.js | 6 +++++ 2 files changed, 29 insertions(+), 1 deletion(-) diff --git a/packages/babel-template/test/index.js b/packages/babel-template/test/index.js index 7eae1c221940..4528c2c2b8de 100644 --- a/packages/babel-template/test/index.js +++ b/packages/babel-template/test/index.js @@ -215,7 +215,7 @@ describe("@babel/template", function () { expect(result.test.left).toBe(value); }); - it("should return assertions when using .ast", () => { + it("should return assertions in ImportDeclaration when using .ast", () => { const result = template.ast( `import json from "./foo.json" assert { type: "json" };`, { @@ -226,6 +226,28 @@ describe("@babel/template", function () { expect(result.assertions[0].type).toBe("ImportAttribute"); }); + it("should return assertions in ExportNamedDeclaration when using .ast", () => { + const result = template.ast( + `export { foo2 } from "foo.json" assert { type: "json" };`, + { + plugins: ["importAssertions"], + }, + ); + + expect(result.assertions[0].type).toBe("ImportAttribute"); + }); + + it("should return assertions in ExportDefaultDeclaration when using .ast", () => { + const result = template.ast( + `export foo2 from "foo.json" assert { type: "json" };`, + { + plugins: ["importAssertions", "exportDefaultFrom"], + }, + ); + + expect(result.assertions[0].type).toBe("ImportAttribute"); + }); + it("should replace JSX placeholder", () => { const result = template.expression( ` diff --git a/packages/babel-types/src/definitions/core.js b/packages/babel-types/src/definitions/core.js index ccfda45a0a6f..7aafa8801aeb 100644 --- a/packages/babel-types/src/definitions/core.js +++ b/packages/babel-types/src/definitions/core.js @@ -1464,6 +1464,12 @@ defineType("ExportNamedDeclaration", { }, ), }, + assertions: { + validate: chain( + assertValueType("array"), + assertNodeType("ImportAttribute"), + ), + }, specifiers: { default: [], validate: chain(