ast-kit
Advanced tools
Comparing version 0.8.0 to 0.9.0
@@ -567,2 +567,35 @@ // src/check.ts | ||
var walkASTAsync = asyncWalk; | ||
async function walkASTSetup(node, cb) { | ||
const callbacks = { | ||
enter: [], | ||
leave: [] | ||
}; | ||
const setup = { | ||
onEnter(type, cb2) { | ||
const types = Array.isArray(type) ? type : [type]; | ||
callbacks.enter.push({ types, cb: cb2 }); | ||
}, | ||
onLeave(type, cb2) { | ||
const types = Array.isArray(type) ? type : [type]; | ||
callbacks.leave.push({ types, cb: cb2 }); | ||
} | ||
}; | ||
await cb(setup); | ||
return walkASTAsync(node, { | ||
async enter(node2, parent, key, index) { | ||
for (const { types, cb: cb2 } of callbacks.enter) { | ||
if (!isTypeOf(node2, types)) | ||
continue; | ||
await cb2.call(this, node2, parent, key, index); | ||
} | ||
}, | ||
async leave(node2, parent, key, index) { | ||
for (const { types, cb: cb2 } of callbacks.leave) { | ||
if (!isTypeOf(node2, types)) | ||
continue; | ||
await cb2.call(this, node2, parent, key, index); | ||
} | ||
} | ||
}); | ||
} | ||
function walkImportDeclaration(imports, node) { | ||
@@ -611,3 +644,4 @@ if (node.importKind === "type") | ||
walkASTAsync, | ||
walkASTSetup, | ||
walkImportDeclaration | ||
}; |
@@ -57,16 +57,20 @@ import * as t from '@babel/types'; | ||
type WalkHandlers<T, R> = { | ||
enter?: (this: { | ||
skip: () => void; | ||
remove: () => void; | ||
replace: (node: T) => void; | ||
}, node: T, parent: T | null | undefined, key: string | null | undefined, index: number | null | undefined) => R; | ||
leave?: (this: { | ||
skip: () => void; | ||
remove: () => void; | ||
replace: (node: T) => void; | ||
}, node: T, parent: T | null | undefined, key: string | null | undefined, index: number | null | undefined) => R; | ||
}; | ||
interface WalkThis<T> { | ||
skip: () => void; | ||
remove: () => void; | ||
replace: (node: T) => void; | ||
} | ||
type WalkCallback<T, R> = (this: WalkThis<T>, node: T, parent: T | null | undefined, key: string | null | undefined, index: number | null | undefined) => R; | ||
interface WalkHandlers<T, R> { | ||
enter?: WalkCallback<T, R>; | ||
leave?: WalkCallback<T, R>; | ||
} | ||
declare const walkAST: <T = t.Node>(node: T, hooks: WalkHandlers<T, void>) => T | null; | ||
declare const walkASTAsync: <T = t.Node>(node: T, handlers: WalkHandlers<T, Promise<void>>) => Promise<T | null>; | ||
type SetupCallback<T extends NodeType = NodeType, N = GetNode<T>> = (this: WalkThis<N>, node: N, parent: T extends keyof t.ParentMaps ? t.ParentMaps[T] : t.Node | null, key: string | null | undefined, index: number | null | undefined) => void | Promise<void>; | ||
interface WalkSetup { | ||
onEnter<T extends NodeType = NodeType>(type: T | T[], cb: SetupCallback<T>): void; | ||
onLeave<T extends NodeType = NodeType>(type: T | T[], cb: SetupCallback<T>): void; | ||
} | ||
declare function walkASTSetup(node: t.Node, cb: (setup: WalkSetup) => void | Promise<void>): Promise<t.Node | null>; | ||
interface ImportBinding { | ||
@@ -81,2 +85,2 @@ local: string; | ||
export { GetNode, ImportBinding, LiteralUnion, ObjectPropertyLike, REGEX_DTS, REGEX_LANG_JSX, REGEX_LANG_TS, TS_NODE_TYPES, WithScope, attachScopes, babelParse, babelParseExpression, escapeKey, getLang, isCallOf, isDts, isFunctionType, isIdentifierOf, isLiteralType, isReferenced, isTs, isTypeOf, resolveIdentifier, resolveLiteral, resolveObjectKey, resolveString, resolveTemplateLiteral, unwrapTSNode, walkAST, walkASTAsync, walkImportDeclaration }; | ||
export { GetNode, ImportBinding, LiteralUnion, NodeType, ObjectPropertyLike, REGEX_DTS, REGEX_LANG_JSX, REGEX_LANG_TS, TS_NODE_TYPES, WithScope, attachScopes, babelParse, babelParseExpression, escapeKey, getLang, isCallOf, isDts, isFunctionType, isIdentifierOf, isLiteralType, isReferenced, isTs, isTypeOf, resolveIdentifier, resolveLiteral, resolveObjectKey, resolveString, resolveTemplateLiteral, unwrapTSNode, walkAST, walkASTAsync, walkASTSetup, walkImportDeclaration }; |
@@ -567,2 +567,35 @@ // src/check.ts | ||
var walkASTAsync = asyncWalk; | ||
async function walkASTSetup(node, cb) { | ||
const callbacks = { | ||
enter: [], | ||
leave: [] | ||
}; | ||
const setup = { | ||
onEnter(type, cb2) { | ||
const types = Array.isArray(type) ? type : [type]; | ||
callbacks.enter.push({ types, cb: cb2 }); | ||
}, | ||
onLeave(type, cb2) { | ||
const types = Array.isArray(type) ? type : [type]; | ||
callbacks.leave.push({ types, cb: cb2 }); | ||
} | ||
}; | ||
await cb(setup); | ||
return walkASTAsync(node, { | ||
async enter(node2, parent, key, index) { | ||
for (const { types, cb: cb2 } of callbacks.enter) { | ||
if (!isTypeOf(node2, types)) | ||
continue; | ||
await cb2.call(this, node2, parent, key, index); | ||
} | ||
}, | ||
async leave(node2, parent, key, index) { | ||
for (const { types, cb: cb2 } of callbacks.leave) { | ||
if (!isTypeOf(node2, types)) | ||
continue; | ||
await cb2.call(this, node2, parent, key, index); | ||
} | ||
} | ||
}); | ||
} | ||
function walkImportDeclaration(imports, node) { | ||
@@ -611,3 +644,4 @@ if (node.importKind === "type") | ||
walkASTAsync, | ||
walkASTSetup, | ||
walkImportDeclaration | ||
}; |
{ | ||
"name": "ast-kit", | ||
"version": "0.8.0", | ||
"version": "0.9.0", | ||
"packageManager": "pnpm@8.6.0", | ||
@@ -5,0 +5,0 @@ "description": "AST Toolkit.", |
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
70541
1996