import { asyncWalk, walk } from 'estree-walker' import { isExpressionType, isForStatement, isFunctionType, isIdentifier, isReferencedIdentifier, } from './check.ts' import { extractIdentifiers } from './extract.ts' import { resolveString } from './resolve.ts' import { TS_NODE_TYPES } from './utils.ts' import type { LiteralUnion } from './types.ts' import type * as t from '@babel/types' interface WalkThis { skip: () => void remove: () => void replace: (node: T) => void } type WalkCallback = ( this: WalkThis, node: T, parent: T | null | undefined, key: string | null | undefined, index: number | null | undefined, ) => R interface WalkHandlers { enter?: WalkCallback leave?: WalkCallback } /** * Walks the AST and applies the provided handlers. * * @template T - The type of the AST node. * @param {T} node - The root node of the AST. * @param {WalkHandlers} hooks - The handlers to be applied during the walk. * @returns {T | null} - The modified AST node or null if the node is removed. */ export const walkAST: ( node: NoInfer, hooks: WalkHandlers, ) => T | null = walk as any /** * Asynchronously walks the AST starting from the given node, * applying the provided handlers to each node encountered. * * @template T - The type of the AST node. * @param {T} node - The root node of the AST. * @param {WalkHandlers>} handlers - The handlers to be applied to each node. * @returns {Promise} - A promise that resolves to the modified AST or null if the AST is empty. */ export const walkASTAsync: ( node: NoInfer, handlers: WalkHandlers>, ) => Promise = asyncWalk as any export interface ImportBinding { local: string imported: LiteralUnion<'*' | 'default'> source: string specifier: t.ImportSpecifier | t.ImportDefaultSpecifier | t.ImportNamespaceSpecifier isType: boolean } /** * Walks through an ImportDeclaration node and populates the provided imports object. * * @param imports - The object to store the import bindings. * @param node - The ImportDeclaration node to walk through. */ export function walkImportDeclaration( imports: Record, node: t.ImportDeclaration, ): void { if (node.importKind === 'type') return const source = node.source.value for (const specifier of node.specifiers) { const isType = specifier.type === 'ImportSpecifier' && specifier.importKind === 'type' const local = specifier.local.name const imported = specifier.type === 'ImportSpecifier' ? resolveString(specifier.imported) : specifier.type === 'ImportNamespaceSpecifier' ? '*' : 'default' imports[local] = { source, local, imported, specifier, isType, } } } /** * Represents an export binding. */ export interface ExportBinding { local: string exported: LiteralUnion<'*' | 'default'> isType: boolean source: string | null specifier: | t.ExportSpecifier | t.ExportDefaultSpecifier | t.ExportNamespaceSpecifier | null declaration: t.Declaration | t.ExportDefaultDeclaration['declaration'] | null } /** * Walks through an ExportDeclaration node and populates the exports object with the relevant information. * @param exports - The object to store the export information. * @param node - The ExportDeclaration node to process. */ export function walkExportDeclaration( exports: Record, node: t.ExportDeclaration, ): void { let local: ExportBinding['local'] let exported: ExportBinding['exported'] let isType: ExportBinding['isType'] let source: ExportBinding['source'] let specifier: ExportBinding['specifier'] let declaration: ExportBinding['declaration'] function setExport() { exports[exported] = { source, local, exported, specifier, isType, declaration, } } if (node.type === 'ExportNamedDeclaration') { if (node.specifiers.length) { for (const s of node.specifiers) { const isExportSpecifier = s.type === 'ExportSpecifier' isType = node.exportKind === 'type' || (isExportSpecifier && s.exportKind === 'type') local = isExportSpecifier ? resolveString(s.local) : s.type === 'ExportNamespaceSpecifier' ? '*' : 'default' source = node.source ? node.source.value : null exported = resolveString(s.exported) declaration = null specifier = s setExport() } } else if (!node.specifiers.length && node.declaration) { // TODO: handle other nodeType /* v8 ignore else */ if (node.declaration.type === 'VariableDeclaration') { for (const decl of node.declaration.declarations) { /* v8 ignore if */ if (decl.id.type !== 'Identifier') { // TODO destructuring continue } local = resolveString(decl.id) source = null exported = local isType = node.exportKind === 'type' declaration = node.declaration specifier = null setExport() } } else if ( 'id' in node.declaration && node.declaration.id && node.declaration.id.type === 'Identifier' ) { local = resolveString(node.declaration.id) source = null exported = local isType = node.exportKind === 'type' declaration = node.declaration specifier = null setExport() } else { // TODO handle other nodeType } } return } else if (node.type === 'ExportDefaultDeclaration') { if (isExpressionType(node.declaration)) { local = 'name' in node.declaration ? node.declaration.name : 'default' } else { local = resolveString(node.declaration.id || 'default') } source = null exported = 'default' isType = false declaration = node.declaration specifier = null } else { local = '*' source = resolveString(node.source) exported = '*' isType = node.exportKind === 'type' specifier = null declaration = null } setExport() } /** * 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 */ export function walkIdentifiers( root: t.Node, onIdentifier: ( node: t.Identifier, parent: t.Node | null | undefined, parentStack: t.Node[], isReference: boolean, isLocal: boolean, ) => void, includeAll = false, parentStack: t.Node[] = [], knownIds: Record = Object.create(null), ): void { const rootExp = root.type === 'Program' ? root.body[0].type === 'ExpressionStatement' && root.body[0].expression : root walkAST(root, { enter(node: t.Node & { scopeIds?: Set }, parent) { parent && parentStack.push(parent) if ( parent && parent.type.startsWith('TS') && !TS_NODE_TYPES.includes(parent.type as any) ) { 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' ) { // mark property in destructure pattern ;(node as any).inPattern = true } else if (isFunctionType(node)) { /* v8 ignore if */ if (node.scopeIds) { node.scopeIds.forEach((id) => markKnownIds(id, knownIds)) } else { // walk function expressions and add its arguments to known identifiers // so that we don't prefix them walkFunctionParams(node, (id) => markScopeIdentifier(node, id, knownIds), ) } } else if (node.type === 'BlockStatement') { /* v8 ignore if */ if (node.scopeIds) { node.scopeIds.forEach((id) => markKnownIds(id, knownIds)) } else { // #3445 record block-level local variables 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: t.Node & { scopeIds?: Set }, parent) { parent && parentStack.pop() if (node !== rootExp && node.scopeIds) { for (const id of node.scopeIds) { knownIds[id]-- if (knownIds[id] === 0) { delete knownIds[id] } } } }, }) } export function walkFunctionParams( node: t.Function, onIdent: (id: t.Identifier) => void, ): void { for (const p of node.params) { for (const id of extractIdentifiers(p)) { onIdent(id) } } } export function walkBlockDeclarations( block: t.BlockStatement | t.Program, onIdent: (node: t.Identifier) => void, ): void { 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 if */ if (stmt.declare || !stmt.id) continue onIdent(stmt.id) } else if (isForStatement(stmt)) { walkForStatement(stmt, true, onIdent) } } } function walkForStatement( stmt: t.ForStatement | t.ForOfStatement | t.ForInStatement, isVar: boolean, onIdent: (id: t.Identifier) => void, ) { 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: string, knownIds: Record) { if (name in knownIds) { knownIds[name]++ } else { knownIds[name] = 1 } } function markScopeIdentifier( node: t.Node & { scopeIds?: Set }, child: t.Identifier, knownIds: Record, ) { const { name } = child /* v8 ignore if */ if (node.scopeIds && node.scopeIds.has(name)) { return } markKnownIds(name, knownIds) ;(node.scopeIds || (node.scopeIds = new Set())).add(name) }