npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

estree-util-scope

v1.0.0

Published

Check what’s defined in an estree scope

Downloads

205

Readme

estree-util-scope

Build Coverage Downloads Size Sponsors Backers Chat

estree utility to check what’s defined in a scope.

Contents

What is this?

This package is a utility that tracks what’s defined in a scope.

When should I use this?

If you are walking an estree already and want to find out what’s defined, use this. If you have more complex scoping needs, see eslint-scope.

Install

This package is ESM only. In Node.js (version 16+), install with npm:

npm install estree-util-scope

In Deno with esm.sh:

import {createVisitors} from 'https://esm.sh/estree-util-scope@1'

In browsers with esm.sh:

<script type="module">
  import {createVisitors} from 'https://esm.sh/estree-util-scope@1?bundle'
</script>

Use

Say we have the following example.js:

/**
 * @import {Program} from 'estree'
 */

import {Parser} from 'acorn'
import {createVisitors} from 'estree-util-scope'
import {walk} from 'estree-walker'

const tree = /** @type {Program} */ (
  Parser.parse('import {a} from "b"; const c = 1', {
    ecmaVersion: 'latest',
    sourceType: 'module'
  })
)
const visitors = createVisitors()

walk(tree, {enter: visitors.enter, leave: visitors.exit})

console.log(visitors.scopes.at(-1))

…now running node example.js yields:

{ block: false, defined: [ 'a', 'c' ] }

API

Scope

Scope.

Fields
  • block (boolean) — whether this is a block scope or not; blocks are things made by for and try and if; non-blocks are functions and the top-level scope
  • defined (Array<string>) — identifiers that are defined in this scope

Visitors

State to track what’s defined; contains enter, exit callbacks you must call and scopes.

Fields
  • enter ((node: Node) => undefined) — callback you must call when entering a node
  • exit ((node: Node) => undefined) — callback you must call when exiting (leaving) a node
  • scopes ([topLevel: Scope, ...rest: Scope[]]) — list of scopes; the first scope is the top-level scope; the last scope is the current scope

createVisitors()

Create state to track what’s defined.

Parameters

There are no parameters.

Returns

State (Visitors).

Examples

Example: just the top scope

Sometimes, you only care about a top-scope. Or otherwise want to skip a node. How to do this depends on how you walk the tree. With estree-walker, you can skip by calling this.skip.

/**
 * @import {Program} from 'estree'
 */

import {Parser} from 'acorn'
import {createVisitors} from 'estree-util-scope'
import {walk} from 'estree-walker'

const tree = /** @type {Program} */ (
  Parser.parse(
    'function a(b) { var c = 1; if (d) { var e = 2 } }; if (f) { var g = 2 }',
    {ecmaVersion: 'latest'}
  )
)
const visitors = createVisitors()

walk(tree, {
  enter(node) {
    visitors.enter(node)

    if (
      node.type === 'ArrowFunctionExpression' ||
      node.type === 'FunctionDeclaration' ||
      node.type === 'FunctionExpression'
    ) {
      this.skip()
      visitors.exit(node) // Call the exit handler manually.
    }
  },
  leave: visitors.exit
})

console.log(visitors.scopes.at(-1))

…yields:

{ block: false, defined: [ 'a', 'g' ] }

Compatibility

Projects maintained by the unified collective are compatible with maintained versions of Node.js.

When we cut a new major release, we drop support for unmaintained versions of Node. This means we try to keep the current release line, estree-util-scope@1, compatible with Node.js 16.

Related

Security

This package is safe.

Contribute

See contributing.md in syntax-tree/.github for ways to get started. See support.md for ways to get help.

This project has a code of conduct. By interacting with this repository, organization, or community you agree to abide by its terms.

License

MIT © Titus Wormer