ast-walker-scope
Traverse Babel AST with scope information.
Last updated a year ago by sxzz .
MIT · Repository · Bugs · Original npm · Tarball · package.json
$ cnpm install ast-walker-scope 
SYNC missed versions from official npm registry.

ast-walker-scope npm

Unit Test

Traverse Babel AST with scope information.

Inherited from estree-walker.

Install

npm i ast-walker-scope

Usage

Basic Example

For a real example, you can refer to example.ts

import { walk } from 'ast-walker-scope'

const code = `
const a = 'root level'

{
  const a = 'second level'
  let secondLevel = true
  console.log(a, secondLevel)
}

var err = undefined
try {
} catch (err) {
  console.log(err)
}

console.log(a)
`.trim()

walk(code, {
  leave(this, node) {
    if (node.type === 'CallExpression') {
      console.log(`\nLevel: ${this.level}`)
      for (const [name, node] of Object.entries(this.scope)) {
        console.log(
          `variable ${name} is located at line ${node.loc?.start.line}, column ${node.loc?.start.column}`,
        )
      }
    }
  },
})

Output:

Level: 2
variable a is located at line 4, column 8
variable secondLevel is located at line 5, column 6

Level: 2
variable a is located at line 1, column 6
variable err is located at line 12, column 9

Level: 1
variable a is located at line 1, column 6
variable err is located at line 9, column 4

Typings

export type Scope = Record<string, Node>
export interface HookContext extends WalkerContext {
  // inherited from estree-walker
  skip: () => void
  remove: () => void
  replace: (node: Node) => void

  // arguments of estree-walker hook
  parent: Node
  key: string
  index: number

  // scope info
  scope: Scope
  scopes: Scope[]
  level: number
}

Sponsors

Credits

License

MIT License © 2022-PRESENT 三咲智子

Current Tags

  • 0.8.1                                ...           latest (a year ago)

18 Versions

  • 0.8.1                                ...           a year ago
  • 0.8.0                                ...           a year ago
  • 0.7.1                                ...           a year ago
  • 0.7.0                                ...           a year ago
  • 0.6.2                                ...           2 years ago
  • 0.6.1                                ...           2 years ago
  • 0.6.0                                ...           2 years ago
  • 0.5.0                                ...           3 years ago
  • 0.4.2                                ...           3 years ago
  • 0.4.1                                ...           3 years ago
  • 0.4.0                                ...           3 years ago
  • 0.3.1                                ...           3 years ago
  • 0.3.0                                ...           3 years ago
  • 0.2.3                                ...           4 years ago
  • 0.2.2                                ...           4 years ago
  • 0.2.1                                ...           4 years ago
  • 0.2.0                                ...           4 years ago
  • 0.1.0                                ...           4 years ago
Maintainers (1)
Downloads
Today 0
This Week 0
This Month 0
Last Day 0
Last Week 0
Last Month 6
Dependencies (2)
Dev Dependencies (13)

Copyright 2013 - present © cnpmjs.org | Home |