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

Restructure virtual types validator #14799

Merged
merged 7 commits into from Jul 27, 2022
Merged
Show file tree
Hide file tree
Changes from 6 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
3 changes: 1 addition & 2 deletions Gulpfile.mjs
Expand Up @@ -142,7 +142,7 @@ async function generateTypeHelpers(helperKind, filename = "index.ts") {

/**
*
* @typedef {("asserts" | "validators" | "virtual-types")} TraverseHelperKind
* @typedef {("asserts" | "validators")} TraverseHelperKind
* @param {TraverseHelperKind} helperKind
*/
function generateTraverseHelpers(helperKind) {
Expand Down Expand Up @@ -525,7 +525,6 @@ gulp.task("generate-type-helpers", () => {
generateTypeHelpers("ast-types"),
generateTraverseHelpers("asserts"),
generateTraverseHelpers("validators"),
generateTraverseHelpers("virtual-types"),
liuxingbaoyu marked this conversation as resolved.
Show resolved Hide resolved
]);
});

Expand Down
34 changes: 7 additions & 27 deletions packages/babel-traverse/scripts/generators/validators.js
@@ -1,46 +1,26 @@
import * as t from "@babel/types";
import * as virtualTypes from "../../lib/path/lib/virtual-types.js";

export default function generateValidators() {
let output = `/*
* This file is auto-generated! Do not modify it directly.
* To re-generate run 'make build'
*/
import * as t from "@babel/types";
import NodePath from "../index";
import type { VirtualTypeAliases } from "./virtual-types";
import type * as t from "@babel/types";
import type NodePath from "../index";
import type { VirtualTypeNodePathValidators } from "../lib/virtual-types-validator";

export interface NodePathValidators {
interface BaseNodePathValidators {
`;

for (const type of [...t.TYPES].sort()) {
output += `is${type}<T extends t.Node>(this: NodePath<T>, opts?: object): this is NodePath<T & t.${type}>;`;
}

for (const type of Object.keys(virtualTypes)) {
// TODO: Remove this check once we stop compiling to CJS
if (type === "default" || type === "__esModule") continue;

const { types } = virtualTypes[type];
if (type[0] === "_") continue;
if (t.NODE_FIELDS[type] || t.FLIPPED_ALIAS_KEYS[type]) {
output += `is${type}<T extends t.Node>(this: NodePath<T>, opts?: object): this is NodePath<T & t.${type}>;`;
} else if (types /* in VirtualTypeAliases */) {
output += `is${type}<T extends t.Node>(this: NodePath<T>, opts?: object): this is NodePath<T & VirtualTypeAliases["${type}"]>;`;
} else if (type === "Pure") {
output += `isPure(constantsOnly?: boolean): boolean;`;
} 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;`;
}
}

output += `
}

export interface NodePathValidators
extends BaseNodePathValidators, VirtualTypeNodePathValidators {}
`;

return output;
Expand Down
27 changes: 0 additions & 27 deletions packages/babel-traverse/scripts/generators/virtual-types.js

This file was deleted.

69 changes: 8 additions & 61 deletions packages/babel-traverse/src/path/generated/validators.ts
Expand Up @@ -2,11 +2,11 @@
* This file is auto-generated! Do not modify it directly.
* To re-generate run 'make build'
*/
import * as t from "@babel/types";
import NodePath from "../index";
import type { VirtualTypeAliases } from "./virtual-types";
import type * as t from "@babel/types";
import type NodePath from "../index";
import type { VirtualTypeNodePathValidators } from "../lib/virtual-types-validator";

export interface NodePathValidators {
interface BaseNodePathValidators {
isAccessor<T extends t.Node>(
this: NodePath<T>,
opts?: object,
Expand Down Expand Up @@ -1211,61 +1211,8 @@ export interface NodePathValidators {
this: NodePath<T>,
opts?: object,
): this is NodePath<T & t.YieldExpression>;
isBindingIdentifier<T extends t.Node>(
this: NodePath<T>,
opts?: object,
): this is NodePath<T & VirtualTypeAliases["BindingIdentifier"]>;
isBlockScoped(opts?: object): boolean;
isExistentialTypeParam<T extends t.Node>(
this: NodePath<T>,
opts?: object,
): this is NodePath<T & VirtualTypeAliases["ExistentialTypeParam"]>;
isExpression<T extends t.Node>(
this: NodePath<T>,
opts?: object,
): this is NodePath<T & t.Expression>;
isFlow<T extends t.Node>(
this: NodePath<T>,
opts?: object,
): this is NodePath<T & t.Flow>;
isForAwaitStatement<T extends t.Node>(
this: NodePath<T>,
opts?: object,
): this is NodePath<T & VirtualTypeAliases["ForAwaitStatement"]>;
isGenerated(opts?: object): boolean;
isNumericLiteralTypeAnnotation<T extends t.Node>(
this: NodePath<T>,
opts?: object,
): this is NodePath<T & VirtualTypeAliases["NumericLiteralTypeAnnotation"]>;
isPure(constantsOnly?: boolean): boolean;
isReferenced(opts?: object): boolean;
isReferencedIdentifier<T extends t.Node>(
this: NodePath<T>,
opts?: object,
): this is NodePath<T & VirtualTypeAliases["ReferencedIdentifier"]>;
isReferencedMemberExpression<T extends t.Node>(
this: NodePath<T>,
opts?: object,
): this is NodePath<T & VirtualTypeAliases["ReferencedMemberExpression"]>;
isRestProperty<T extends t.Node>(
this: NodePath<T>,
opts?: object,
): this is NodePath<T & VirtualTypeAliases["RestProperty"]>;
isScope<T extends t.Node>(
this: NodePath<T>,
opts?: object,
): this is NodePath<T & VirtualTypeAliases["Scope"]>;
isSpreadProperty<T extends t.Node>(
this: NodePath<T>,
opts?: object,
): this is NodePath<T & VirtualTypeAliases["SpreadProperty"]>;
isStatement<T extends t.Node>(
this: NodePath<T>,
opts?: object,
): this is NodePath<T & t.Statement>;
isUser(opts?: object): boolean;
isVar<T extends t.Node>(
this: NodePath<T>,
opts?: object,
): this is NodePath<T & VirtualTypeAliases["Var"]>;
}

export interface NodePathValidators
extends BaseNodePathValidators,
VirtualTypeNodePathValidators {}
26 changes: 0 additions & 26 deletions packages/babel-traverse/src/path/generated/virtual-types.ts

This file was deleted.

13 changes: 5 additions & 8 deletions packages/babel-traverse/src/path/index.ts
Expand Up @@ -22,6 +22,7 @@ import * as NodePath_removal from "./removal";
import * as NodePath_modification from "./modification";
import * as NodePath_family from "./family";
import * as NodePath_comments from "./comments";
import * as NodePath_virtual_types_validator from "./lib/virtual-types-validator";
import type { NodePathAssetions } from "./generated/asserts";
import type { NodePathValidators } from "./generated/validators";

Expand Down Expand Up @@ -266,16 +267,12 @@ for (const type of t.TYPES) {
};
}

// Register virtual types validators after base types validators
Object.assign(NodePath.prototype, NodePath_virtual_types_validator);

for (const type of Object.keys(virtualTypes) as (keyof typeof virtualTypes)[]) {
if (type[0] === "_") continue;
if (t.TYPES.indexOf(type) < 0) t.TYPES.push(type);

const virtualType = virtualTypes[type];

NodePath.prototype[`is${type}`] = function (opts?: any) {
// @ts-expect-error checkPath will throw when type is ExistentialTypeParam/NumericLiteralTypeAnnotation
return virtualType.checkPath(this, opts);
};
if (!t.TYPES.includes(type)) t.TYPES.push(type);
}

type NodePathMixins = typeof NodePath_ancestry &
Expand Down