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 import assertions type definitions #12794

Merged
merged 2 commits into from Feb 12, 2021
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
6 changes: 3 additions & 3 deletions packages/babel-types/src/ast-types/generated/index.ts
Expand Up @@ -820,7 +820,7 @@ export interface ClassDeclaration extends BaseNode {
export interface ExportAllDeclaration extends BaseNode {
type: "ExportAllDeclaration";
source: StringLiteral;
assertions?: ImportAttribute | null;
assertions?: Array<ImportAttribute> | null;
exportKind?: "type" | "value" | null;
}

Expand All @@ -840,7 +840,7 @@ export interface ExportNamedDeclaration extends BaseNode {
ExportSpecifier | ExportDefaultSpecifier | ExportNamespaceSpecifier
>;
source?: StringLiteral | null;
assertions?: ImportAttribute | null;
assertions?: Array<ImportAttribute> | null;
exportKind?: "type" | "value" | null;
}

Expand All @@ -864,7 +864,7 @@ export interface ImportDeclaration extends BaseNode {
ImportSpecifier | ImportDefaultSpecifier | ImportNamespaceSpecifier
>;
source: StringLiteral;
assertions?: ImportAttribute | null;
assertions?: Array<ImportAttribute> | null;
importKind?: "type" | "typeof" | "value" | null;
}

Expand Down
6 changes: 3 additions & 3 deletions packages/babel-types/src/definitions/core.ts
Expand Up @@ -1404,7 +1404,7 @@ defineType("ExportAllDeclaration", {
optional: true,
validate: chain(
assertValueType("array"),
assertNodeType("ImportAttribute"),
assertEach(assertNodeType("ImportAttribute")),
),
},
},
Expand Down Expand Up @@ -1474,7 +1474,7 @@ defineType("ExportNamedDeclaration", {
optional: true,
validate: chain(
assertValueType("array"),
assertNodeType("ImportAttribute"),
assertEach(assertNodeType("ImportAttribute")),
),
},
specifiers: {
Expand Down Expand Up @@ -1576,7 +1576,7 @@ defineType("ImportDeclaration", {
optional: true,
validate: chain(
assertValueType("array"),
assertNodeType("ImportAttribute"),
assertEach(assertNodeType("ImportAttribute")),
),
},
specifiers: {
Expand Down
34 changes: 26 additions & 8 deletions packages/babel-types/src/definitions/utils.ts
Expand Up @@ -19,12 +19,18 @@ function getType(val) {
}
}

// TODO: Import and use Node instead of any
type Validator = { chainOf?: Validator[] } & ((
parent: any,
key: string,
node: any,
) => void);
type Validator = (
| { type: string }
| { each: Validator }
| { chainOf: Validator[] }
| { oneOf: any[] }
| { oneOfNodeTypes: string[] }
| { oneOfNodeOrValueTypes: string[] }
| { shapeOf: { [x: string]: FieldOptions } }
| {}
) &
// TODO: Import and use Node instead of any
((parent: any, key: string, node: any) => void);

type FieldOptions = {
default?: any;
Expand Down Expand Up @@ -218,12 +224,24 @@ export function assertOptionalChainStart(): Validator {
}

export function chain(...fns: Array<Validator>): Validator {
const validate: Validator = function (...args) {
function validate(...args: Parameters<Validator>) {
for (const fn of fns) {
fn(...args);
}
};
}
validate.chainOf = fns;

if (
fns.length >= 2 &&
"type" in fns[0] &&
fns[0].type === "array" &&
!("each" in fns[1])
) {
throw new Error(
`An assertValueType("array") validator can only be followed by an assertEach(...) validator.`,
);
}

return validate;
}

Expand Down