Fix import assertions type definitions (#12794)

* Fix import assertions type definitions

* Ensure that an array validator is followed by assertEach
This commit is contained in:
Nicolò Ribaudo 2021-02-12 10:10:05 +01:00 committed by GitHub
parent 4e2f8301dc
commit 7c9dd78b8d
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 32 additions and 14 deletions

View File

@ -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;
}
@ -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;
}
@ -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;
}

View File

@ -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: {

View File

@ -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>): 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;
}