# ast-walker-scope

> Traverse Babel AST with scope information.

Latest version **0.10.0** (published 2026-06-16) · MIT license · 0 weekly downloads

## Install

```sh
npm install ast-walker-scope
pnpm add ast-walker-scope
yarn add ast-walker-scope
bun add ast-walker-scope
```

## Health

**Score 55/100 (C)** — status: active.

Positive: esm support; no vulnerabilities; has provenance.

Warnings: low downloads; no types; pre 1.0.

## Facts

| | |
|---|---|
| Version | 0.10.0 |
| Published | 2026-06-16 |
| First published | 2022-07-29 |
| Weekly downloads | 0 |
| License | MIT |
| TypeScript types | none |
| Module format | ESM |
| Node | ^22.18.0 \|\| >=24.11.0 |
| Dependencies | 3 |
| Unpacked size | 12.5 KB |
| Known vulnerabilities | 0 |
| Install scripts | no |
| Provenance | attested (GitHub Actions) |
| Author | Kevin Deng <sxzz@sxzz.moe> |
| Maintainers | sxzz |

## Links

- npm: https://www.npmjs.com/package/ast-walker-scope
- Repository: https://github.com/sxzz/ast-walker-scope
- Homepage: https://github.com/sxzz/ast-walker-scope#readme
- Issues: https://github.com/sxzz/ast-walker-scope/issues
- Funding: https://github.com/sponsors/sxzz
- npm.io page: https://npm.io/package/ast-walker-scope

## Dependencies (3)

- [ast-kit](https://npm.io/package/ast-kit.md) ^3.0.0
- [@babel/types](https://npm.io/package/@babel/types.md) ^8.0.0
- [@babel/parser](https://npm.io/package/@babel/parser.md) ^8.0.0

## Recent versions

- 0.10.0 (latest) — 2026-06-16
- 0.9.0 — 2026-04-13
- 0.8.3 — 2025-10-06
- 0.8.2 — 2025-08-17
- 0.8.1 — 2025-05-09
- 0.8.0 — 2025-05-08
- 0.7.1 — 2025-03-05
- 0.7.0 — 2025-02-15
- 0.6.2 — 2024-08-12
- 0.6.1 — 2024-03-16
- 0.6.0 — 2024-01-22
- 0.5.0 — 2023-08-02
- 0.4.2 — 2023-06-06
- 0.4.1 — 2023-03-23
- 0.4.0 — 2023-01-27
- … 7 more at https://npm.io/package/ast-walker-scope/versions

## README

# ast-walker-scope

[![npm version][npm-version-src]][npm-version-href]
[![npm downloads][npm-downloads-src]][npm-downloads-href]
[![Unit Test][unit-test-src]][unit-test-href]

Traverse Babel AST with scope information.

Inherited from [estree-walker](https://github.com/Rich-Harris/estree-walker).

## Install

```bash
npm i ast-walker-scope
```

## Usage

### Basic Example

For a real example, you can refer to [example.ts](./example.ts)

```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

```ts
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

<p align="center">
  <a href="https://cdn.jsdelivr.net/gh/sxzz/sponsors/sponsors.svg">
    <img src='https://cdn.jsdelivr.net/gh/sxzz/sponsors/sponsors.svg'/>
  </a>
</p>

## Credits

- [@vue/reactivity-transform](https://github.com/vuejs/core/blob/v3.2.37/packages/reactivity-transform/src/reactivityTransform.ts) - almost copy-like referenced

## License

[MIT](./LICENSE) License © 2022-PRESENT [三咲智子](https://github.com/sxzz)

<!-- Badges -->

[npm-version-src]: https://img.shields.io/npm/v/ast-walker-scope.svg
[npm-version-href]: https://npmjs.com/package/ast-walker-scope
[npm-downloads-src]: https://img.shields.io/npm/dm/ast-walker-scope
[npm-downloads-href]: https://www.npmcharts.com/compare/ast-walker-scope?interval=30
[unit-test-src]: https://github.com/sxzz/ast-walker-scope/actions/workflows/unit-test.yml/badge.svg
[unit-test-href]: https://github.com/sxzz/ast-walker-scope/actions/workflows/unit-test.yml

---
_Source: https://npm.io/package/ast-walker-scope · Machine-readable twin of the npm.io package page. Health data is recomputed on every publish._
