Socket
Socket
Sign inDemoInstall

ast-kit

Package Overview
Dependencies
2
Maintainers
0
Versions
37
Alerts
File Explorer

Advanced tools

Install Socket

Detect and block malicious and high-risk dependencies

Install

Comparing version 0.12.2 to 1.0.0

10

dist/index.d.ts

@@ -288,10 +288,2 @@ import * as t from '@babel/types';

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[] | SetupFilter<GetNode<T>> | WalkCallback<t.Node, void>, cb?: SetupCallback<T, GetNode<T>>) => void;
onLeave: <T extends NodeType = NodeType>(type: T | T[] | SetupFilter<GetNode<T>> | WalkCallback<t.Node, void>, cb?: SetupCallback<T, GetNode<T>>) => void;
}
type SetupFilter<N extends t.Node = t.Node> = (this: WalkThis<t.Node>, node: t.Node, parent: t.Node | null | undefined, key: string | null | undefined, index: number | null | undefined) => node is N;
/** @deprecated */
declare function walkASTSetup(node: t.Node, cb: (setup: WalkSetup) => void | Promise<void>): Promise<t.Node | null>;
interface ImportBinding {

@@ -329,2 +321,2 @@ local: string;

export { type AttachedScope, type ExportBinding, type GetNode, type ImportBinding, type LiteralUnion, type NodeType, type ObjectPropertyLike, type ParseResult, REGEX_DTS, REGEX_LANG_JSX, REGEX_LANG_TS, TS_NODE_TYPES, type WithScope, attachScopes, babelParse, babelParseExpression, createStringLiteral, createTSLiteralType, createTSUnionType, escapeKey, extractIdentifiers, getLang, isCallOf, isDts, isExpressionType, isFunctionType, isIdentifierOf, isLiteralType, isReferenced, isTs, isTypeOf, locateTrailingComma, resolveIdentifier, resolveLiteral, resolveObjectKey, resolveString, resolveTemplateLiteral, unwrapTSNode, walkAST, walkASTAsync, walkASTSetup, walkExportDeclaration, walkImportDeclaration };
export { type AttachedScope, type ExportBinding, type GetNode, type ImportBinding, type LiteralUnion, type NodeType, type ObjectPropertyLike, type ParseResult, REGEX_DTS, REGEX_LANG_JSX, REGEX_LANG_TS, TS_NODE_TYPES, type WithScope, attachScopes, babelParse, babelParseExpression, createStringLiteral, createTSLiteralType, createTSUnionType, escapeKey, extractIdentifiers, getLang, isCallOf, isDts, isExpressionType, isFunctionType, isIdentifierOf, isLiteralType, isReferenced, isTs, isTypeOf, locateTrailingComma, resolveIdentifier, resolveLiteral, resolveObjectKey, resolveString, resolveTemplateLiteral, unwrapTSNode, walkAST, walkASTAsync, walkExportDeclaration, walkImportDeclaration };
// src/check.ts
function isTypeOf(node, types) {
if (!node)
return false;
if (!node) return false;
return [].concat(types).some((type) => {

@@ -200,4 +199,3 @@ if (type === "Function") {

function getLang(filename) {
if (isDts(filename))
return "dts";
if (isDts(filename)) return "dts";
return extname(filename).replace(/^\./, "").replace(/\?.*$/, "");

@@ -221,6 +219,4 @@ }

const char = code[i];
if (["}", ")"].includes(char))
return -1;
if (char === ",")
return i;
if (["}", ")"].includes(char)) return -1;
if (char === ",") return i;
i++;

@@ -284,7 +280,5 @@ }

function resolveString(node, computed = false) {
if (typeof node === "string")
return node;
if (typeof node === "string") return node;
else if (node.type === "Identifier") {
if (computed)
throw new TypeError("Invalid Identifier");
if (computed) throw new TypeError("Invalid Identifier");
return node.name;

@@ -359,4 +353,3 @@ } else if (node.type === "PrivateName") {

case "Identifier":
if (!computed)
return raw ? `"${key.name}"` : key.name;
if (!computed) return raw ? `"${key.name}"` : key.name;
throw "Cannot resolve computed Identifier";

@@ -463,6 +456,4 @@ default:

this.replacement = _replacement;
if (skipped)
return node;
if (removed)
return null;
if (skipped) return node;
if (removed) return null;
}

@@ -507,4 +498,3 @@ let key;

this.should_remove = _should_remove;
if (removed)
return null;
if (removed) return null;
}

@@ -569,6 +559,4 @@ }

this.replacement = _replacement;
if (skipped)
return node;
if (removed)
return null;
if (skipped) return node;
if (removed) return null;
}

@@ -613,4 +601,3 @@ let key;

this.should_remove = _should_remove;
if (removed)
return null;
if (removed) return null;
}

@@ -638,41 +625,4 @@ }

var walkASTAsync = asyncWalk;
async function walkASTSetup(node, cb) {
const callbacks = {
enter: [],
leave: []
};
function getFilter(types) {
if (typeof types === "function")
return types;
return (node2) => isTypeOf(node2, Array.isArray(types) ? types : [types]);
}
const setup = {
onEnter(type, cb2) {
callbacks.enter.push({ filter: getFilter(type), cb: cb2 });
},
onLeave(type, cb2) {
callbacks.leave.push({ filter: getFilter(type), cb: cb2 });
}
};
await cb(setup);
return walkASTAsync(node, {
async enter(...args) {
for (const { filter, cb: cb2 } of callbacks.enter) {
if (!filter.apply(this, args))
continue;
await cb2?.apply(this, args);
}
},
async leave(...args) {
for (const { filter, cb: cb2 } of callbacks.leave) {
if (!filter.apply(this, args))
continue;
await cb2?.apply(this, args);
}
}
});
}
function walkImportDeclaration(imports, node) {
if (node.importKind === "type")
return;
if (node.importKind === "type") return;
const source = node.source.value;

@@ -773,4 +723,3 @@ for (const specifier of node.specifiers) {

for (const element of param.elements) {
if (element)
extractors[element.type](names, element);
if (element) extractors[element.type](names, element);
}

@@ -893,4 +842,3 @@ },

const node = n;
if (node[propertyName])
scope = scope.parent;
if (node[propertyName]) scope = scope.parent;
}

@@ -923,4 +871,3 @@ });

function escapeKey(rawKey) {
if (String(+rawKey) === rawKey)
return rawKey;
if (String(+rawKey) === rawKey) return rawKey;
try {

@@ -966,5 +913,4 @@ const node = parseExpression2(`({${rawKey}: 1})`);

walkASTAsync,
walkASTSetup,
walkExportDeclaration,
walkImportDeclaration
};

20

package.json
{
"name": "ast-kit",
"version": "0.12.2",
"version": "1.0.0",
"description": "A toolkit for easy Babel AST generation and manipulation.",

@@ -32,20 +32,20 @@ "type": "module",

"dependencies": {
"@babel/parser": "^7.24.6",
"@babel/parser": "^7.24.7",
"pathe": "^1.1.2"
},
"devDependencies": {
"@babel/types": "^7.24.6",
"@sxzz/eslint-config": "^3.11.0",
"@babel/types": "^7.24.7",
"@sxzz/eslint-config": "^3.13.0",
"@sxzz/prettier-config": "^2.0.2",
"@types/node": "^20.13.0",
"@types/node": "^20.14.8",
"@vitest/coverage-v8": "^1.6.0",
"@vitest/ui": "^1.6.0",
"bumpp": "^9.4.1",
"eslint": "^9.3.0",
"eslint": "^9.5.0",
"estree-walker": "^3.0.3",
"fast-glob": "^3.3.2",
"prettier": "^3.2.5",
"tsup": "^8.0.2",
"tsx": "^4.11.0",
"typescript": "5.5.0-beta",
"prettier": "^3.3.2",
"tsup": "^8.1.0",
"tsx": "^4.15.7",
"typescript": "5.5.2",
"vitest": "^1.6.0"

@@ -52,0 +52,0 @@ },

@@ -5,7 +5,10 @@ # ast-kit [![npm](https://img.shields.io/npm/v/ast-kit.svg)](https://npmjs.com/package/ast-kit) [![JSR](https://jsr.io/badges/@sxzz/ast-kit)](https://jsr.io/@sxzz/ast-kit) [![Unit Test](https://github.com/sxzz/ast-kit/actions/workflows/unit-test.yml/badge.svg)](https://github.com/sxzz/ast-kit/actions/workflows/unit-test.yml) [![codecov](https://codecov.io/gh/sxzz/ast-kit/graph/badge.svg?token=MHTCPNMZAK)](https://codecov.io/gh/sxzz/ast-kit)

[API Reference](https://paka.dev/npm/ast-kit/api)
[📖 API Reference](https://jsr.io/@sxzz/ast-kit/doc)
## Requirements
## Features
- Node.js >= 16.14.0
- **😇 Easy to Use**: Parse and resolve Babel AST with a straightforward API.
- **🦾 Type-Friendly**: Built in TypeScript with complete type definitions.
- **🧪 Thoroughly Tested**: 95% test coverage.
- **🪶 Lightweight**: Tree-shakable with only two dependencies.

@@ -18,2 +21,8 @@ ## Install

Requires Node.js 16.14.0 or higher.
## AST Explorer
You can explore your code's AST using the [AST Explorer](https://ast.sxzz.moe/).
## Sponsors

@@ -20,0 +29,0 @@

Sorry, the diff of this file is not supported yet

Sorry, the diff of this file is not supported yet

SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap

Packages

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc