Skip to content

Commit

Permalink
Improve the type definition of path.isX (#15661)
Browse files Browse the repository at this point in the history
* improve

* fix

* improve

* review
  • Loading branch information
liuxingbaoyu committed Jul 31, 2023
1 parent 74b5ac2 commit 078d59a
Show file tree
Hide file tree
Showing 8 changed files with 848 additions and 650 deletions.
9 changes: 8 additions & 1 deletion packages/babel-traverse/scripts/generators/asserts.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,13 +8,20 @@ export default function generateAsserts() {
import type * as t from "@babel/types";
import type NodePath from "../index";
type Opts<Obj> = Partial<{
[Prop in keyof Obj]: Obj[Prop] extends t.Node
? t.Node
: Obj[Prop] extends t.Node[]
? t.Node[]
: Obj[Prop];
}>;
export interface NodePathAssertions {`;

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

Expand Down
13 changes: 11 additions & 2 deletions packages/babel-traverse/scripts/generators/validators.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,18 +9,27 @@ import type * as t from "@babel/types";
import type NodePath from "../index";
import type { VirtualTypeNodePathValidators } from "../lib/virtual-types-validator";
type Opts<Obj> = Partial<{
[Prop in keyof Obj]: Obj[Prop] extends t.Node
? t.Node
: Obj[Prop] extends t.Node[]
? t.Node[]
: Obj[Prop];
}>;
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}>;`;
output += `is${type}<T extends t.Node>(this: NodePath<T>, opts?: Opts<t.${type}>): this is NodePath<T & t.${type}>;`;
}

output += `
}
export interface NodePathValidators
extends BaseNodePathValidators, VirtualTypeNodePathValidators {}
extends Omit<BaseNodePathValidators, keyof VirtualTypeNodePathValidators>,
VirtualTypeNodePathValidators {}
`;

return output;
Expand Down

0 comments on commit 078d59a

Please sign in to comment.