Skip to content

morganney/swc-walk

Repository files navigation

CI codecov NPM version

Walk an AST from SWC and visit each node type.

Requirements

Example

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,
)