fix: babel-types: ImportDeclaration: add assertions (#12263)

This commit is contained in:
coderaiser 2020-10-27 15:58:25 +02:00 committed by GitHub
parent df908fc63b
commit 9eb661b285
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 45 additions and 0 deletions

View File

@ -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(
`

View File

@ -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"),