{"id":13469611,"url":"https://github.com/sarsamurmu/estree-toolkit","last_synced_at":"2025-03-26T07:30:56.042Z","repository":{"id":57230622,"uuid":"325017937","full_name":"sarsamurmu/estree-toolkit","owner":"sarsamurmu","description":"Tools for working with ESTree AST","archived":false,"fork":false,"pushed_at":"2024-10-02T12:49:59.000Z","size":1948,"stargazers_count":54,"open_issues_count":3,"forks_count":1,"subscribers_count":3,"default_branch":"main","last_synced_at":"2024-10-19T14:23:15.736Z","etag":null,"topics":["acorn","analysis","ast","fast","javascript","meriyah","scope","traversal","typescript","visitor","walker"],"latest_commit_sha":null,"homepage":"https://estree-toolkit.netlify.app/","language":"TypeScript","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/sarsamurmu.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,"governance":null,"roadmap":null,"authors":null,"dei":null,"publiccode":null,"codemeta":null}},"created_at":"2020-12-28T13:18:17.000Z","updated_at":"2024-10-01T06:16:31.000Z","dependencies_parsed_at":"2024-05-11T08:25:24.298Z","dependency_job_id":"f1cdcb9e-372b-43f3-bfba-376cd5708350","html_url":"https://github.com/sarsamurmu/estree-toolkit","commit_stats":{"total_commits":191,"total_committers":1,"mean_commits":191.0,"dds":0.0,"last_synced_commit":"638c0c08855f50c5803fa358d0db9e63be76e2e4"},"previous_names":[],"tags_count":26,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/sarsamurmu%2Festree-toolkit","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/sarsamurmu%2Festree-toolkit/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/sarsamurmu%2Festree-toolkit/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/sarsamurmu%2Festree-toolkit/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/sarsamurmu","download_url":"https://codeload.github.com/sarsamurmu/estree-toolkit/tar.gz/refs/heads/main","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":222121829,"owners_count":16934973,"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":["acorn","analysis","ast","fast","javascript","meriyah","scope","traversal","typescript","visitor","walker"],"created_at":"2024-07-31T15:01:46.726Z","updated_at":"2024-10-29T22:31:21.721Z","avatar_url":"https://github.com/sarsamurmu.png","language":"TypeScript","funding_links":[],"categories":["TypeScript"],"sub_categories":[],"readme":"\u003ch1 align=center\u003e\n  \u003ccode\u003eestree-toolkit\u003c/code\u003e\n\u003c/h1\u003e\n\u003ch4 align=center\u003eTools for working with ESTree AST\u003c/h4\u003e\n\u003cp align=center\u003e\n  \u003ca href=\"https://npmjs.com/package/estree-toolkit\"\u003e\n    \u003cimg alt=\"npm\" src=\"https://img.shields.io/npm/v/estree-toolkit?style=flat-square\"\u003e\n  \u003c/a\u003e\n  \u003ca href=\"https://circleci.com/gh/sarsamurmu/estree-toolkit\"\u003e\n    \u003cimg alt=\"Circle CI\" src=\"https://circleci.com/gh/sarsamurmu/estree-toolkit.svg?style=svg\"\u003e\n  \u003c/a\u003e\n  \u003ca href=\"https://codecov.io/gh/sarsamurmu/estree-toolkit\"\u003e\n    \u003cimg alt=\"codecov\" src=\"https://img.shields.io/codecov/c/github/sarsamurmu/estree-toolkit?style=flat-square\"\u003e\n  \u003c/a\u003e\n  \u003ca href=\"https://github.com/sarsamurmu/estree-toolkit/blob/main/LICENSE\"\u003e\n    \u003cimg alt=\"License\" src=\"https://img.shields.io/github/license/sarsamurmu/estree-toolkit?style=flat-square\"\u003e\n  \u003c/a\u003e\n\u003c/p\u003e\n\n## Installation\n```bash\nnpm i estree-toolkit\n# or\nyarn add estree-toolkit\n```\n\n## Usage\n```js\n// Supports both CommonJS and ES Modules\n// ES Module\nimport { traverse, builders as b } from 'estree-toolkit';\n// CommonJS\nconst { traverse, builders: b } = require('estree-toolkit');\n```\n\n## Basic operations\n### Traversing an AST\n```js\nconst { traverse } = require('estree-toolkit');\n\ntraverse(ast, {\n  Program(path) {\n    // Do something with the path\n  }\n});\n```\n### Building Nodes\n```js\nconst { builders: b } = require('estree-toolkit');\n\nb.identifier('x'); // =\u003e { type: 'Identifier', name: 'x' }\n```\n### Checking node types\n```js\nconst { traverse, is } = require('estree-toolkit');\nconst { parseModule } = require('meriyah');\n\nconst ast = parseModule(`x = 0`);\n\ntraverse(ast, {\n  AssignmentExpression(path) {\n    if (is.identifier(path.node.left, { name: 'x' })) {\n      // `left` is an identifier with name `x`\n    }\n  }\n});\n```\n### Replacing a node\n```js\nconst { traverse, builders: b } = require('estree-toolkit');\nconst { parseModule } = require('meriyah');\n\nconst ast = parseModule('a = b');\n\ntraverse(ast, {\n  Identifier(path) {\n    if (path.node.name === 'a') {\n      path.replaceWith(b.identifier('c'));\n    }\n  }\n});\n\n// Now the AST represents - `c = b`\n```\n### Collecting scope information\n```js\nconst { traverse } = require('estree-toolkit');\n\ntraverse(ast, {\n  // Enable scope\n  $: { scope: true },\n  Program(path) {\n    // `path.scope` is now available in all paths\n  }\n});\n```\n#### Checking if a binding is available\n```js\nconst { traverse } = require('estree-toolkit');\nconst { parseModule } = require('meriyah');\n\nconst ast = parseModule(`\nimport { a } from 'source';\n\nconst { b, c: [d, { e }] } = a;\n`);\n\ntraverse(ast, {\n  $: { scope: true },\n  Program(path) {\n    path.scope.hasBinding('a') // =\u003e true\n    path.scope.hasBinding('b') // =\u003e true\n    path.scope.hasBinding('c') // =\u003e false\n    path.scope.hasBinding('d') // =\u003e true\n    path.scope.hasBinding('e') // =\u003e true\n  }\n});\n```\n#### Getting all references of a binding\n```js\nconst { traverse } = require('estree-toolkit');\nconst { parseModule } = require('meriyah');\n\nconst ast = parseModule(`\nimport { a } from 'source';\n\nfn(a);\ns = a;\nlet obj = { a };\n`);\n\ntraverse(ast, {\n  $: { scope: true },\n  Program(path) {\n    // Returns all the paths that reference the binding `a`\n    path.scope.getBinding('a').references // =\u003e [NodePath, NodePath, NodePath]\n  }\n});\n```\n#### Checking if a global has been used\n```js\nconst { traverse } = require('estree-toolkit');\nconst { parseModule } = require('meriyah');\n\nconst ast = parseModule(`\nconst fx = require('fx-mod');\n`);\n\ntraverse(ast, {\n  $: { scope: true },\n  Program(path) {\n    path.scope.hasGlobalBinding('require') // =\u003e true\n  }\n});\n```\n#### Renaming a binding\n```js\nconst { traverse } = require('estree-toolkit');\nconst { parseModule } = require('meriyah');\n\nconst ast = parseModule(`\nconst a = 0\n\na.reload()\nwhile (a.ok) a.run()\n`);\n\ntraverse(ast, {\n  $: { scope: true },\n  Program(path) {\n    // `a` -\u003e `b`\n    path.scope.renameBinding('a', 'b')\n  }\n});\n\n// Output code:\n// const b = 0\n//\n// b.reload()\n// while (b.ok) b.run()\n```\n### Utilities\nThere are several static utilities that you can use.\n- `evaluate`\\\n  Evaluates the given path.\n  ```js\n  const { utils: u, traverse } = require('estree-toolkit');\n  // We are using `meriyah` but you can use any parser (like `acorn`)\n  const { parseModule } = require('meriyah');\n\n  traverse(parseModule(`1 + 2`), {\n    BinaryExpression(path) {\n      u.evaluate(path) // =\u003e { value: 3 }\n    }\n  });\n\n  traverse(parseModule(`1 === 2`), {\n    BinaryExpression(path) {\n      u.evaluate(path) // =\u003e { value: false }\n    }\n  });\n\n  traverse(parseModule(`iDoNotKnowWhatThisIs === 55`), {\n    BinaryExpression(path) {\n      u.evaluate(path) // =\u003e undefined\n    }\n  });\n\n  traverse(parseModule(`\n    ({\n      text: 'This is an object',\n      data: [1, 'two']\n    })\n  `), {\n    ObjectExpression(path) {\n      u.evaluate(path) // =\u003e { value: { text: 'This is an object', data: [1, 'two'] } }\n    }\n  });\n\n  traverse(parseModule(`1 \u003e 5 ? 'YES' : 'NO'`), {\n    ConditionalExpression(path) {\n      u.evaluate(path) // =\u003e { value: 'NO' }\n    }\n  });\n  ```\n- `evaluateTruthy`\\\n  Evaluates the path for truthiness and returns `true`, `false` or `undefined` depending on\n  evaluation result.\n\nThere's more functionalities, please read the documentation.\n\n## Documentation\nYou can find the documentation at https://estree-toolkit.netlify.app/\n\n## Why another traverser?\nI know there is [Babel](https://github.com/babel/babel). But there are\nother tools which are faster than Babel. For example, [`meriyah`](https://github.com/meriyah/meriyah) is 3x faster than [`@babel/parser`](https://www.npmjs.com/package/@babel/parser), [`astring`](https://github.com/davidbonnet/astring) is up to 50x faster than [`@babel/generator`](https://www.npmjs.com/package/@babel/generator). But these tool only work with ESTree AST. I wanted to use these\nfaster alternatives for one of my projects but could not find any traverser with\nbatteries-included. So I built one myself, with awesome scope analysis, it has all the things that you would need for traversing an ESTree AST. Also, a little bit faster than Babel.\n\n## Need help?\nIf you need help in any kind of ESTree AST modification, then don't hesitate to open a new discussion in [Q\u0026A Discussions](https://github.com/sarsamurmu/estree-toolkit/discussions/categories/q-a). I will try my best to help you :)\n\n## License\nLicensed under the [MIT License](/LICENSE).\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fsarsamurmu%2Festree-toolkit","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fsarsamurmu%2Festree-toolkit","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fsarsamurmu%2Festree-toolkit/lists"}