From 289c533aadb4ee5d6a7ef2a740c4d38cd7b6cc59 Mon Sep 17 00:00:00 2001 From: Cameron Martin Date: Tue, 27 Nov 2018 17:27:21 +0000 Subject: [PATCH 1/2] [@babel/traverse]: Fixed type of path when using enter & exit visitor. --- types/babel__traverse/babel__traverse-tests.ts | 8 ++++++-- types/babel__traverse/index.d.ts | 2 +- 2 files changed, 7 insertions(+), 3 deletions(-) diff --git a/types/babel__traverse/babel__traverse-tests.ts b/types/babel__traverse/babel__traverse-tests.ts index 5b4fbbef5f..974831dbca 100644 --- a/types/babel__traverse/babel__traverse-tests.ts +++ b/types/babel__traverse/babel__traverse-tests.ts @@ -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 + path; console.log("Entered!"); }, - exit() { + exit(path) { + // $ExpectType NodePath + path; console.log("Exited!"); } } diff --git a/types/babel__traverse/index.d.ts b/types/babel__traverse/index.d.ts index 465a60464f..740f5f4429 100644 --- a/types/babel__traverse/index.d.ts +++ b/types/babel__traverse/index.d.ts @@ -142,7 +142,7 @@ export type Visitor = VisitNodeObject & { [P in Node["type"]]?: VisitNode>; }; -export type VisitNode = VisitNodeFunction | VisitNodeObject; +export type VisitNode = VisitNodeFunction | VisitNodeObject

; export type VisitNodeFunction = (this: T, path: NodePath

, state: any) => void; From 0226045de5e8db79594af54dbab3f5b77b972d3c Mon Sep 17 00:00:00 2001 From: Cameron Martin Date: Tue, 27 Nov 2018 20:42:30 +0000 Subject: [PATCH 2/2] Implemented all changes outlined in the comments in pull request #30862. --- .../babel__traverse/babel__traverse-tests.ts | 37 +++++++++++++++++++ types/babel__traverse/index.d.ts | 14 +++---- 2 files changed, 44 insertions(+), 7 deletions(-) diff --git a/types/babel__traverse/babel__traverse-tests.ts b/types/babel__traverse/babel__traverse-tests.ts index 974831dbca..772b392b63 100644 --- a/types/babel__traverse/babel__traverse-tests.ts +++ b/types/babel__traverse/babel__traverse-tests.ts @@ -112,3 +112,40 @@ const BindingKindTest: Visitor = { // kind === 'anythingElse'; }, }; + +interface SomeVisitorState { someState: string; } + +const VisitorStateTest: Visitor = { + 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; + } + } +}; diff --git a/types/babel__traverse/index.d.ts b/types/babel__traverse/index.d.ts index 740f5f4429..a0a3730057 100644 --- a/types/babel__traverse/index.d.ts +++ b/types/babel__traverse/index.d.ts @@ -138,17 +138,17 @@ export class Binding { constantViolations: NodePath[]; } -export type Visitor = VisitNodeObject & { - [P in Node["type"]]?: VisitNode>; +export type Visitor = VisitNodeObject & { + [Type in Node["type"]]?: VisitNode>; }; -export type VisitNode = VisitNodeFunction | VisitNodeObject

; +export type VisitNode = VisitNodeFunction | VisitNodeObject; -export type VisitNodeFunction = (this: T, path: NodePath

, state: any) => void; +export type VisitNodeFunction = (this: S, path: NodePath

, state: S) => void; -export interface VisitNodeObject { - enter?(path: NodePath, state: any): void; - exit?(path: NodePath, state: any): void; +export interface VisitNodeObject { + enter?: VisitNodeFunction; + exit?: VisitNodeFunction; } export class NodePath {