Merge pull request #30862 from cameron-martin/fixed-babel-traverse-enter-exit-visitor

[@babel/traverse]: Fixed types of Visitor
This commit is contained in:
Mine Starks
2018-12-02 16:20:51 -08:00
committed by GitHub
2 changed files with 50 additions and 9 deletions
+43 -2
View File
@@ -4,10 +4,14 @@ import * as t from "@babel/types";
// Examples from: https://github.com/thejameskyle/babel-handbook/blob/master/translations/en/plugin-handbook.md
const MyVisitor: Visitor = {
Identifier: {
enter() {
enter(path) {
// $ExpectType NodePath<Identifier>
path;
console.log("Entered!");
},
exit() {
exit(path) {
// $ExpectType NodePath<Identifier>
path;
console.log("Exited!");
}
}
@@ -109,3 +113,40 @@ const BindingKindTest: Visitor = {
kind === 'anythingElse';
},
};
interface SomeVisitorState { someState: string; }
const VisitorStateTest: Visitor<SomeVisitorState> = {
enter(path, state) {
// $ExpectType SomeVisitorState
state;
// $ExpectType SomeVisitorState
this;
},
exit(path, state) {
// $ExpectType SomeVisitorState
state;
// $ExpectType SomeVisitorState
this;
},
Identifier(path, state) {
// $ExpectType SomeVisitorState
state;
// $ExpectType SomeVisitorState
this;
},
FunctionDeclaration: {
enter(path, state) {
// $ExpectType SomeVisitorState
state;
// $ExpectType SomeVisitorState
this;
},
exit(path, state) {
// $ExpectType SomeVisitorState
state;
// $ExpectType SomeVisitorState
this;
}
}
};
+7 -7
View File
@@ -143,17 +143,17 @@ export class Binding {
constantViolations: NodePath[];
}
export type Visitor<S = Node> = VisitNodeObject<Node> & {
[P in Node["type"]]?: VisitNode<S, Extract<Node, { type: P; }>>;
export type Visitor<S = {}> = VisitNodeObject<S, Node> & {
[Type in Node["type"]]?: VisitNode<S, Extract<Node, { type: Type; }>>;
};
export type VisitNode<T, P> = VisitNodeFunction<T, P> | VisitNodeObject<T>;
export type VisitNode<S, P> = VisitNodeFunction<S, P> | VisitNodeObject<S, P>;
export type VisitNodeFunction<T, P> = (this: T, path: NodePath<P>, state: any) => void;
export type VisitNodeFunction<S, P> = (this: S, path: NodePath<P>, state: S) => void;
export interface VisitNodeObject<T> {
enter?(path: NodePath<T>, state: any): void;
exit?(path: NodePath<T>, state: any): void;
export interface VisitNodeObject<S, P> {
enter?: VisitNodeFunction<S, P>;
exit?: VisitNodeFunction<S, P>;
}
export class NodePath<T = Node> {