Comparing version 1.0.1 to 1.1.0
@@ -185,4 +185,6 @@ import * as t from '@babel/types'; | ||
* @returns An array of resolved strings representing the identifier. | ||
* @throws TypeError If the identifier is invalid. | ||
*/ | ||
declare function resolveIdentifier(node: t.Identifier | t.PrivateName | t.MemberExpression | t.ThisExpression | t.Super | t.TSEntityName): string[]; | ||
declare function tryResolveIdentifier(...args: Parameters<typeof resolveIdentifier>): string[] | undefined; | ||
type ObjectPropertyLike = t.ObjectMethod | t.ObjectProperty | t.TSMethodSignature | t.TSPropertySignature | t.ImportAttribute; | ||
@@ -321,2 +323,2 @@ /** | ||
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 }; | ||
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, tryResolveIdentifier, unwrapTSNode, walkAST, walkASTAsync, walkExportDeclaration, walkImportDeclaration }; |
@@ -44,2 +44,5 @@ // src/check.ts | ||
switch (parent.type) { | ||
// yes: PARENT[NODE] | ||
// yes: NODE.child | ||
// no: parent.NODE | ||
case "MemberExpression": | ||
@@ -53,8 +56,19 @@ case "OptionalMemberExpression": | ||
return parent.object === node; | ||
// no: let NODE = init; | ||
// yes: let id = NODE; | ||
case "VariableDeclarator": | ||
return parent.init === node; | ||
// yes: () => NODE | ||
// no: (NODE) => {} | ||
case "ArrowFunctionExpression": | ||
return parent.body === node; | ||
// no: class { #NODE; } | ||
// no: class { get #NODE() {} } | ||
// no: class { #NODE() {} } | ||
// no: class { fn() { return this.#NODE; } } | ||
case "PrivateName": | ||
return false; | ||
// no: class { NODE() {} } | ||
// yes: class { [NODE]() {} } | ||
// no: class { foo(NODE) {} } | ||
case "ClassMethod": | ||
@@ -67,2 +81,6 @@ case "ClassPrivateMethod": | ||
return false; | ||
// yes: { [NODE]: "" } | ||
// no: { NODE: "" } | ||
// depends: { NODE } | ||
// depends: { key: NODE } | ||
case "ObjectProperty": | ||
@@ -73,2 +91,5 @@ if (parent.key === node) { | ||
return !grandparent || grandparent.type !== "ObjectPattern"; | ||
// no: class { NODE = value; } | ||
// yes: class { [NODE] = value; } | ||
// yes: class { key = NODE; } | ||
case "ClassProperty": | ||
@@ -82,13 +103,22 @@ case "ClassAccessorProperty": | ||
return parent.key !== node; | ||
// no: class NODE {} | ||
// yes: class Foo extends NODE {} | ||
case "ClassDeclaration": | ||
case "ClassExpression": | ||
return parent.superClass === node; | ||
// yes: left = NODE; | ||
// no: NODE = right; | ||
case "AssignmentExpression": | ||
return parent.right === node; | ||
// no: [NODE = foo] = []; | ||
// yes: [foo = NODE] = []; | ||
case "AssignmentPattern": | ||
return parent.right === node; | ||
// no: NODE: for (;;) {} | ||
case "LabeledStatement": | ||
return false; | ||
// no: try {} catch (NODE) {} | ||
case "CatchClause": | ||
return false; | ||
// no: function foo(...NODE) {} | ||
case "RestElement": | ||
@@ -99,8 +129,15 @@ return false; | ||
return false; | ||
// no: function NODE() {} | ||
// no: function foo(NODE) {} | ||
case "FunctionDeclaration": | ||
case "FunctionExpression": | ||
return false; | ||
// no: export NODE from "foo"; | ||
// no: export * as NODE from "foo"; | ||
case "ExportNamespaceSpecifier": | ||
case "ExportDefaultSpecifier": | ||
return false; | ||
// no: export { foo as NODE }; | ||
// yes: export { NODE as foo }; | ||
// no: export { NODE as foo } from "foo"; | ||
case "ExportSpecifier": | ||
@@ -111,2 +148,7 @@ if (grandparent?.source) { | ||
return parent.local === node; | ||
// no: import NODE from "foo"; | ||
// no: import * as NODE from "foo"; | ||
// no: import { NODE as foo } from "foo"; | ||
// no: import { foo as NODE } from "foo"; | ||
// no: import NODE from "bar"; | ||
case "ImportDefaultSpecifier": | ||
@@ -116,15 +158,27 @@ case "ImportNamespaceSpecifier": | ||
return false; | ||
// no: import "foo" assert { NODE: "json" } | ||
case "ImportAttribute": | ||
return false; | ||
// no: <div NODE="foo" /> | ||
case "JSXAttribute": | ||
return false; | ||
// no: [NODE] = []; | ||
// no: ({ NODE }) = []; | ||
case "ObjectPattern": | ||
case "ArrayPattern": | ||
return false; | ||
// no: new.NODE | ||
// no: NODE.target | ||
case "MetaProperty": | ||
return false; | ||
// yes: type X = { someProperty: NODE } | ||
// no: type X = { NODE: OtherType } | ||
case "ObjectTypeProperty": | ||
return parent.key !== node; | ||
// yes: enum X { Foo = NODE } | ||
// no: enum X { NODE } | ||
case "TSEnumMember": | ||
return parent.id !== node; | ||
// yes: { [NODE]: value } | ||
// no: { NODE: value } | ||
case "TSPropertySignature": | ||
@@ -313,2 +367,3 @@ if (parent.key === node) { | ||
return node.value; | ||
/* c8 ignore next 2 */ | ||
case "DecimalLiteral": | ||
@@ -352,2 +407,9 @@ return Number(node.value); | ||
} | ||
function tryResolveIdentifier(...args) { | ||
try { | ||
return resolveIdentifier(...args); | ||
} catch { | ||
return; | ||
} | ||
} | ||
function resolveObjectKey(node, raw = false) { | ||
@@ -748,7 +810,7 @@ const { key, computed } = node; | ||
}; | ||
var extractAssignedNames = function extractAssignedNames2(param) { | ||
function extractAssignedNames(param) { | ||
const names = []; | ||
extractors[param.type](names, param); | ||
return names; | ||
}; | ||
} | ||
var blockDeclarations = { | ||
@@ -790,5 +852,4 @@ const: true, | ||
walkAST(ast, { | ||
enter(n, parent) { | ||
const node = n; | ||
if (/(Function|Class)Declaration/.test(node.type)) { | ||
enter(node, parent) { | ||
if (/(?:Function|Class)Declaration/.test(node.type)) { | ||
scope.addDeclaration(node, false, false); | ||
@@ -815,3 +876,3 @@ } | ||
} | ||
if (/For(In|Of)?Statement/.test(node.type)) { | ||
if (/For(?:In|Of)?Statement/.test(node.type)) { | ||
newScope = new Scope({ | ||
@@ -843,4 +904,3 @@ parent: scope, | ||
}, | ||
leave(n) { | ||
const node = n; | ||
leave(node) { | ||
if (node[propertyName]) scope = scope.parent; | ||
@@ -912,2 +972,3 @@ } | ||
resolveTemplateLiteral, | ||
tryResolveIdentifier, | ||
unwrapTSNode, | ||
@@ -914,0 +975,0 @@ walkAST, |
{ | ||
"name": "ast-kit", | ||
"version": "1.0.1", | ||
"version": "1.1.0", | ||
"description": "A toolkit for easy Babel AST generation and manipulation.", | ||
@@ -32,21 +32,21 @@ "type": "module", | ||
"dependencies": { | ||
"@babel/parser": "^7.24.8", | ||
"@babel/parser": "^7.25.3", | ||
"pathe": "^1.1.2" | ||
}, | ||
"devDependencies": { | ||
"@babel/types": "^7.24.9", | ||
"@sxzz/eslint-config": "^3.15.0", | ||
"@babel/types": "^7.25.2", | ||
"@sxzz/eslint-config": "^3.17.1", | ||
"@sxzz/prettier-config": "^2.0.2", | ||
"@types/node": "^20.14.11", | ||
"@vitest/coverage-v8": "^2.0.4", | ||
"@vitest/ui": "^2.0.4", | ||
"bumpp": "^9.4.1", | ||
"eslint": "^9.7.0", | ||
"@types/node": "^20.15.0", | ||
"@vitest/coverage-v8": "^2.0.5", | ||
"@vitest/ui": "^2.0.5", | ||
"bumpp": "^9.5.1", | ||
"eslint": "^9.9.0", | ||
"estree-walker": "^3.0.3", | ||
"fast-glob": "^3.3.2", | ||
"prettier": "^3.3.3", | ||
"tsup": "^8.2.2", | ||
"tsx": "^4.16.2", | ||
"typescript": "5.5.3", | ||
"vitest": "^2.0.4" | ||
"tsup": "^8.2.4", | ||
"tsx": "^4.17.0", | ||
"typescript": "5.5.4", | ||
"vitest": "^2.0.5" | ||
}, | ||
@@ -53,0 +53,0 @@ "engines": { |
Sorry, the diff of this file is not supported yet
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
88087
2232
Updated@babel/parser@^7.25.3