Comparing version 1.1.0 to 1.2.0
import * as t from '@babel/types'; | ||
import { Node } from '@babel/types'; | ||
import { ParseResult as ParseResult$1, ParserOptions } from '@babel/parser'; | ||
import { ParserOptions, ParseResult } from '@babel/parser'; | ||
@@ -38,3 +38,3 @@ /** | ||
*/ | ||
declare function isIdentifierOf(node: t.Node | undefined | null, test: string | string[]): node is t.Identifier; | ||
declare function isIdentifierOf(node: t.Node | undefined | null, test: string | string[] | ((id: string) => boolean)): node is t.Identifier; | ||
/** | ||
@@ -140,6 +140,10 @@ * Checks if the given node is a literal type. | ||
type ParseResult<T> = ParseResult$1<T> & { | ||
comments?: t.Comment[] | null; | ||
}; | ||
/** | ||
* Gets the Babel parser options for the given language. | ||
* @param lang - The language of the code (optional). | ||
* @param options - The parser options (optional). | ||
* @returns The Babel parser options for the given language. | ||
*/ | ||
declare function getBabelParserOptions(lang?: string, options?: ParserOptions): ParserOptions; | ||
/** | ||
* Parses the given code using Babel parser. | ||
@@ -152,3 +156,3 @@ * | ||
*/ | ||
declare function babelParse(code: string, lang?: string, options?: ParserOptions): ParseResult<t.Program>; | ||
declare function babelParse(code: string, lang?: string, options?: ParserOptions): t.Program & Omit<ParseResult<t.File>, 'type' | 'program'>; | ||
/** | ||
@@ -325,2 +329,2 @@ * Parses the given code using the Babel parser as an expression. | ||
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 }; | ||
export { type AttachedScope, type ExportBinding, type GetNode, type ImportBinding, type LiteralUnion, type NodeType, type ObjectPropertyLike, REGEX_DTS, REGEX_LANG_JSX, REGEX_LANG_TS, TS_NODE_TYPES, type WithScope, attachScopes, babelParse, babelParseExpression, createStringLiteral, createTSLiteralType, createTSUnionType, escapeKey, extractIdentifiers, getBabelParserOptions, getLang, isCallOf, isDts, isExpressionType, isFunctionType, isIdentifierOf, isLiteralType, isReferenced, isTs, isTypeOf, locateTrailingComma, resolveIdentifier, resolveLiteral, resolveObjectKey, resolveString, resolveTemplateLiteral, tryResolveIdentifier, unwrapTSNode, walkAST, walkASTAsync, walkExportDeclaration, walkImportDeclaration }; |
@@ -17,6 +17,6 @@ // src/check.ts | ||
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)); | ||
return !!node && node.type === "CallExpression" && isIdentifierOf(node.callee, test); | ||
} | ||
function isIdentifierOf(node, test) { | ||
return !!node && node.type === "Identifier" && (typeof test === "string" ? node.name === test : test.includes(node.name)); | ||
return !!node && node.type === "Identifier" && match(node.name, test); | ||
} | ||
@@ -43,2 +43,7 @@ function isLiteralType(node) { | ||
} | ||
function match(value, test) { | ||
if (Array.isArray(test)) return test.includes(value); | ||
if (typeof test === "function") return test(value); | ||
return value === test; | ||
} | ||
function isReferenced(node, parent, grandparent) { | ||
@@ -289,3 +294,3 @@ switch (parent.type) { | ||
} | ||
function getParserOptions(lang, options = {}) { | ||
function getBabelParserOptions(lang, options = {}) { | ||
const plugins = [...options.plugins || []]; | ||
@@ -320,7 +325,7 @@ if (isTs(lang)) { | ||
function babelParse(code, lang, options = {}) { | ||
const { program, errors, comments } = parse( | ||
const { program, type, ...rest } = parse( | ||
code, | ||
getParserOptions(lang, options) | ||
getBabelParserOptions(lang, options) | ||
); | ||
return { ...program, errors, comments }; | ||
return { ...program, ...rest }; | ||
} | ||
@@ -330,3 +335,3 @@ function babelParseExpression(code, lang, options = {}) { | ||
code, | ||
getParserOptions(lang, options) | ||
getBabelParserOptions(lang, options) | ||
); | ||
@@ -947,2 +952,3 @@ } | ||
extractIdentifiers, | ||
getBabelParserOptions, | ||
getLang, | ||
@@ -949,0 +955,0 @@ isCallOf, |
{ | ||
"name": "ast-kit", | ||
"version": "1.1.0", | ||
"version": "1.2.0", | ||
"description": "A toolkit for easy Babel AST generation and manipulation.", | ||
@@ -32,14 +32,14 @@ "type": "module", | ||
"dependencies": { | ||
"@babel/parser": "^7.25.3", | ||
"@babel/parser": "^7.25.6", | ||
"pathe": "^1.1.2" | ||
}, | ||
"devDependencies": { | ||
"@babel/types": "^7.25.2", | ||
"@sxzz/eslint-config": "^3.17.1", | ||
"@babel/types": "^7.25.6", | ||
"@sxzz/eslint-config": "^4.2.0", | ||
"@sxzz/prettier-config": "^2.0.2", | ||
"@types/node": "^20.15.0", | ||
"@vitest/coverage-v8": "^2.0.5", | ||
"@vitest/ui": "^2.0.5", | ||
"bumpp": "^9.5.1", | ||
"eslint": "^9.9.0", | ||
"@types/node": "^20.16.5", | ||
"@vitest/coverage-v8": "^2.1.1", | ||
"@vitest/ui": "^2.1.1", | ||
"bumpp": "^9.5.2", | ||
"eslint": "^9.10.0", | ||
"estree-walker": "^3.0.3", | ||
@@ -49,5 +49,5 @@ "fast-glob": "^3.3.2", | ||
"tsup": "^8.2.4", | ||
"tsx": "^4.17.0", | ||
"typescript": "5.5.4", | ||
"vitest": "^2.0.5" | ||
"tsx": "^4.19.1", | ||
"typescript": "5.6.2", | ||
"vitest": "^2.1.1" | ||
}, | ||
@@ -54,0 +54,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
88716
2248
Updated@babel/parser@^7.25.6