{"_id":"estree-util-scope","_rev":"3912605","name":"estree-util-scope","description":"Check what’s defined in an estree scope","dist-tags":{"latest":"1.0.0"},"maintainers":[{"name":"kmck","email":"keith@mcknig.ht"},{"name":"wooorm","email":"tituswormer@gmail.com"}],"time":{"modified":"2025-09-01T01:15:38.000Z","created":"2024-08-29T14:50:30.092Z","1.0.0":"2024-08-29T14:50:30.092Z"},"users":{},"author":{"name":"Titus Wormer","email":"tituswormer@gmail.com","url":"https://wooorm.com"},"repository":{"type":"git","url":"git+https://github.com/syntax-tree/estree-util-scope.git"},"versions":{"1.0.0":{"name":"estree-util-scope","version":"1.0.0","description":"Check what’s defined in an estree scope","license":"MIT","keywords":["ast","ecmascript","estree","javascript","scope","tree"],"repository":{"type":"git","url":"git+https://github.com/syntax-tree/estree-util-scope.git"},"bugs":{"url":"https://github.com/syntax-tree/estree-util-scope/issues"},"funding":{"type":"opencollective","url":"https://opencollective.com/unified"},"author":{"name":"Titus Wormer","email":"tituswormer@gmail.com","url":"https://wooorm.com"},"contributors":[{"name":"Titus Wormer","email":"tituswormer@gmail.com","url":"https://wooorm.com"}],"sideEffects":false,"type":"module","exports":"./index.js","dependencies":{"@types/estree":"^1.0.0","devlop":"^1.0.0"},"devDependencies":{"@types/node":"^22.0.0","acorn":"^8.0.0","c8":"^10.0.0","estree-walker":"^3.0.0","prettier":"^3.0.0","remark-api":"^1.1.0","remark-cli":"^12.0.0","remark-preset-wooorm":"^10.0.0","type-coverage":"^2.0.0","typescript":"^5.0.0","xo":"^0.59.0"},"scripts":{"prepack":"npm run build && npm run format","build":"tsc --build --clean && tsc --build && type-coverage","format":"remark . -qfo && prettier . -w --log-level warn && xo --fix","test-api":"node --conditions development test.js","test-coverage":"c8 --100 --reporter lcov npm run test-api","test":"npm run build && npm run format && npm run test-coverage"},"prettier":{"bracketSpacing":false,"semi":false,"singleQuote":true,"tabWidth":2,"trailingComma":"none","useTabs":false},"remarkConfig":{"plugins":["remark-api","remark-preset-wooorm"]},"typeCoverage":{"atLeast":100,"detail":true,"ignoreCatch":true,"strict":true},"xo":{"overrides":[{"files":["**/*.d.ts"],"rules":{"@typescript-eslint/array-type":["error",{"default":"generic"}],"@typescript-eslint/ban-types":["error",{"extendDefaults":true}],"@typescript-eslint/consistent-type-definitions":["error","interface"]}}],"prettier":true,"rules":{"complexity":"off","unicorn/prefer-switch":"off"}},"_id":"estree-util-scope@1.0.0","gitHead":"32dae2048cea45545ea3d54525a3283135b8260b","types":"./index.d.ts","homepage":"https://github.com/syntax-tree/estree-util-scope#readme","_nodeVersion":"22.7.0","_npmVersion":"10.8.2","dist":{"shasum":"9cbdfc77f5cb51e3d9ed4ad9c4adbff22d43e585","size":5540,"noattachment":false,"key":"/estree-util-scope/-/estree-util-scope-1.0.0.tgz","tarball":"http://registry.cnpm.dingdandao.com/estree-util-scope/download/estree-util-scope-1.0.0.tgz"},"_npmUser":{"name":"wooorm","email":"tituswormer@gmail.com"},"directories":{},"maintainers":[{"name":"kmck","email":"keith@mcknig.ht"},{"name":"wooorm","email":"tituswormer@gmail.com"}],"_npmOperationalInternal":{"host":"s3://npm-registry-packages","tmp":"tmp/estree-util-scope_1.0.0_1724943029943_0.042160378709692026"},"_hasShrinkwrap":false,"_cnpmcore_publish_time":"2024-08-29T14:50:30.092Z","publish_time":1724943030092,"_source_registry_name":"default","_cnpm_publish_time":1724943030092}},"readme":"# estree-util-scope\n\n[![Build][build-badge]][build]\n[![Coverage][coverage-badge]][coverage]\n[![Downloads][downloads-badge]][downloads]\n[![Size][size-badge]][size]\n[![Sponsors][sponsors-badge]][collective]\n[![Backers][backers-badge]][collective]\n[![Chat][chat-badge]][chat]\n\n[estree][] utility to check what’s defined in a scope.\n\n## Contents\n\n* [What is this?](#what-is-this)\n* [When should I use this?](#when-should-i-use-this)\n* [Install](#install)\n* [Use](#use)\n* [API](#api)\n  * [`Scope`](#scope)\n  * [`Visitors`](#visitors)\n  * [`createVisitors()`](#createvisitors)\n* [Examples](#examples)\n  * [Example: just the top scope](#example-just-the-top-scope)\n* [Compatibility](#compatibility)\n* [Related](#related)\n* [Security](#security)\n* [Contribute](#contribute)\n* [License](#license)\n\n## What is this?\n\nThis package is a utility that tracks what’s defined in a scope.\n\n## When should I use this?\n\nIf you are walking an estree already and want to find out what’s defined,\nuse this.\nIf you have more complex scoping needs,\nsee [`eslint-scope`][github-eslint-scope].\n\n## Install\n\nThis package is [ESM only][esm].\nIn Node.js (version 16+), install with [npm][]:\n\n```sh\nnpm install estree-util-scope\n```\n\nIn Deno with [`esm.sh`][esmsh]:\n\n```js\nimport {createVisitors} from 'https://esm.sh/estree-util-scope@1'\n```\n\nIn browsers with [`esm.sh`][esmsh]:\n\n```html\n<script type=\"module\">\n  import {createVisitors} from 'https://esm.sh/estree-util-scope@1?bundle'\n</script>\n```\n\n## Use\n\nSay we have the following `example.js`:\n\n```js\n/**\n * @import {Program} from 'estree'\n */\n\nimport {Parser} from 'acorn'\nimport {createVisitors} from 'estree-util-scope'\nimport {walk} from 'estree-walker'\n\nconst tree = /** @type {Program} */ (\n  Parser.parse('import {a} from \"b\"; const c = 1', {\n    ecmaVersion: 'latest',\n    sourceType: 'module'\n  })\n)\nconst visitors = createVisitors()\n\nwalk(tree, {enter: visitors.enter, leave: visitors.exit})\n\nconsole.log(visitors.scopes.at(-1))\n```\n\n…now running `node example.js` yields:\n\n```js\n{ block: false, defined: [ 'a', 'c' ] }\n```\n\n## API\n\n### `Scope`\n\nScope.\n\n###### Fields\n\n* `block` (`boolean`)\n  — whether this is a block scope or not;\n  blocks are things made by `for` and `try` and `if`;\n  non-blocks are functions and the top-level scope\n* `defined` (`Array<string>`)\n  — identifiers that are defined in this scope\n\n### `Visitors`\n\nState to track what’s defined;\ncontains `enter`, `exit` callbacks you must call and `scopes`.\n\n###### Fields\n\n* `enter` (`(node: Node) => undefined`)\n  — callback you must call when entering a node\n* `exit` (`(node: Node) => undefined`)\n  — callback you must call when exiting (leaving) a node\n* `scopes` (`[topLevel: Scope, ...rest: Scope[]]`)\n  — list of scopes;\n  the first scope is the top-level scope;\n  the last scope is the current scope\n\n### `createVisitors()`\n\nCreate state to track what’s defined.\n\n###### Parameters\n\nThere are no parameters.\n\n###### Returns\n\nState (`Visitors`).\n\n## Examples\n\n### Example: just the top scope\n\nSometimes, you only care about a top-scope.\nOr otherwise want to skip a node.\nHow to do this depends on how you walk the tree.\nWith `estree-walker`,\nyou can skip by calling `this.skip`.\n\n```js\n/**\n * @import {Program} from 'estree'\n */\n\nimport {Parser} from 'acorn'\nimport {createVisitors} from 'estree-util-scope'\nimport {walk} from 'estree-walker'\n\nconst tree = /** @type {Program} */ (\n  Parser.parse(\n    'function a(b) { var c = 1; if (d) { var e = 2 } }; if (f) { var g = 2 }',\n    {ecmaVersion: 'latest'}\n  )\n)\nconst visitors = createVisitors()\n\nwalk(tree, {\n  enter(node) {\n    visitors.enter(node)\n\n    if (\n      node.type === 'ArrowFunctionExpression' ||\n      node.type === 'FunctionDeclaration' ||\n      node.type === 'FunctionExpression'\n    ) {\n      this.skip()\n      visitors.exit(node) // Call the exit handler manually.\n    }\n  },\n  leave: visitors.exit\n})\n\nconsole.log(visitors.scopes.at(-1))\n```\n\n…yields:\n\n```js\n{ block: false, defined: [ 'a', 'g' ] }\n```\n\n## Compatibility\n\nProjects maintained by the unified collective are compatible with maintained\nversions of Node.js.\n\nWhen we cut a new major release, we drop support for unmaintained versions of\nNode.\nThis means we try to keep the current release line, `estree-util-scope@1`,\ncompatible with Node.js 16.\n\n## Related\n\n## Security\n\nThis package is safe.\n\n## Contribute\n\nSee [`contributing.md` in `syntax-tree/.github`][contributing] for ways to get\nstarted.\nSee [`support.md`][support] for ways to get help.\n\nThis project has a [code of conduct][coc].\nBy interacting with this repository, organization, or community you agree to\nabide by its terms.\n\n## License\n\n[MIT][license] © [Titus Wormer][author]\n\n<!-- Definitions -->\n\n[build-badge]: https://github.com/syntax-tree/estree-util-scope/workflows/main/badge.svg\n\n[build]: https://github.com/syntax-tree/estree-util-scope/actions\n\n[coverage-badge]: https://img.shields.io/codecov/c/github/syntax-tree/estree-util-scope.svg\n\n[coverage]: https://codecov.io/github/syntax-tree/estree-util-scope\n\n[downloads-badge]: https://img.shields.io/npm/dm/estree-util-scope.svg\n\n[downloads]: https://www.npmjs.com/package/estree-util-scope\n\n[size-badge]: https://img.shields.io/badge/dynamic/json?label=minzipped%20size&query=$.size.compressedSize&url=https://deno.bundlejs.com/?q=estree-util-scope\n\n[size]: https://bundlejs.com/?q=estree-util-scope\n\n[sponsors-badge]: https://opencollective.com/unified/sponsors/badge.svg\n\n[backers-badge]: https://opencollective.com/unified/backers/badge.svg\n\n[collective]: https://opencollective.com/unified\n\n[chat-badge]: https://img.shields.io/badge/chat-discussions-success.svg\n\n[chat]: https://github.com/syntax-tree/unist/discussions\n\n[esm]: https://gist.github.com/sindresorhus/a39789f98801d908bbc7ff3ecc99d99c\n\n[npm]: https://docs.npmjs.com/cli/install\n\n[esmsh]: https://esm.sh\n\n[license]: license\n\n[author]: https://wooorm.com\n\n[contributing]: https://github.com/syntax-tree/.github/blob/main/contributing.md\n\n[support]: https://github.com/syntax-tree/.github/blob/main/support.md\n\n[coc]: https://github.com/syntax-tree/.github/blob/main/code-of-conduct.md\n\n[estree]: https://github.com/estree/estree\n\n[github-eslint-scope]: https://github.com/eslint/eslint-scope\n","_attachments":{},"homepage":"https://github.com/syntax-tree/estree-util-scope#readme","bugs":{"url":"https://github.com/syntax-tree/estree-util-scope/issues"},"license":"MIT"}