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鈥檒l occasionally send you account related emails.

Already on GitHub? Sign in to your account

[babel__traverse] Add new method hasNode() to NodePath #59355

Merged
merged 1 commit into from Apr 14, 2022
Merged
Show file tree
Hide file tree
Changes from all 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
8 changes: 8 additions & 0 deletions types/babel__traverse/babel__traverse-tests.ts
Expand Up @@ -359,3 +359,11 @@ const path: NodePath<t.ExportDefaultDeclaration | t.ExportNamedDeclaration> = ne
if (path.isExportNamedDeclaration()) {
path.type; // $ExpectType "ExportNamedDeclaration"
}

const nullPath: NodePath<t.Identifier | undefined> = new NodePath<t.Identifier | undefined>(null as any, {} as any);

nullPath.type; // $ExpectType "Identifier" | undefined

if (nullPath.hasNode()) {
nullPath.type; // $ExpectType "Identifier"
}
23 changes: 14 additions & 9 deletions types/babel__traverse/index.d.ts
@@ -1,4 +1,4 @@
// Type definitions for @babel/traverse 7.14
// Type definitions for @babel/traverse 7.17
// Project: https://github.com/babel/babel/tree/main/packages/babel-traverse, https://babeljs.io
// Definitions by: Troy Gerwien <https://github.com/yortus>
// Marvin Hagemeister <https://github.com/marvinhagemeister>
Expand Down Expand Up @@ -140,7 +140,12 @@ export class Scope {

crawl(): void;

push(opts: { id: t.LVal; init?: t.Expression | undefined; unique?: boolean | undefined; kind?: 'var' | 'let' | 'const' | undefined }): void;
push(opts: {
id: t.LVal;
init?: t.Expression | undefined;
unique?: boolean | undefined;
kind?: 'var' | 'let' | 'const' | undefined;
}): void;
danez marked this conversation as resolved.
Show resolved Hide resolved

getProgramParent(): Scope;

Expand Down Expand Up @@ -194,13 +199,11 @@ export class Binding {
constantViolations: NodePath[];
}

export type Visitor<S = {}> = VisitNodeObject<S, Node> &
{
[Type in Node['type']]?: VisitNode<S, Extract<Node, { type: Type }>>;
} &
{
[K in keyof t.Aliases]?: VisitNode<S, t.Aliases[K]>;
};
export type Visitor<S = {}> = VisitNodeObject<S, Node> & {
[Type in Node['type']]?: VisitNode<S, Extract<Node, { type: Type }>>;
} & {
[K in keyof t.Aliases]?: VisitNode<S, t.Aliases[K]>;
};
danez marked this conversation as resolved.
Show resolved Hide resolved

export type VisitNode<S, P extends Node> = VisitNodeFunction<S, P> | VisitNodeObject<S, P>;

Expand Down Expand Up @@ -254,6 +257,8 @@ export class NodePath<T = Node> {

getData(key: string, def?: any): any;

hasNode(): this is NodePath<NonNullable<this['node']>>;

buildCodeFrameError<TError extends Error>(msg: string, Error?: new (msg: string) => TError): TError;

traverse<T>(visitor: Visitor<T>, state: T): void;
Expand Down
11 changes: 11 additions & 0 deletions types/babel__traverse/ts4.1/babel__traverse-tests.ts
Expand Up @@ -349,3 +349,14 @@ const visitorWithInvalidDenylist: Visitor = {
// $ExpectError
denylist: ['SomeRandomType'],
};

const nullPath: NodePath<t.Identifier | undefined> = new NodePath<t.Identifier | undefined>(
null as any,
{} as any,
);

nullPath.type; // $ExpectType "Identifier" | undefined

if (nullPath.hasNode()) {
nullPath.type; // $ExpectType "Identifier"
}
14 changes: 7 additions & 7 deletions types/babel__traverse/ts4.1/index.d.ts
Expand Up @@ -182,13 +182,11 @@ export class Binding {
constantViolations: NodePath[];
}

export type Visitor<S = {}> = VisitNodeObject<S, Node> &
{
[Type in Node['type']]?: VisitNode<S, Extract<Node, { type: Type }>>;
} &
{
[K in keyof t.Aliases]?: VisitNode<S, t.Aliases[K]>;
};
export type Visitor<S = {}> = VisitNodeObject<S, Node> & {
[Type in Node['type']]?: VisitNode<S, Extract<Node, { type: Type }>>;
} & {
[K in keyof t.Aliases]?: VisitNode<S, t.Aliases[K]>;
};
danez marked this conversation as resolved.
Show resolved Hide resolved

export type VisitNode<S, P extends Node> = VisitNodeFunction<S, P> | VisitNodeObject<S, P>;

Expand Down Expand Up @@ -242,6 +240,8 @@ export class NodePath<T = Node> {

getData(key: string, def?: any): any;

hasNode(): this is NodePath<NonNullable<this['node']>>;

buildCodeFrameError<TError extends Error>(msg: string, Error?: new (msg: string) => TError): TError;

traverse<T>(visitor: Visitor<T>, state: T): void;
Expand Down