diff --git a/packages/babel-types/src/ast-types/generated/index.ts b/packages/babel-types/src/ast-types/generated/index.ts index ae461bbae0..ced0274bc5 100755 --- a/packages/babel-types/src/ast-types/generated/index.ts +++ b/packages/babel-types/src/ast-types/generated/index.ts @@ -820,7 +820,7 @@ export interface ClassDeclaration extends BaseNode { export interface ExportAllDeclaration extends BaseNode { type: "ExportAllDeclaration"; source: StringLiteral; - assertions?: ImportAttribute | null; + assertions?: Array | null; exportKind?: "type" | "value" | null; } @@ -840,7 +840,7 @@ export interface ExportNamedDeclaration extends BaseNode { ExportSpecifier | ExportDefaultSpecifier | ExportNamespaceSpecifier >; source?: StringLiteral | null; - assertions?: ImportAttribute | null; + assertions?: Array | null; exportKind?: "type" | "value" | null; } @@ -864,7 +864,7 @@ export interface ImportDeclaration extends BaseNode { ImportSpecifier | ImportDefaultSpecifier | ImportNamespaceSpecifier >; source: StringLiteral; - assertions?: ImportAttribute | null; + assertions?: Array | null; importKind?: "type" | "typeof" | "value" | null; } diff --git a/packages/babel-types/src/definitions/core.ts b/packages/babel-types/src/definitions/core.ts index 744c266b68..fdede9c2e6 100644 --- a/packages/babel-types/src/definitions/core.ts +++ b/packages/babel-types/src/definitions/core.ts @@ -1404,7 +1404,7 @@ defineType("ExportAllDeclaration", { optional: true, validate: chain( assertValueType("array"), - assertNodeType("ImportAttribute"), + assertEach(assertNodeType("ImportAttribute")), ), }, }, @@ -1474,7 +1474,7 @@ defineType("ExportNamedDeclaration", { optional: true, validate: chain( assertValueType("array"), - assertNodeType("ImportAttribute"), + assertEach(assertNodeType("ImportAttribute")), ), }, specifiers: { @@ -1576,7 +1576,7 @@ defineType("ImportDeclaration", { optional: true, validate: chain( assertValueType("array"), - assertNodeType("ImportAttribute"), + assertEach(assertNodeType("ImportAttribute")), ), }, specifiers: { diff --git a/packages/babel-types/src/definitions/utils.ts b/packages/babel-types/src/definitions/utils.ts index be5390813a..92ba7b32c3 100644 --- a/packages/babel-types/src/definitions/utils.ts +++ b/packages/babel-types/src/definitions/utils.ts @@ -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; @@ -218,12 +224,24 @@ export function assertOptionalChainStart(): Validator { } export function chain(...fns: Array): Validator { - const validate: Validator = function (...args) { + function validate(...args: Parameters) { 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; }