Skip to content

Commit

Permalink
fix: dynamic import estree-walker
Browse files Browse the repository at this point in the history
  • Loading branch information
yuyinws committed Dec 23, 2024
1 parent bc97d10 commit 3d11f70
Show file tree
Hide file tree
Showing 2 changed files with 3 additions and 4 deletions.
2 changes: 1 addition & 1 deletion src/core/transform/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,7 @@ export async function transform(context: Context) {
}
}

walk(program, {
await walk(program, {
enter(node) {
if (isConsoleExpression(node)) {
const originalExpression = oxcMs.getSourceText(node.start, node.end)
Expand Down
5 changes: 2 additions & 3 deletions src/core/utils/walker.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,15 +4,14 @@ import type { SyncHandler } from 'estree-walker'

import type { CatchClause, ClassBody, Declaration, ExportSpecifier, Expression, ImportDefaultSpecifier, ImportNamespaceSpecifier, ImportSpecifier, MagicString, MethodDefinition, ModuleDeclaration, ObjectProperty, ParseResult, Pattern, PrivateIdentifier, Program, PropertyDefinition, SpreadElement, Statement, Super, SwitchCase, TemplateElement } from 'oxc-parser'

import { walk as _walk } from 'estree-walker'

/** estree also has AssignmentProperty, Identifier and Literal as possible node types */
export type Node = Declaration | Expression | ClassBody | CatchClause | MethodDefinition | ModuleDeclaration | ImportSpecifier | ImportDefaultSpecifier | ImportNamespaceSpecifier | ExportSpecifier | Pattern | PrivateIdentifier | Program | SpreadElement | Statement | Super | SwitchCase | TemplateElement | ObjectProperty | PropertyDefinition

type WalkerCallback = (this: ThisParameterType<SyncHandler>, node: Node, parent: Node | null, ctx: { key: string | number | symbol | null | undefined, index: number | null | undefined, ast: Program | Node, magicString?: MagicString | undefined }) => void

export function walk(input: Program | Node | ParseResult, callback: { enter?: WalkerCallback, leave?: WalkerCallback }) {
export async function walk(input: Program | Node | ParseResult, callback: { enter?: WalkerCallback, leave?: WalkerCallback }) {
const [ast, magicString] = 'magicString' in input ? [input.program, input.magicString] : [input]
const { walk: _walk } = await import('estree-walker')
return _walk(ast as unknown as ESTreeProgram | ESTreeNode, {
enter(node, parent, key, index) {
callback.enter?.call(this, node as Node, parent as Node | null, { key, index, ast, magicString })
Expand Down

0 comments on commit 3d11f70

Please sign in to comment.