diff --git a/packages/babel-template/test/index.js b/packages/babel-template/test/index.js index 117254bc6fb4..4528c2c2b8de 100644 --- a/packages/babel-template/test/index.js +++ b/packages/babel-template/test/index.js @@ -215,6 +215,39 @@ describe("@babel/template", function () { expect(result.test.left).toBe(value); }); + it("should return assertions in ImportDeclaration 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 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 51be45bfd766..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( @@ -1559,6 +1565,12 @@ defineType("ImportDeclaration", { visitor: ["specifiers", "source"], aliases: ["Statement", "Declaration", "ModuleDeclaration"], fields: { + assertions: { + validate: chain( + assertValueType("array"), + assertNodeType("ImportAttribute"), + ), + }, specifiers: { validate: chain( assertValueType("array"),