Skip to content

Commit

Permalink
Further clean up walker types
Browse files Browse the repository at this point in the history
  • Loading branch information
marijnh committed Feb 13, 2020
1 parent de6edeb commit e9372c1
Showing 1 changed file with 72 additions and 74 deletions.
146 changes: 72 additions & 74 deletions acorn-walk/dist/walk.d.ts
Original file line number Diff line number Diff line change
@@ -1,115 +1,113 @@
import acorn from 'acorn';
import {Node} from 'acorn';

declare module "acorn-walk" {
type NodeType = acorn.Node["type"];

type FullWalkerCallback<TState> = (
node: acorn.Node,
state: TState,
type: NodeType
node: Node,
state: TState,
type: string
) => void;

type FullAncestorWalkerCallback<TState> = (
node: acorn.Node,
state: TState | acorn.Node[],
ancestors: acorn.Node[],
type: NodeType
node: Node,
state: TState | Node[],
ancestors: Node[],
type: string
) => void;
type WalkerCallback<TState> = (node: acorn.Node, state: TState) => void;
type WalkerCallback<TState> = (node: Node, state: TState) => void;

type SimpleWalkerFn<K extends NodeType, TState> = (
node: acorn.Node,
state: TState
type SimpleWalkerFn<TState> = (
node: Node,
state: TState
) => void;

type AncestorWalkerFn<K extends NodeType, TState> = (
node: acorn.Node,
state: TState| acorn.Node[],
ancestors: acorn.Node[]
type AncestorWalkerFn<TState> = (
node: Node,
state: TState| Node[],
ancestors: Node[]
) => void;

type RecursiveWalkerFn<K extends NodeType, TState> = (
node: acorn.Node,
state: TState,
callback: WalkerCallback<TState>
type RecursiveWalkerFn<TState> = (
node: Node,
state: TState,
callback: WalkerCallback<TState>
) => void;

type SimpleVisitors<Types extends NodeType, TState> = {
[Type in Types]: SimpleWalkerFn<Type, TState>
type SimpleVisitors<TState> = {
[type: string]: SimpleWalkerFn<TState>
};

type AncestorVisitors<Types extends NodeType, TState> = {
[Type in Types]: AncestorWalkerFn<Type, TState>
type AncestorVisitors<TState> = {
[type: string]: AncestorWalkerFn<TState>
};

type RecursiveVisitors<Types extends NodeType, TState> = {
[Type in Types]: RecursiveWalkerFn<Type, TState>
type RecursiveVisitors<TState> = {
[type: string]: RecursiveWalkerFn<TState>
};

type FindPredicate = (type: NodeType, node: acorn.Node) => boolean;
type FindPredicate = (type: string, node: Node) => boolean;

interface Found<Type extends NodeType, TState> {
node: acorn.Node,
state: TState
interface Found<TState> {
node: Node,
state: TState
}

export function simple<TState, K extends NodeType>(
node: acorn.Node,
visitors: SimpleVisitors<K, TState>,
base?: RecursiveVisitors<NodeType, TState>,
state?: TState
export function simple<TState>(
node: Node,
visitors: SimpleVisitors<TState>,
base?: RecursiveVisitors<TState>,
state?: TState
): void;

export function ancestor<TState, K extends NodeType>(
node: acorn.Node,
visitors: AncestorVisitors<K, TState>,
base?: RecursiveVisitors<NodeType, TState>,
state?: TState
export function ancestor<TState>(
node: Node,
visitors: AncestorVisitors<TState>,
base?: RecursiveVisitors<TState>,
state?: TState
): void;

export function recursive<TState, K extends NodeType>(
node: acorn.Node,
state: TState,
functions: RecursiveVisitors<K, TState>,
base?: RecursiveVisitors<NodeType, TState>
export function recursive<TState>(
node: Node,
state: TState,
functions: RecursiveVisitors<TState>,
base?: RecursiveVisitors<TState>
): void;

export function full<TState>(
node: acorn.Node,
callback: FullWalkerCallback<TState>,
base?: RecursiveVisitors<NodeType, TState>,
state?: TState
node: Node,
callback: FullWalkerCallback<TState>,
base?: RecursiveVisitors<TState>,
state?: TState
): void;

export function fullAncestor<TState>(
node: acorn.Node,
callback: FullAncestorWalkerCallback<TState>,
base?: RecursiveVisitors<NodeType, TState>,
state?: TState
node: Node,
callback: FullAncestorWalkerCallback<TState>,
base?: RecursiveVisitors<TState>,
state?: TState
): void;

export function make<TState, K extends NodeType>(
functions: RecursiveVisitors<K, TState>,
base?: RecursiveVisitors<NodeType, TState>
): RecursiveVisitors<NodeType, TState>;
export function make<TState>(
functions: RecursiveVisitors<TState>,
base?: RecursiveVisitors<TState>
): RecursiveVisitors<TState>;

export function findNodeAt<TState, K extends NodeType>(
node: acorn.Node,
start: number | undefined,
end: number | undefined,
type: K,
base?: RecursiveVisitors<NodeType, TState>,
state?: TState
): Found<K, TState> | undefined;
export function findNodeAt<TState>(
node: Node,
start: number | undefined,
end: number | undefined,
type: string,
base?: RecursiveVisitors<TState>,
state?: TState
): Found<TState> | undefined;

export function findNodeAt<TState>(
node: acorn.Node,
start: number | undefined,
end: number | undefined,
type?: FindPredicate,
base?: RecursiveVisitors<NodeType, TState>,
state?: TState
): Found<NodeType, TState> | undefined;
node: Node,
start: number | undefined,
end: number | undefined,
type?: FindPredicate,
base?: RecursiveVisitors<TState>,
state?: TState
): Found<TState> | undefined;

export const findNodeAround: typeof findNodeAt;

Expand Down

0 comments on commit e9372c1

Please sign in to comment.