Comparing version 0.5.2 to 0.6.1
@@ -0,1 +1,28 @@ | ||
// src/check.ts | ||
function isTypeOf(node, types) { | ||
if (!node) | ||
return false; | ||
return types.some((type) => { | ||
if (type === "Function") { | ||
return isFunctionType(node); | ||
} else if (type === "Literal") { | ||
return isLiteralType(node); | ||
} else { | ||
return node.type === type; | ||
} | ||
}); | ||
} | ||
function isCallOf(node, test) { | ||
return !!node && node.type === "CallExpression" && node.callee.type === "Identifier" && (typeof test === "string" ? node.callee.name === test : Array.isArray(test) ? test.includes(node.callee.name) : test(node.callee.name)); | ||
} | ||
function isVariableOf(node, test) { | ||
return !!node && node.type === "Identifier" && (typeof test === "string" ? node.name === test : test.includes(node.name)); | ||
} | ||
function isLiteralType(node) { | ||
return !!node && node.type.endsWith("Literal"); | ||
} | ||
function isFunctionType(node) { | ||
return !!node && /Function(?:Expression|Declaration)$|Method$/.test(node.type); | ||
} | ||
// src/lang.ts | ||
@@ -39,57 +66,2 @@ import pathe from "pathe"; | ||
// src/utils.ts | ||
import { parseExpression } from "@babel/parser"; | ||
function isTypeOf(node, types) { | ||
if (!node) | ||
return false; | ||
return types.some((type) => { | ||
if (type === "Function") { | ||
return isFunctionType(node); | ||
} else if (type === "Literal") { | ||
return isLiteralType(node); | ||
} else { | ||
return node.type === type; | ||
} | ||
}); | ||
} | ||
function isCallOf(node, test) { | ||
return !!node && node.type === "CallExpression" && node.callee.type === "Identifier" && (typeof test === "string" ? node.callee.name === test : Array.isArray(test) ? test.includes(node.callee.name) : test(node.callee.name)); | ||
} | ||
function isLiteralType(node) { | ||
return !!node && node.type.endsWith("Literal"); | ||
} | ||
function isFunctionType(node) { | ||
return !!node && /Function(?:Expression|Declaration)$|Method$/.test(node.type); | ||
} | ||
var TS_NODE_TYPES = [ | ||
"TSAsExpression", | ||
// foo as number | ||
"TSTypeAssertion", | ||
// (<number>foo) | ||
"TSNonNullExpression", | ||
// foo! | ||
"TSInstantiationExpression", | ||
// foo<string> | ||
"TSSatisfiesExpression" | ||
// foo satisfies T | ||
]; | ||
function unwrapTSNode(node) { | ||
if (isTypeOf(node, TS_NODE_TYPES)) { | ||
return unwrapTSNode(node.expression); | ||
} else { | ||
return node; | ||
} | ||
} | ||
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); | ||
} | ||
// src/resolve.ts | ||
@@ -167,2 +139,35 @@ function resolveString(node, computed = false) { | ||
// src/utils.ts | ||
import { parseExpression } from "@babel/parser"; | ||
var TS_NODE_TYPES = [ | ||
"TSAsExpression", | ||
// foo as number | ||
"TSTypeAssertion", | ||
// (<number>foo) | ||
"TSNonNullExpression", | ||
// foo! | ||
"TSInstantiationExpression", | ||
// foo<string> | ||
"TSSatisfiesExpression" | ||
// foo satisfies T | ||
]; | ||
function unwrapTSNode(node) { | ||
if (isTypeOf(node, TS_NODE_TYPES)) { | ||
return unwrapTSNode(node.expression); | ||
} else { | ||
return node; | ||
} | ||
} | ||
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); | ||
} | ||
// node_modules/.pnpm/estree-walker@3.0.3/node_modules/estree-walker/src/walker.js | ||
@@ -357,2 +362,3 @@ var WalkerBase = class { | ||
isTypeOf, | ||
isVariableOf, | ||
resolveIdentifier, | ||
@@ -359,0 +365,0 @@ resolveLiteral, |
@@ -0,6 +1,16 @@ | ||
import { Function, Literal, Node, CallExpression, Identifier, Program, PrivateName, ThisExpression, Super, TemplateLiteral, MemberExpression, ImportSpecifier, ImportDefaultSpecifier, ImportNamespaceSpecifier, ImportDeclaration } from '@babel/types'; | ||
import { ParserOptions } from '@babel/parser'; | ||
import { Program, Identifier, Literal, PrivateName, ThisExpression, Super, TemplateLiteral, MemberExpression, Function, Node, CallExpression, ImportSpecifier, ImportDefaultSpecifier, ImportNamespaceSpecifier, ImportDeclaration } from '@babel/types'; | ||
import { AttachedScope } from '@rollup/pluginutils'; | ||
export { AttachedScope } from '@rollup/pluginutils'; | ||
type NodeType = Node['type'] | 'Function' | 'Literal'; | ||
type GetNode<K extends NodeType> = K extends 'Function' ? Function : K extends 'Literal' ? Literal : Extract<Node, { | ||
type: K; | ||
}>; | ||
declare function isTypeOf<K extends NodeType>(node: Node | undefined | null, types: Readonly<K[]>): node is GetNode<K>; | ||
declare function isCallOf(node: Node | null | undefined, test: string | string[] | ((id: string) => boolean)): node is CallExpression; | ||
declare function isVariableOf(node: Node | undefined | null, test: string | string[]): node is Identifier; | ||
declare function isLiteralType(node: Node | undefined | null): node is Literal; | ||
declare function isFunctionType(node: Node | undefined | null): node is Function; | ||
declare const REGEX_DTS: RegExp; | ||
@@ -26,10 +36,2 @@ declare const REGEX_LANG_TS: RegExp; | ||
type NodeType = Node['type'] | 'Function' | 'Literal'; | ||
type GetNode<K extends NodeType> = K extends 'Function' ? Function : K extends 'Literal' ? Literal : Extract<Node, { | ||
type: K; | ||
}>; | ||
declare function isTypeOf<K extends NodeType>(node: Node | undefined | null, types: Readonly<K[]>): node is GetNode<K>; | ||
declare function isCallOf(node: Node | null | undefined, test: string | string[] | ((id: string) => boolean)): node is CallExpression; | ||
declare function isLiteralType(node: Node | undefined | null): node is Literal; | ||
declare function isFunctionType(node: Node | undefined | null): node is Function; | ||
declare const TS_NODE_TYPES: readonly ["TSAsExpression", "TSTypeAssertion", "TSNonNullExpression", "TSInstantiationExpression", "TSSatisfiesExpression"]; | ||
@@ -59,2 +61,2 @@ declare function unwrapTSNode(node: Node): Node; | ||
export { GetNode, ImportBinding, REGEX_DTS, REGEX_LANG_JSX, REGEX_LANG_TS, TS_NODE_TYPES, WithScope, attachScopes, babelParse, escapeKey, getLang, isCallOf, isDts, isFunctionType, 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, escapeKey, getLang, isCallOf, isDts, isFunctionType, isLiteralType, isTs, isTypeOf, isVariableOf, resolveIdentifier, resolveLiteral, resolveString, resolveTemplateLiteral, unwrapTSNode, walkAST, walkImportDeclaration }; |
@@ -0,1 +1,28 @@ | ||
// src/check.ts | ||
function isTypeOf(node, types) { | ||
if (!node) | ||
return false; | ||
return types.some((type) => { | ||
if (type === "Function") { | ||
return isFunctionType(node); | ||
} else if (type === "Literal") { | ||
return isLiteralType(node); | ||
} else { | ||
return node.type === type; | ||
} | ||
}); | ||
} | ||
function isCallOf(node, test) { | ||
return !!node && node.type === "CallExpression" && node.callee.type === "Identifier" && (typeof test === "string" ? node.callee.name === test : Array.isArray(test) ? test.includes(node.callee.name) : test(node.callee.name)); | ||
} | ||
function isVariableOf(node, test) { | ||
return !!node && node.type === "Identifier" && (typeof test === "string" ? node.name === test : test.includes(node.name)); | ||
} | ||
function isLiteralType(node) { | ||
return !!node && node.type.endsWith("Literal"); | ||
} | ||
function isFunctionType(node) { | ||
return !!node && /Function(?:Expression|Declaration)$|Method$/.test(node.type); | ||
} | ||
// src/lang.ts | ||
@@ -39,57 +66,2 @@ import pathe from "pathe"; | ||
// src/utils.ts | ||
import { parseExpression } from "@babel/parser"; | ||
function isTypeOf(node, types) { | ||
if (!node) | ||
return false; | ||
return types.some((type) => { | ||
if (type === "Function") { | ||
return isFunctionType(node); | ||
} else if (type === "Literal") { | ||
return isLiteralType(node); | ||
} else { | ||
return node.type === type; | ||
} | ||
}); | ||
} | ||
function isCallOf(node, test) { | ||
return !!node && node.type === "CallExpression" && node.callee.type === "Identifier" && (typeof test === "string" ? node.callee.name === test : Array.isArray(test) ? test.includes(node.callee.name) : test(node.callee.name)); | ||
} | ||
function isLiteralType(node) { | ||
return !!node && node.type.endsWith("Literal"); | ||
} | ||
function isFunctionType(node) { | ||
return !!node && /Function(?:Expression|Declaration)$|Method$/.test(node.type); | ||
} | ||
var TS_NODE_TYPES = [ | ||
"TSAsExpression", | ||
// foo as number | ||
"TSTypeAssertion", | ||
// (<number>foo) | ||
"TSNonNullExpression", | ||
// foo! | ||
"TSInstantiationExpression", | ||
// foo<string> | ||
"TSSatisfiesExpression" | ||
// foo satisfies T | ||
]; | ||
function unwrapTSNode(node) { | ||
if (isTypeOf(node, TS_NODE_TYPES)) { | ||
return unwrapTSNode(node.expression); | ||
} else { | ||
return node; | ||
} | ||
} | ||
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); | ||
} | ||
// src/resolve.ts | ||
@@ -167,2 +139,35 @@ function resolveString(node, computed = false) { | ||
// src/utils.ts | ||
import { parseExpression } from "@babel/parser"; | ||
var TS_NODE_TYPES = [ | ||
"TSAsExpression", | ||
// foo as number | ||
"TSTypeAssertion", | ||
// (<number>foo) | ||
"TSNonNullExpression", | ||
// foo! | ||
"TSInstantiationExpression", | ||
// foo<string> | ||
"TSSatisfiesExpression" | ||
// foo satisfies T | ||
]; | ||
function unwrapTSNode(node) { | ||
if (isTypeOf(node, TS_NODE_TYPES)) { | ||
return unwrapTSNode(node.expression); | ||
} else { | ||
return node; | ||
} | ||
} | ||
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); | ||
} | ||
// node_modules/.pnpm/estree-walker@3.0.3/node_modules/estree-walker/src/walker.js | ||
@@ -357,2 +362,3 @@ var WalkerBase = class { | ||
isTypeOf, | ||
isVariableOf, | ||
resolveIdentifier, | ||
@@ -359,0 +365,0 @@ resolveLiteral, |
{ | ||
"name": "ast-kit", | ||
"version": "0.5.2", | ||
"version": "0.6.1", | ||
"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
39152
1158