You're Invited:Meet the Socket Team at RSAC and BSidesSF 2026, March 23–26.RSVP
Socket
Book a DemoSign in
Socket

ast-kit

Package Overview
Dependencies
Maintainers
1
Versions
54
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

ast-kit - npm Package Compare versions

Comparing version
2.0.0
to
2.1.0
+20
-15
dist/index.d.ts

@@ -16,5 +16,2 @@ import { ParseError, ParseResult, ParserOptions } from "@babel/parser";

*/
/**
* All possible node types.
*/
type NodeType = t$7.Node["type"] | "Function" | "Literal" | "Expression";

@@ -103,3 +100,9 @@ /**

declare function isReferenced(node: t$7.Node, parent: t$7.Node, grandparent?: t$7.Node): boolean;
declare function isIdentifier(node?: t$7.Node | undefined | null): node is t$7.Identifier;
declare function isStaticProperty(node?: t$7.Node | undefined | null): node is t$7.ObjectProperty;
declare function isStaticPropertyKey(node: t$7.Node, parent: t$7.Node): boolean;
declare function isForStatement(stmt: t$7.Node): stmt is t$7.ForStatement | t$7.ForOfStatement | t$7.ForInStatement;
declare function isReferencedIdentifier(id: t$7.Identifier, parent: t$7.Node | null | undefined, parentStack: t$7.Node[]): boolean;
declare function isInDestructureAssignment(parent: t$7.Node, parentStack: t$7.Node[]): boolean;
declare function isInNewExpression(parentStack: t$7.Node[]): boolean;
//#endregion

@@ -128,3 +131,2 @@ //#region src/create.d.ts

declare function createTSLiteralType(literal: t$6.TSLiteralType["literal"]): t$6.TSLiteralType;
//#endregion

@@ -139,3 +141,2 @@ //#region src/extract.d.ts

declare function extractIdentifiers(node: t$5.Node, identifiers?: t$5.Identifier[]): t$5.Identifier[];
//#endregion

@@ -164,3 +165,2 @@ //#region src/lang.d.ts

declare function isTs(lang?: string): boolean;
//#endregion

@@ -178,3 +178,2 @@ //#region src/loc.d.ts

declare function locateTrailingComma(code: string, start: number, end: number, comments?: t$4.Comment[]): number;
//#endregion

@@ -217,3 +216,2 @@ //#region src/parse.d.ts

};
//#endregion

@@ -257,3 +255,2 @@ //#region src/resolve.d.ts

declare function resolveObjectKey(node: ObjectPropertyLike, raw: true): string;
//#endregion

@@ -302,3 +299,2 @@ //#region src/scope.d.ts

declare function attachScopes(ast: Node, propertyName?: string): Scope;
//#endregion

@@ -310,3 +306,2 @@ //#region src/types.d.ts

type LiteralUnion<LiteralType, BaseType extends null | undefined | string | number | boolean | symbol | bigint = string> = LiteralType | (BaseType & Record<never, never>);
//#endregion

@@ -328,3 +323,2 @@ //#region src/utils.d.ts

declare function escapeKey(rawKey: string): string;
//#endregion

@@ -392,4 +386,15 @@ //#region src/walk.d.ts

declare function walkExportDeclaration(exports: Record<string, ExportBinding>, node: t.ExportDeclaration): void;
/**
* Modified from https://github.com/vuejs/core/blob/main/packages/compiler-core/src/babelUtils.ts
* To support browser environments and JSX.
*
* https://github.com/vuejs/core/blob/main/LICENSE
*/
/**
* Return value indicates whether the AST walked can be a constant
*/
declare function walkIdentifiers(root: t.Node, onIdentifier: (node: t.Identifier, parent: t.Node | null | undefined, parentStack: t.Node[], isReference: boolean, isLocal: boolean) => void, includeAll?: boolean, parentStack?: t.Node[], knownIds?: Record<string, number>): void;
declare function walkFunctionParams(node: t.Function, onIdent: (id: t.Identifier) => void): void;
declare function walkBlockDeclarations(block: t.BlockStatement | t.Program, onIdent: (node: t.Identifier) => void): void;
//#endregion
export { AttachedScope, ExportBinding, GetNode, ImportBinding, LiteralUnion, NodeType, ObjectPropertyLike, REGEX_DTS, REGEX_LANG_JSX, REGEX_LANG_TS, Scope, ScopeOptions, TS_NODE_TYPES, WithScope, attachScopes, babelParse, babelParseExpression, createStringLiteral, createTSLiteralType, createTSUnionType, escapeKey, extractIdentifiers, getBabelParserOptions, getLang, isCallOf, isDeclarationType, isDts, isExpressionType, isFunctionType, isIdentifierOf, isLiteralType, isReferenced, isTaggedFunctionCallOf, isTs, isTypeOf, locateTrailingComma, parseCache, resolveIdentifier, resolveLiteral, resolveObjectKey, resolveString, resolveTemplateLiteral, tryResolveIdentifier, unwrapTSNode, walkAST, walkASTAsync, walkExportDeclaration, walkImportDeclaration };
export { AttachedScope, ExportBinding, GetNode, ImportBinding, LiteralUnion, NodeType, ObjectPropertyLike, REGEX_DTS, REGEX_LANG_JSX, REGEX_LANG_TS, Scope, ScopeOptions, TS_NODE_TYPES, WithScope, attachScopes, babelParse, babelParseExpression, createStringLiteral, createTSLiteralType, createTSUnionType, escapeKey, extractIdentifiers, getBabelParserOptions, getLang, isCallOf, isDeclarationType, isDts, isExpressionType, isForStatement, isFunctionType, isIdentifier, isIdentifierOf, isInDestructureAssignment, isInNewExpression, isLiteralType, isReferenced, isReferencedIdentifier, isStaticProperty, isStaticPropertyKey, isTaggedFunctionCallOf, isTs, isTypeOf, locateTrailingComma, parseCache, resolveIdentifier, resolveLiteral, resolveObjectKey, resolveString, resolveTemplateLiteral, tryResolveIdentifier, unwrapTSNode, walkAST, walkASTAsync, walkBlockDeclarations, walkExportDeclaration, walkFunctionParams, walkIdentifiers, walkImportDeclaration };

@@ -49,3 +49,3 @@ import { extname } from "pathe";

function isIdentifierOf(node, test) {
return !!node && node.type === "Identifier" && match(node.name, test);
return isIdentifier(node) && match(node.name, test);
}

@@ -204,2 +204,46 @@ /**

}
function isIdentifier(node) {
return !!node && (node.type === "Identifier" || node.type === "JSXIdentifier");
}
function isStaticProperty(node) {
return !!node && (node.type === "ObjectProperty" || node.type === "ObjectMethod") && !node.computed;
}
function isStaticPropertyKey(node, parent) {
return isStaticProperty(parent) && parent.key === node;
}
function isForStatement(stmt) {
return stmt.type === "ForOfStatement" || stmt.type === "ForInStatement" || stmt.type === "ForStatement";
}
function isReferencedIdentifier(id, parent, parentStack) {
if (!parent) return true;
if (id.name === "arguments") return false;
if (isReferenced(id, parent)) return true;
switch (parent.type) {
case "AssignmentExpression":
case "AssignmentPattern": return true;
case "ObjectPattern":
case "ArrayPattern": return isInDestructureAssignment(parent, parentStack);
}
return false;
}
function isInDestructureAssignment(parent, parentStack) {
if (parent && (parent.type === "ObjectProperty" || parent.type === "ArrayPattern")) {
let i = parentStack.length;
while (i--) {
const p = parentStack[i];
if (p.type === "AssignmentExpression") return true;
else if (p.type !== "ObjectProperty" && !p.type.endsWith("Pattern")) break;
}
}
return false;
}
function isInNewExpression(parentStack) {
let i = parentStack.length;
while (i--) {
const p = parentStack[i];
if (p.type === "NewExpression") return true;
else if (p.type !== "MemberExpression") break;
}
return false;
}
/* v8 ignore end */

@@ -261,5 +305,7 @@

case "Identifier":
case "JSXIdentifier":
identifiers.push(node);
break;
case "MemberExpression": {
case "MemberExpression":
case "JSXMemberExpression": {
let object = node;

@@ -827,2 +873,35 @@ while (object.type === "MemberExpression") object = object.object;

//#endregion
//#region src/utils.ts
const TS_NODE_TYPES = [
"TSAsExpression",
"TSTypeAssertion",
"TSNonNullExpression",
"TSInstantiationExpression",
"TSSatisfiesExpression"
];
/**
* Unwraps a TypeScript node by recursively traversing the AST until a non-TypeScript node is found.
* @param node - The TypeScript node to unwrap.
* @returns The unwrapped node.
*/
function unwrapTSNode(node) {
if (isTypeOf(node, TS_NODE_TYPES)) return unwrapTSNode(node.expression);
else return node;
}
/**
* Escapes a raw key by checking if it needs to be wrapped with quotes or not.
*
* @param rawKey - The raw key to escape.
* @returns The escaped key.
*/
function escapeKey(rawKey) {
if (String(+rawKey) === rawKey) return rawKey;
try {
const node = parseExpression(`({${rawKey}: 1})`);
if (node.properties[0].key.type === "Identifier") return rawKey;
} catch {}
return JSON.stringify(rawKey);
}
//#endregion
//#region src/walk.ts

@@ -944,2 +1023,71 @@ /**

}
/**
* Modified from https://github.com/vuejs/core/blob/main/packages/compiler-core/src/babelUtils.ts
* To support browser environments and JSX.
*
* https://github.com/vuejs/core/blob/main/LICENSE
*/
/**
* Return value indicates whether the AST walked can be a constant
*/
function walkIdentifiers(root, onIdentifier, includeAll = false, parentStack = [], knownIds = Object.create(null)) {
const rootExp = root.type === "Program" ? root.body[0].type === "ExpressionStatement" && root.body[0].expression : root;
walkAST(root, {
enter(node, parent) {
parent && parentStack.push(parent);
if (parent && parent.type.startsWith("TS") && !TS_NODE_TYPES.includes(parent.type)) return this.skip();
if (isIdentifier(node)) {
const isLocal = !!knownIds[node.name];
const isRefed = isReferencedIdentifier(node, parent, parentStack);
if (includeAll || isRefed && !isLocal) onIdentifier(node, parent, parentStack, isRefed, isLocal);
} else if (node.type === "ObjectProperty" && parent?.type === "ObjectPattern") node.inPattern = true;
else if (isFunctionType(node))
/* v8 ignore start */
if (node.scopeIds) node.scopeIds.forEach((id) => markKnownIds(id, knownIds));
else walkFunctionParams(node, (id) => markScopeIdentifier(node, id, knownIds));
else if (node.type === "BlockStatement")
/* v8 ignore start */
if (node.scopeIds) node.scopeIds.forEach((id) => markKnownIds(id, knownIds));
else walkBlockDeclarations(node, (id) => markScopeIdentifier(node, id, knownIds));
else if (node.type === "CatchClause" && node.param) for (const id of extractIdentifiers(node.param)) markScopeIdentifier(node, id, knownIds);
else if (isForStatement(node)) walkForStatement(node, false, (id) => markScopeIdentifier(node, id, knownIds));
},
leave(node, parent) {
parent && parentStack.pop();
if (node !== rootExp && node.scopeIds) for (const id of node.scopeIds) {
knownIds[id]--;
if (knownIds[id] === 0) delete knownIds[id];
}
}
});
}
function walkFunctionParams(node, onIdent) {
for (const p of node.params) for (const id of extractIdentifiers(p)) onIdent(id);
}
function walkBlockDeclarations(block, onIdent) {
for (const stmt of block.body) if (stmt.type === "VariableDeclaration") {
if (stmt.declare) continue;
for (const decl of stmt.declarations) for (const id of extractIdentifiers(decl.id)) onIdent(id);
} else if (stmt.type === "FunctionDeclaration" || stmt.type === "ClassDeclaration") {
/* v8 ignore next */
if (stmt.declare || !stmt.id) continue;
onIdent(stmt.id);
} else if (isForStatement(stmt)) walkForStatement(stmt, true, onIdent);
}
function walkForStatement(stmt, isVar, onIdent) {
const variable = stmt.type === "ForStatement" ? stmt.init : stmt.left;
if (variable && variable.type === "VariableDeclaration" && (variable.kind === "var" ? isVar : !isVar)) for (const decl of variable.declarations) for (const id of extractIdentifiers(decl.id)) onIdent(id);
}
function markKnownIds(name, knownIds) {
if (name in knownIds) knownIds[name]++;
else knownIds[name] = 1;
}
function markScopeIdentifier(node, child, knownIds) {
const { name } = child;
/* v8 ignore start */
if (node.scopeIds && node.scopeIds.has(name)) return;
/* v8 ignore end */
markKnownIds(name, knownIds);
(node.scopeIds || (node.scopeIds = new Set())).add(name);
}

@@ -980,2 +1128,5 @@ //#endregion

var Scope = class {
parent;
isBlockScope;
declarations;
constructor(options = {}) {

@@ -1060,35 +1211,2 @@ this.parent = options.parent;

//#endregion
//#region src/utils.ts
const TS_NODE_TYPES = [
"TSAsExpression",
"TSTypeAssertion",
"TSNonNullExpression",
"TSInstantiationExpression",
"TSSatisfiesExpression"
];
/**
* Unwraps a TypeScript node by recursively traversing the AST until a non-TypeScript node is found.
* @param node - The TypeScript node to unwrap.
* @returns The unwrapped node.
*/
function unwrapTSNode(node) {
if (isTypeOf(node, TS_NODE_TYPES)) return unwrapTSNode(node.expression);
else return node;
}
/**
* Escapes a raw key by checking if it needs to be wrapped with quotes or not.
*
* @param rawKey - The raw key to escape.
* @returns The escaped key.
*/
function escapeKey(rawKey) {
if (String(+rawKey) === rawKey) return rawKey;
try {
const node = parseExpression(`({${rawKey}: 1})`);
if (node.properties[0].key.type === "Identifier") return rawKey;
} catch {}
return JSON.stringify(rawKey);
}
//#endregion
export { REGEX_DTS, REGEX_LANG_JSX, REGEX_LANG_TS, Scope, TS_NODE_TYPES, attachScopes, babelParse, babelParseExpression, createStringLiteral, createTSLiteralType, createTSUnionType, escapeKey, extractIdentifiers, getBabelParserOptions, getLang, isCallOf, isDeclarationType, isDts, isExpressionType, isFunctionType, isIdentifierOf, isLiteralType, isReferenced, isTaggedFunctionCallOf, isTs, isTypeOf, locateTrailingComma, parseCache, resolveIdentifier, resolveLiteral, resolveObjectKey, resolveString, resolveTemplateLiteral, tryResolveIdentifier, unwrapTSNode, walkAST, walkASTAsync, walkExportDeclaration, walkImportDeclaration };
export { REGEX_DTS, REGEX_LANG_JSX, REGEX_LANG_TS, Scope, TS_NODE_TYPES, attachScopes, babelParse, babelParseExpression, createStringLiteral, createTSLiteralType, createTSUnionType, escapeKey, extractIdentifiers, getBabelParserOptions, getLang, isCallOf, isDeclarationType, isDts, isExpressionType, isForStatement, isFunctionType, isIdentifier, isIdentifierOf, isInDestructureAssignment, isInNewExpression, isLiteralType, isReferenced, isReferencedIdentifier, isStaticProperty, isStaticPropertyKey, isTaggedFunctionCallOf, isTs, isTypeOf, locateTrailingComma, parseCache, resolveIdentifier, resolveLiteral, resolveObjectKey, resolveString, resolveTemplateLiteral, tryResolveIdentifier, unwrapTSNode, walkAST, walkASTAsync, walkBlockDeclarations, walkExportDeclaration, walkFunctionParams, walkIdentifiers, walkImportDeclaration };
{
"name": "ast-kit",
"version": "2.0.0",
"version": "2.1.0",
"description": "A toolkit for easy Babel AST generation and manipulation.",

@@ -31,21 +31,21 @@ "type": "module",

"dependencies": {
"@babel/parser": "^7.27.2",
"@babel/parser": "^7.27.3",
"pathe": "^2.0.3"
},
"devDependencies": {
"@babel/types": "^7.27.1",
"@sxzz/eslint-config": "^7.0.0",
"@babel/types": "^7.27.3",
"@sxzz/eslint-config": "^7.0.1",
"@sxzz/prettier-config": "^2.2.1",
"@types/node": "^22.15.17",
"@vitest/coverage-v8": "^3.1.3",
"@vitest/ui": "^3.1.3",
"bumpp": "^10.1.0",
"eslint": "^9.26.0",
"@types/node": "^22.15.24",
"@vitest/coverage-v8": "^3.1.4",
"@vitest/ui": "^3.1.4",
"bumpp": "^10.1.1",
"eslint": "^9.27.0",
"estree-walker": "^3.0.3",
"fast-glob": "^3.3.3",
"prettier": "^3.5.3",
"tsdown": "^0.11.1",
"tsdown": "^0.12.4",
"tsx": "^4.19.4",
"typescript": "5.8.3",
"vitest": "^3.1.3"
"vitest": "^3.1.4"
},

@@ -52,0 +52,0 @@ "engines": {