Comparing version 0.6.3 to 0.6.4
@@ -329,2 +329,106 @@ // src/check.ts | ||
// node_modules/.pnpm/estree-walker@3.0.3/node_modules/estree-walker/src/async.js | ||
var AsyncWalker = class extends WalkerBase { | ||
/** | ||
* | ||
* @param {AsyncHandler} [enter] | ||
* @param {AsyncHandler} [leave] | ||
*/ | ||
constructor(enter, leave) { | ||
super(); | ||
this.should_skip = false; | ||
this.should_remove = false; | ||
this.replacement = null; | ||
this.context = { | ||
skip: () => this.should_skip = true, | ||
remove: () => this.should_remove = true, | ||
replace: (node) => this.replacement = node | ||
}; | ||
this.enter = enter; | ||
this.leave = leave; | ||
} | ||
/** | ||
* @template {Node} Parent | ||
* @param {Node} node | ||
* @param {Parent | null} parent | ||
* @param {keyof Parent} [prop] | ||
* @param {number | null} [index] | ||
* @returns {Promise<Node | null>} | ||
*/ | ||
async visit(node, parent, prop, index) { | ||
if (node) { | ||
if (this.enter) { | ||
const _should_skip = this.should_skip; | ||
const _should_remove = this.should_remove; | ||
const _replacement = this.replacement; | ||
this.should_skip = false; | ||
this.should_remove = false; | ||
this.replacement = null; | ||
await this.enter.call(this.context, node, parent, prop, index); | ||
if (this.replacement) { | ||
node = this.replacement; | ||
this.replace(parent, prop, index, node); | ||
} | ||
if (this.should_remove) { | ||
this.remove(parent, prop, index); | ||
} | ||
const skipped = this.should_skip; | ||
const removed = this.should_remove; | ||
this.should_skip = _should_skip; | ||
this.should_remove = _should_remove; | ||
this.replacement = _replacement; | ||
if (skipped) | ||
return node; | ||
if (removed) | ||
return null; | ||
} | ||
let key; | ||
for (key in node) { | ||
const value = node[key]; | ||
if (value && typeof value === "object") { | ||
if (Array.isArray(value)) { | ||
const nodes = ( | ||
/** @type {Array<unknown>} */ | ||
value | ||
); | ||
for (let i = 0; i < nodes.length; i += 1) { | ||
const item = nodes[i]; | ||
if (isNode2(item)) { | ||
if (!await this.visit(item, node, key, i)) { | ||
i--; | ||
} | ||
} | ||
} | ||
} else if (isNode2(value)) { | ||
await this.visit(value, node, key, null); | ||
} | ||
} | ||
} | ||
if (this.leave) { | ||
const _replacement = this.replacement; | ||
const _should_remove = this.should_remove; | ||
this.replacement = null; | ||
this.should_remove = false; | ||
await this.leave.call(this.context, node, parent, prop, index); | ||
if (this.replacement) { | ||
node = this.replacement; | ||
this.replace(parent, prop, index, node); | ||
} | ||
if (this.should_remove) { | ||
this.remove(parent, prop, index); | ||
} | ||
const removed = this.should_remove; | ||
this.replacement = _replacement; | ||
this.should_remove = _should_remove; | ||
if (removed) | ||
return null; | ||
} | ||
} | ||
return node; | ||
} | ||
}; | ||
function isNode2(value) { | ||
return value !== null && typeof value === "object" && "type" in value && typeof value.type === "string"; | ||
} | ||
// node_modules/.pnpm/estree-walker@3.0.3/node_modules/estree-walker/src/index.js | ||
@@ -335,5 +439,10 @@ function walk(ast, { enter, leave }) { | ||
} | ||
async function asyncWalk(ast, { enter, leave }) { | ||
const instance = new AsyncWalker(enter, leave); | ||
return await instance.visit(ast, null); | ||
} | ||
// src/walk.ts | ||
var walkAST = walk; | ||
var walkASTAsync = asyncWalk; | ||
function walkImportDeclaration(imports, node) { | ||
@@ -379,3 +488,4 @@ if (node.importKind === "type") | ||
walkAST, | ||
walkASTAsync, | ||
walkImportDeclaration | ||
}; |
@@ -41,3 +41,3 @@ import { Function, Literal, Node, CallExpression, Identifier, Program, Expression, PrivateName, ThisExpression, Super, TemplateLiteral, MemberExpression, ImportSpecifier, ImportDefaultSpecifier, ImportNamespaceSpecifier, ImportDeclaration } from '@babel/types'; | ||
declare const walkAST: <T = Node>(node: T, hooks: { | ||
type WalkHandlers<T, R> = { | ||
enter?: (this: { | ||
@@ -47,3 +47,3 @@ skip: () => void; | ||
replace: (node: T) => void; | ||
}, node: T, parent: T | null | undefined, key: string | null | undefined, index: number | null | undefined) => void; | ||
}, node: T, parent: T | null | undefined, key: string | null | undefined, index: number | null | undefined) => R; | ||
leave?: (this: { | ||
@@ -53,4 +53,6 @@ skip: () => void; | ||
replace: (node: T) => void; | ||
}, node: T, parent: T | null | undefined, key: string | null | undefined, index: number | null | undefined) => void; | ||
}) => T; | ||
}, node: T, parent: T | null | undefined, key: string | null | undefined, index: number | null | undefined) => R; | ||
}; | ||
declare const walkAST: <T = Node>(node: T, hooks: WalkHandlers<T, void>) => T; | ||
declare const walkASTAsync: <T = Node>(node: T, handlers: WalkHandlers<T, Promise<void>>) => T; | ||
interface ImportBinding { | ||
@@ -64,2 +66,2 @@ local: string; | ||
export { GetNode, ImportBinding, REGEX_DTS, REGEX_LANG_JSX, REGEX_LANG_TS, TS_NODE_TYPES, WithScope, attachScopes, babelParse, babelParseExpression, escapeKey, getLang, isCallOf, isDts, isFunctionType, isIdentifierOf, isLiteralType, isTs, isTypeOf, resolveIdentifier, resolveLiteral, resolveString, resolveTemplateLiteral, unwrapTSNode, walkAST, walkImportDeclaration }; | ||
export { GetNode, ImportBinding, REGEX_DTS, REGEX_LANG_JSX, REGEX_LANG_TS, TS_NODE_TYPES, WithScope, attachScopes, babelParse, babelParseExpression, escapeKey, getLang, isCallOf, isDts, isFunctionType, isIdentifierOf, isLiteralType, isTs, isTypeOf, resolveIdentifier, resolveLiteral, resolveString, resolveTemplateLiteral, unwrapTSNode, walkAST, walkASTAsync, walkImportDeclaration }; |
@@ -329,2 +329,106 @@ // src/check.ts | ||
// node_modules/.pnpm/estree-walker@3.0.3/node_modules/estree-walker/src/async.js | ||
var AsyncWalker = class extends WalkerBase { | ||
/** | ||
* | ||
* @param {AsyncHandler} [enter] | ||
* @param {AsyncHandler} [leave] | ||
*/ | ||
constructor(enter, leave) { | ||
super(); | ||
this.should_skip = false; | ||
this.should_remove = false; | ||
this.replacement = null; | ||
this.context = { | ||
skip: () => this.should_skip = true, | ||
remove: () => this.should_remove = true, | ||
replace: (node) => this.replacement = node | ||
}; | ||
this.enter = enter; | ||
this.leave = leave; | ||
} | ||
/** | ||
* @template {Node} Parent | ||
* @param {Node} node | ||
* @param {Parent | null} parent | ||
* @param {keyof Parent} [prop] | ||
* @param {number | null} [index] | ||
* @returns {Promise<Node | null>} | ||
*/ | ||
async visit(node, parent, prop, index) { | ||
if (node) { | ||
if (this.enter) { | ||
const _should_skip = this.should_skip; | ||
const _should_remove = this.should_remove; | ||
const _replacement = this.replacement; | ||
this.should_skip = false; | ||
this.should_remove = false; | ||
this.replacement = null; | ||
await this.enter.call(this.context, node, parent, prop, index); | ||
if (this.replacement) { | ||
node = this.replacement; | ||
this.replace(parent, prop, index, node); | ||
} | ||
if (this.should_remove) { | ||
this.remove(parent, prop, index); | ||
} | ||
const skipped = this.should_skip; | ||
const removed = this.should_remove; | ||
this.should_skip = _should_skip; | ||
this.should_remove = _should_remove; | ||
this.replacement = _replacement; | ||
if (skipped) | ||
return node; | ||
if (removed) | ||
return null; | ||
} | ||
let key; | ||
for (key in node) { | ||
const value = node[key]; | ||
if (value && typeof value === "object") { | ||
if (Array.isArray(value)) { | ||
const nodes = ( | ||
/** @type {Array<unknown>} */ | ||
value | ||
); | ||
for (let i = 0; i < nodes.length; i += 1) { | ||
const item = nodes[i]; | ||
if (isNode2(item)) { | ||
if (!await this.visit(item, node, key, i)) { | ||
i--; | ||
} | ||
} | ||
} | ||
} else if (isNode2(value)) { | ||
await this.visit(value, node, key, null); | ||
} | ||
} | ||
} | ||
if (this.leave) { | ||
const _replacement = this.replacement; | ||
const _should_remove = this.should_remove; | ||
this.replacement = null; | ||
this.should_remove = false; | ||
await this.leave.call(this.context, node, parent, prop, index); | ||
if (this.replacement) { | ||
node = this.replacement; | ||
this.replace(parent, prop, index, node); | ||
} | ||
if (this.should_remove) { | ||
this.remove(parent, prop, index); | ||
} | ||
const removed = this.should_remove; | ||
this.replacement = _replacement; | ||
this.should_remove = _should_remove; | ||
if (removed) | ||
return null; | ||
} | ||
} | ||
return node; | ||
} | ||
}; | ||
function isNode2(value) { | ||
return value !== null && typeof value === "object" && "type" in value && typeof value.type === "string"; | ||
} | ||
// node_modules/.pnpm/estree-walker@3.0.3/node_modules/estree-walker/src/index.js | ||
@@ -335,5 +439,10 @@ function walk(ast, { enter, leave }) { | ||
} | ||
async function asyncWalk(ast, { enter, leave }) { | ||
const instance = new AsyncWalker(enter, leave); | ||
return await instance.visit(ast, null); | ||
} | ||
// src/walk.ts | ||
var walkAST = walk; | ||
var walkASTAsync = asyncWalk; | ||
function walkImportDeclaration(imports, node) { | ||
@@ -379,3 +488,4 @@ if (node.importKind === "type") | ||
walkAST, | ||
walkASTAsync, | ||
walkImportDeclaration | ||
}; |
{ | ||
"name": "ast-kit", | ||
"version": "0.6.3", | ||
"version": "0.6.4", | ||
"packageManager": "pnpm@8.6.0", | ||
@@ -5,0 +5,0 @@ "description": "AST Toolkit.", |
Sorry, the diff of this file is not supported yet
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
50586
1515