{"id":22423429,"url":"https://github.com/prantlf/estree-walkie","last_synced_at":"2025-03-27T05:41:08.862Z","repository":{"id":43102908,"uuid":"511038065","full_name":"prantlf/estree-walkie","owner":"prantlf","description":"Walks a JavaScript AST compatible with ESTree.","archived":false,"fork":false,"pushed_at":"2022-07-08T19:11:53.000Z","size":47,"stargazers_count":1,"open_issues_count":0,"forks_count":0,"subscribers_count":2,"default_branch":"master","last_synced_at":"2025-03-02T15:11:58.896Z","etag":null,"topics":["ast","estree","estree-ast-traversal","estree-visitor","estree-walker","traversal","visitor","walker"],"latest_commit_sha":null,"homepage":"","language":"JavaScript","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"mit","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/prantlf.png","metadata":{"files":{"readme":"README.md","changelog":"CHANGELOG.md","contributing":null,"funding":null,"license":"LICENSE","code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null}},"created_at":"2022-07-06T07:41:12.000Z","updated_at":"2022-07-06T09:11:02.000Z","dependencies_parsed_at":"2022-09-26T17:00:48.667Z","dependency_job_id":null,"html_url":"https://github.com/prantlf/estree-walkie","commit_stats":null,"previous_names":[],"tags_count":4,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/prantlf%2Festree-walkie","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/prantlf%2Festree-walkie/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/prantlf%2Festree-walkie/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/prantlf%2Festree-walkie/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/prantlf","download_url":"https://codeload.github.com/prantlf/estree-walkie/tar.gz/refs/heads/master","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":245791899,"owners_count":20672668,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":["ast","estree","estree-ast-traversal","estree-visitor","estree-walker","traversal","visitor","walker"],"created_at":"2024-12-05T18:11:18.505Z","updated_at":"2025-03-27T05:41:08.837Z","avatar_url":"https://github.com/prantlf.png","language":"JavaScript","funding_links":[],"categories":[],"sub_categories":[],"readme":"# estree-walkie\n\n[![Latest version](https://img.shields.io/npm/v/estree-walkie)\n ![Dependency status](https://img.shields.io/librariesio/release/npm/estree-walkie)\n](https://www.npmjs.com/package/estree-walkie)\n[![Coverage](https://codecov.io/gh/prantlf/estree-walkie/branch/master/graph/badge.svg)](https://codecov.io/gh/prantlf/estree-walkie)\n\nWalks a JavaScript [AST] compatible with [ESTree].\n\n* [ESM], [CJS] and [UMD] modules offering an [API] for programmatic usage in [Node.js], [Deno] and web browser.\n* Ignores keys in AST nodes pointing to parent nodes. (Often needed and would cause a stack overflow.)\n* Includes TypeScript types.\n* No dependencies.\n* Tiny size - 926 B minified, 435 B gzipped, 390 B brotlied.\n* [Blazingly fast].\n\n## Synopsis\n\n```js\nimport { parse } from 'meriyah'\nimport { walk } from 'estree-walkie'\n\nconst program = parse('...', { module: true, next: true })\nconst identifiers = []\n\nwalk(program, {\n  Identifier(node) {\n    identifiers.push(node.name)\n  }\n})\n```\n\n## API\n\nExported methods:\n\n    walk(node, visitor, fallback, state)\n    walkAtOffset(node, offset, visitor, fallback, state)\n    walkAtPosition(node, line, column, visitor, fallback, state)\n    walkInRange(node, start, end, visitor, fallback, state)\n    walkBetweenLines(node, first, last, visitor, fallback, state)\n\nArguments:\n\n* `node` - [AST] node to start walking with\n* `visitor` - object with visiting handlers named by node types\n* `fallback` - handler for nodes not matched by the visitor (optional)\n* `state` - custom value passed to each handler (optional)\n* `offset` - source code character offset to walk the [AST] around\n* `start` and `ens` - source code character range to walk the [AST] within\n* `line` and `column` - source code position to walk the [AST] around\n* `first` and `last` - source code line range to walk the [AST] within\n\nVisiting handler:\n\n    handler(node, state, parent) =\u003e boolean?\n    { enter?: handler, exit?: handler }\n\n* `node` - currently visited [AST] node\n* `state` - custom value passed to the walking method (optional)\n* `node` - parent of the currently visited [AST] node\n\nIf an object with `enter` and/or `exit` methods is used as a visiting handler, `enter` will be caled before visiting children and `exit` after visiting children of the current node.\n\nIf a handler retuns `false` children of the current node will not be visited.\n\nWalking can be interrupted by throwing an error and ignoring the thrown type in error handling.\n\n## Examples\n\nCollect only identifiers used in the outer scope:\n\n```js\nimport { parse } from 'meriyah'\nimport { walk } from 'estree-walkie'\n\nconst program = parse('...', { module: true, next: true })\nconst identifiers = []\n\nconst avoid = () =\u003e false\nwalk(program, {\n  FunctionDeclaration: avoid,\n  ClassDeclaration: avoid,\n  CallExpression: avoid,\n  FunctionExpression: avoid,\n  ArrowFunctionExpression: avoid,\n  Identifier(node) {\n    identifiers.push(node.name)\n  }\n})\n```\n\nCollect variables declared at all scopes and assign `node.parent` to all nodes:\n\n```js\nimport { parse } from 'meriyah'\nimport { walk } from 'estree-walkie'\n\nconst program = parse('...', { module: true, next: true, loc: true })\nconst variables = []\n\nconst assignParent = (node, parent) =\u003e node.parent = parent\nwalk(astRoot, line, column, {\n  VariableDeclarator(node, state, parent) {\n    if (node.type !== 'Identifier') {\n      throw new Error(`unsupported declarator at ${node.loc.start.line}:${node.loc.start.column + 1}`)\n    }\n    state.push(node.name)\n    assignParent(node, parent)\n  }\n}, (node, _state, parent) =\u003e assignParent(node, parent), variables)\n```\n\nCheck if there is an identifier at a specific position and remember its parent:\n\n```js\nimport { parse } from 'meriyah'\nimport { walk } from 'estree-walkie'\n\nconst program = parse('...', { module: true, next: true, loc: true })\nlet identified\n\ntry {\n  walkAtLocation(astRoot, line, column, {\n    Identifier(node, _state, parent) {\n      identified = parent\n      throw 0\n    }\n  });\n} catch (err) {\n  if (typeof err !== 'number') throw err\n}\n```\n\n## Node.js usage\n\nMake sure that you use [Node.js] \u003e= 8.3. Install the `estree-walkie` package locally using your favourite package manager:\n\n```\nnpm i estree-walkie\npnpm i estree-walkie\nyarn add estree-walkie\n```\n\nEither import methods from the [API] using the [CJS] module:\n\n```js\nconst { walk, walkAtOffset, walkAtPosition, walkInRange, walkBetweenLines } = require('estree-walkie')\n...\n```\n\nOr import methods from the [API] using the [ESM] module:\n\n```js\nimport { walk, walkAtOffset, walkAtPosition, walkInRange, walkBetweenLines } from 'estree-walkie'\n...\n```\n\n## Browser usage\n\nYou can either compile this module to your application bundle, or load it directly to the browser. For the former, you would install this package and import methods from the [API] in the same way as for the [Node.js usage]. For the latter, you would either refer to the module installed locally, or to the module from the [UNPKG CDN], for example:\n\n    node_modules/estree-walkie/dist/index.umd.min.js\n    https://unpkg.com/estree-walkie@0.0.1/dist/index.min.mjs\n\nThe following modules are available in the `dist` directory:\n\n| Name               | Type                       |\n| ------------------ | -------------------------- |\n| `index.cjs`        | [CJS] module, not minified |\n| `index.mjs`        | [ESM] module, not minified |\n| `index.min.mjs`    | [ESM] module, minified     |\n| `index.umd.js`     | [UMD] module, not minified |\n| `index.umd.min.js` | [UMD] module, minified     |\n\nEither import methods from the [API] using the [ESM] module:\n\n```html\n\u003cscript type=module\u003e\n  import { walk, walkAtOffset, walkAtPosition, walkInRange, walkBetweenLines }\n    from 'https://unpkg.com/estree-walkie@0.0.1/dist/index.min.mjs'\n  ...\n\u003c/script\u003e\n```\n\nOr import methods from the [API] using the [UMD] module, which will set a global object `estree-walkie`:\n\n```html\n\u003cscript src=https://unpkg.com/estree-walkie@0.0.1/dist/index.umd.min.js\u003e\u003c/script\u003e\n\u003cscript\u003e\n  (() =\u003e {\n    const { walk, walkAtOffset, walkAtPosition, walkInRange, walkBetweenLines } = estreeWalkie\n    ...\n  })()\n\u003c/script\u003e\n```\n\nIf an [AMD] module loader is detected, the [UMD] module will return exports es expected:\n\n```html\n\u003cscript\u003e\n  require(['https://unpkg.com/estree-walkie@0.0.1/dist/index.umd.min.js'],\n    ({ walk, walkAtOffset, walkAtPosition, walkInRange, walkBetweenLines }) =\u003e {\n      ...\n    })\n\u003c/script\u003e\n```\n\n## Performance\n\nComparison with other libraries, which allow visiting [AST] nodes by their types:\n\n    $ cd bench \u0026\u0026 pnpm i \u0026\u0026 node walk\n\n    Validation:\n      ✔ @babel/traverse (46,955 identifiers)\n      ✔ estree-walker (46,955 identifiers)\n      ✔ estree-walk (46,955 identifiers)\n      ✔ estree-visitor (46,955 identifiers)\n      ✘ acorn-walk (26,662 identifiers)\n      ✔ ast-types (46,955 identifiers)\n      ✔ astray (46,955 identifiers)\n      ✔ estree-walkie (46,955 identifiers)\n\n    Benchmark:\n      @babel/traverse x 8.18 ops/sec ±7.00% (25 runs sampled)\n      estree-walker   x 95.59 ops/sec ±2.63% (69 runs sampled)\n      estree-walk     x 46.53 ops/sec ±1.57% (60 runs sampled)\n      estree-visitor  x 61.86 ops/sec ±1.87% (64 runs sampled)\n      acorn-walk      x 78.81 ops/sec ±0.92% (68 runs sampled)\n      ast-types       x 3.65 ops/sec ±8.24% (14 runs sampled)\n      astray          x 98.37 ops/sec ±0.93% (72 runs sampled)\n      estree-walkie   x 131 ops/sec ±0.99% (76 runs sampled)\n\n## Contributing\n\nIn lieu of a formal styleguide, take care to maintain the existing coding style. Add unit tests for any new or changed functionality. Lint and test your code using `npm test`.\n\n## License\n\nCopyright (c) 2022 Ferdinand Prantl\n\nLicensed under the MIT license.\n\n[AST]: https://astexplorer.net/\n[ESTree]: https://github.com/estree/estree#readme\n[Node.js]: https://nodejs.org/\n[Deno]: https://deno.land/\n[Blazingly fast]: #performance\n[API]: #api\n[Node.js usage]: #nodejs-usage\n[UNPKG CDN]: https://unpkg.com/\n[CJS]: https://blog.risingstack.com/node-js-at-scale-module-system-commonjs-require/#commonjstotherescue\n[UMD]: https://github.com/umdjs/umd#readme\n[AMD]: https://github.com/amdjs/amdjs-api/wiki/AMD\n[ESM]: https://hacks.mozilla.org/2018/03/es-modules-a-cartoon-deep-dive/#content-head\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fprantlf%2Festree-walkie","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fprantlf%2Festree-walkie","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fprantlf%2Festree-walkie/lists"}