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

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

Merged
merged 2 commits into from Oct 27, 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
33 changes: 33 additions & 0 deletions packages/babel-template/test/index.js
Expand Up @@ -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(
`
Expand Down
12 changes: 12 additions & 0 deletions packages/babel-types/src/definitions/core.js
Expand Up @@ -1464,6 +1464,12 @@ defineType("ExportNamedDeclaration", {
},
),
},
assertions: {
validate: chain(
assertValueType("array"),
assertNodeType("ImportAttribute"),
),
},
specifiers: {
default: [],
validate: chain(
Expand Down Expand Up @@ -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"),
Expand Down