{"name":"yuku-ast","version":"0.8.2","keywords":["ast","estree","walker","builder","traverse","visitor","typescript","javascript","yuku"],"license":"MIT","_id":"yuku-ast@0.8.2","maintainers":[{"name":"arshadyaseen","email":"arshadpyaseen@gmail.com"}],"homepage":"https://github.com/yuku-toolchain/yuku#readme","bugs":{"url":"https://github.com/yuku-toolchain/yuku/issues"},"dist":{"shasum":"9df82d0325fd79342043e50deb354fc255efcd35","tarball":"http://registry-npm.ywwl.com/yuku-ast/download/yuku-ast-0.8.2.tgz","fileCount":4,"integrity":"sha512-GIxDPIyxjAKmF7J2AwrMDt2oCn+PZ0i7e+CIrvt5Tf0LbBS+v/xdbaAF1sUwrbntDq4YqtF7l6OG0amg1qhxWA==","signatures":[{"sig":"MEQCIE43dqBTnsV6uvjuaXvExVKDTex2dBEiiSZUhICVH1NiAiBzoAz4daZEHvUYFhcWtIsD0oc8gVLhTKgW3MpW6aTpQA==","keyid":"SHA256:DhQ8wR5APBvFHLF/+Tc+AYvPOdTpcIDqOhxsBHRwC7U"}],"attestations":{"url":"https://registry.npmjs.org/-/npm/v1/attestations/yuku-ast@0.8.2","provenance":{"predicateType":"https://slsa.dev/provenance/v1"}},"unpackedSize":80785,"size":17834,"noattachment":false,"key":"/yuku-ast/-/yuku-ast-0.8.2.tgz"},"main":"./dist/index.js","type":"module","_from":"file:yuku-ast-0.8.2.tgz","types":"./dist/index.d.ts","module":"./dist/index.js","exports":{".":"./dist/index.js","./package.json":"./package.json"},"scripts":{"build":"tsdown"},"_npmUser":{"name":"GitHub Actions","email":"npm-oidc-no-reply@github.com","trustedPublisher":{"id":"github","oidcConfigId":"oidc:538ddaea-518e-4580-a8ff-acbea6f16f79"}},"_resolved":"/home/runner/work/yuku/yuku/npm/yuku-ast/yuku-ast-0.8.2.tgz","_integrity":"sha512-GIxDPIyxjAKmF7J2AwrMDt2oCn+PZ0i7e+CIrvt5Tf0LbBS+v/xdbaAF1sUwrbntDq4YqtF7l6OG0amg1qhxWA==","repository":{"url":"git+https://github.com/yuku-toolchain/yuku.git","type":"git"},"_npmVersion":"11.16.0","description":"Typed AST toolkit for JavaScript and TypeScript ESTree trees: walker, builders, guards, and syntactic utilities","directories":{},"sideEffects":false,"_nodeVersion":"24.18.0","dependencies":{"@yuku-toolchain/types":"^0.8.2"},"_hasShrinkwrap":false,"devDependencies":{"tsdown":"^0.15.6"},"_npmOperationalInternal":{"tmp":"tmp/yuku-ast_0.8.2_1785582839520_0.257330659020357","host":"s3://npm-registry-packages-npm-production"},"readme":"# yuku-ast\n\nWalk, build, and check any ESTree / TypeScript-ESTree AST, with typed visitors, in-place mutation, builders, guards, and syntactic utilities, part of [Yuku](https://yuku.fyi).\n\nIt is plain JavaScript and works on any ESTree AST, whichever parser produced it.\n\n- [Install](#install)\n- [Walking](#walking)\n- [Builders](#builders)\n- [Guards](#guards)\n- [Imports and exports](#imports-and-exports)\n- [Utilities](#utilities)\n- [Identifier names](#identifier-names)\n\n## Install\n\n```bash\nnpm install yuku-ast\n```\n\n## Walking\n\n```ts\nimport { parse } from \"yuku-parser\";\nimport { walk } from \"yuku-ast\";\n\nconst { program } = parse(source);\n\nwalk(program, {\n  Identifier(node) {\n    console.log(node.name);\n  },\n  CallExpression: {\n    enter(node, ctx) {},\n    leave(node, ctx) {},\n  },\n  Function(node) {\n    // function declarations, expressions, and arrows\n  },\n  enter(node) {},\n});\n```\n\nHandlers are keyed by node `type`, by alias group, or by the universal `enter` / `leave`, and receive the exact node type. The alias groups are `Expression`, `Statement`, `Declaration`, `ModuleDeclaration`, `Function`, `Class`, `Method`, `Loop`, `Pattern`, `JSX`, and `TSType`. Per node, the order is the universal `enter`, alias enters, the typed enter, the children, then the same in reverse for leave.\n\nAn optional third argument threads state to every handler as `ctx.state`. `walkAsync` is the async counterpart, with the same traversal order and mutation semantics and every handler awaited before the walk moves on.\n\n### The context\n\nOne context object is reused across the whole walk, so do not store it.\n\n```js\nctx.node;        // the current node\nctx.parent;      // its parent, or null at the walk root\nctx.key;         // the field on the parent holding this node\nctx.index;       // position in an array field, or null\nctx.ancestors(); // a copy of the ancestor chain, root first\n```\n\n### Mutation\n\nHandlers can mutate the AST in place.\n\n| Operation                | Effect                                                                                                         |\n| ------------------------ | -------------------------------------------------------------------------------------------------------------- |\n| `ctx.skip()`             | Do not descend into this node's children. `leave` still fires.                                                 |\n| `ctx.stop()`             | End the walk immediately.                                                                                      |\n| `ctx.replace(node)`      | Swap the current node. The walk continues into the replacement's children and `leave` fires for its new type. |\n| `ctx.remove()`           | Splice the node out of an array field, or null a plain field. Children are not walked, `leave` does not fire.  |\n| `ctx.insertBefore(node)` | Insert a sibling before the current node. The inserted node is not visited.                                    |\n| `ctx.insertAfter(node)`  | Insert a sibling after the current node. The walk visits it.                                                   |\n\nA replacement created with `start: 0, end: 0`, such as a [builder](#builders) node, inherits the original node's span, which keeps source maps meaningful through [`yuku-codegen`](https://www.npmjs.com/package/yuku-codegen).\n\n```js\nwalk(program, {\n  DebuggerStatement(node, ctx) {\n    ctx.remove();\n  },\n});\n```\n\n### findAll\n\n`findAll` collects every node of the given types, in source order.\n\n```js\nimport { findAll } from \"yuku-ast\";\n\nfindAll(program, \"CallExpression\");\nfindAll(program, [\"ClassDeclaration\", \"TSInterfaceDeclaration\"]);\n```\n\n`CHILD_KEYS` maps each node type to its child fields in traversal order, for walkers of your own.\n\n## Builders\n\n`b` has one typed constructor per node type, its fields derived from the node type itself, so a builder never drifts from the AST. Spans default to 0, which `ctx.replace` fills from the replaced node.\n\n```ts\nimport { b } from \"yuku-ast\";\n\nb.CallExpression({ callee: b.Identifier({ name: \"f\" }), arguments: [], optional: false });\n```\n\n## Guards\n\n`is` has one guard per node type, per alias group, and for the shapes ESTree folds into one type: literal kinds, member expression kinds, and directives. Every guard accepts `null` and `undefined` and narrows.\n\n```ts\nimport { is } from \"yuku-ast\";\n\nis.CallExpression(node);\nis.Identifier(node, \"require\");\nis.oneOf(node, [\"FunctionDeclaration\", \"ClassDeclaration\"]);\nis.Expression(node);\nis.StringLiteral(node);\nis.StaticMemberExpression(node);\nis.Directive(node);\n```\n\n## Imports and exports\n\n`collectImports` and `collectExports` read a module's import and export declarations, one record per bound name, destructuring included.\n\n```ts\nimport { collectExports, collectImports } from \"yuku-ast\";\n\nfor (const record of collectImports(program)) {\n  record.source;   // \"./m\"\n  record.local;    // the local binding name\n  record.imported; // \"default\", \"*\", or the export name\n  record.typeOnly; // import type / import { type x }\n  record.phase;    // \"source\" | \"defer\" | null\n}\n\nfor (const record of collectExports(program)) {\n  record.exported; // the exported name, null for a bare export *\n  record.local;    // the backing local name, when there is one\n  record.source;   // the re-export specifier, when there is one\n  record.typeOnly;\n}\n```\n\n`collectImportDeclaration` and `collectExportDeclaration` return the records of a single statement, for use inside a walk.\n\n```ts\nwalk(program, {\n  ImportDeclaration(node) {\n    records.push(...collectImportDeclaration(node));\n  },\n});\n```\n\n## Utilities\n\n```ts\nimport {\n  bindingIdentifiers, // every binding Identifier a pattern introduces\n  isCallOf,           // isCallOf(node, \"require\")\n  isWrapper,          // true for the wrappers unwrap strips\n  literalValue,       // string | number | boolean | bigint | RegExp | null\n  nameOf,             // Identifier name or string Literal value\n  unwrap,             // strips parens and erased TypeScript assertion wrappers\n} from \"yuku-ast\";\n```\n\n## Identifier names\n\n```ts\nimport { isIdentifierName, isValidIdentifier } from \"yuku-ast\";\n\nisValidIdentifier(\"foo\");   // true\nisValidIdentifier(\"class\"); // false, reserved\nisIdentifierName(\"class\");  // true, syntactically an IdentifierName\n```\n\nPlus `isIdentifierStart`, `isIdentifierChar`, `isKeyword`, `isReservedWord`, `isStrictReservedWord`, `isStrictBindReservedWord`, and `isStrictBindOnlyReservedWord`.\n\n## Semantic analysis\n\n[`yuku-analyzer`](https://www.npmjs.com/package/yuku-analyzer) builds on this walker. Its `module.walk` adds the semantic model to the context, as `ctx.binding`, `ctx.reference`, and `ctx.scope`.\n\n## License\n\nMIT\n","publish_time":1785582839683,"_cnpm_publish_time":1785582839683,"dist-tags":{"latest":"0.19.0"}}