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 { export interface ExportAllDeclaration extends BaseNode {
type: "ExportAllDeclaration"; type: "ExportAllDeclaration";
source: StringLiteral; source: StringLiteral;
assertions?: ImportAttribute | null; assertions?: Array<ImportAttribute> | null;
exportKind?: "type" | "value" | null; exportKind?: "type" | "value" | null;
} }
@ -840,7 +840,7 @@ export interface ExportNamedDeclaration extends BaseNode {
ExportSpecifier | ExportDefaultSpecifier | ExportNamespaceSpecifier ExportSpecifier | ExportDefaultSpecifier | ExportNamespaceSpecifier
>; >;
source?: StringLiteral | null; source?: StringLiteral | null;
assertions?: ImportAttribute | null; assertions?: Array<ImportAttribute> | null;
exportKind?: "type" | "value" | null; exportKind?: "type" | "value" | null;
} }
@ -864,7 +864,7 @@ export interface ImportDeclaration extends BaseNode {
ImportSpecifier | ImportDefaultSpecifier | ImportNamespaceSpecifier ImportSpecifier | ImportDefaultSpecifier | ImportNamespaceSpecifier
>; >;
source: StringLiteral; source: StringLiteral;
assertions?: ImportAttribute | null; assertions?: Array<ImportAttribute> | null;
importKind?: "type" | "typeof" | "value" | null; importKind?: "type" | "typeof" | "value" | null;
} }

View File

@ -1404,7 +1404,7 @@ defineType("ExportAllDeclaration", {
optional: true, optional: true,
validate: chain( validate: chain(
assertValueType("array"), assertValueType("array"),
assertNodeType("ImportAttribute"), assertEach(assertNodeType("ImportAttribute")),
), ),
}, },
}, },
@ -1474,7 +1474,7 @@ defineType("ExportNamedDeclaration", {
optional: true, optional: true,
validate: chain( validate: chain(
assertValueType("array"), assertValueType("array"),
assertNodeType("ImportAttribute"), assertEach(assertNodeType("ImportAttribute")),
), ),
}, },
specifiers: { specifiers: {
@ -1576,7 +1576,7 @@ defineType("ImportDeclaration", {
optional: true, optional: true,
validate: chain( validate: chain(
assertValueType("array"), assertValueType("array"),
assertNodeType("ImportAttribute"), assertEach(assertNodeType("ImportAttribute")),
), ),
}, },
specifiers: { specifiers: {

View File

@ -19,12 +19,18 @@ function getType(val) {
} }
} }
// TODO: Import and use Node instead of any type Validator = (
type Validator = { chainOf?: Validator[] } & (( | { type: string }
parent: any, | { each: Validator }
key: string, | { chainOf: Validator[] }
node: any, | { oneOf: any[] }
) => void); | { 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 = { type FieldOptions = {
default?: any; default?: any;
@ -218,12 +224,24 @@ export function assertOptionalChainStart(): Validator {
} }
export function chain(...fns: Array<Validator>): Validator { export function chain(...fns: Array<Validator>): Validator {
const validate: Validator = function (...args) { function validate(...args: Parameters<Validator>) {
for (const fn of fns) { for (const fn of fns) {
fn(...args); fn(...args);
} }
}; }
validate.chainOf = fns; 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; return validate;
} }