Walk an AST from SWC and visit each node type.
- AST is as defined by
@swc/types
.
swc-walk
wraps acorn-walk
walkers.
import { simple } from 'swc-walk'
import { parseSync } from '@swc/core'
import assert from 'node:assert/strict'
type WalkState = {
[k: string]: string
}
const state = {
search: 'const foo',
}
const ast = parseSync('const foo: string = "bar"', { syntax: 'typescript' })
simple<WalkState>(
ast,
{
VariableDeclaration(node, state) {
assert.equal(node.type, 'VariableDeclaration')
assert.ok(typeof state.search === 'string')
},
},
undefined,
state,
)