Huge News!Announcing our $40M Series B led by Abstract Ventures.Learn More
Socket
Sign inDemoInstall
Socket

ast-walker-scope

Package Overview
Dependencies
Maintainers
1
Versions
14
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

ast-walker-scope

Traverse Babel AST with scope information.

  • 0.6.2
  • latest
  • Source
  • npm
  • Socket score

Version published
Weekly downloads
539K
decreased by-0.68%
Maintainers
1
Weekly downloads
 
Created
Source

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 三咲智子

FAQs

Package last updated on 12 Aug 2024

Did you know?

Socket

Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.

Install

Related posts

SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap
  • Changelog

Packages

npm

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc