feat(traverse): specific return type for virtual types' validators (#13578)

* feat(traverse): specific return type for virtual types' validators

update generator script to use virtualType.types[0] as it's type

fix #13576

* fix: remove unused ts-expect-error

so glad that it works

* feat: use VirtualTypeAliases to generate validators for virtual types

* fix: return boolean when it doesn't have any alias

* fix: use type only import and simplify condition
This commit is contained in:
王清雨
2021-07-27 22:02:09 +08:00
committed by GitHub
parent 790c5180d4
commit e0dc925bbe
4 changed files with 36 additions and 15 deletions

View File

@@ -9,6 +9,7 @@ export default function generateValidators() {
*/
import * as t from "@babel/types";
import NodePath from "../index";
import type { VirtualTypeAliases } from "./virtual-types";
export interface NodePathValidators {
`;
@@ -18,10 +19,18 @@ export interface NodePathValidators {
}
for (const type of Object.keys(virtualTypes)) {
const { types } = virtualTypes[type];
if (type[0] === "_") continue;
if (definitions.NODE_FIELDS[type] || definitions.FLIPPED_ALIAS_KEYS[type]) {
output += `is${type}(opts?: object): this is NodePath<t.${type}>;`;
} else if (types /* in VirtualTypeAliases */) {
output += `is${type}(opts?: object): this is NodePath<VirtualTypeAliases["${type}"]>;`;
} else {
// if it don't have types, then VirtualTypeAliases[type] is t.Node
// which TS marked as always true
// eg. if (path.isBlockScope()) return;
// path resolved to `never` here
// so we have to return boolean instead of this is NodePath<t.Node> here
output += `is${type}(opts?: object): boolean;`;
}
}