{"name":"yuku-ast","version":"0.1.2","keywords":["ast","codemod","estree","javascript","transform","traverse","typescript","visitor","walker","yuku"],"author":{"url":"https://arshad.fyi","name":"Arshad Yaseen","email":"arshadpyaseen@gmail.com"},"license":"MIT","_id":"yuku-ast@0.1.2","maintainers":[{"name":"arshadyaseen","email":"arshadpyaseen@gmail.com"}],"homepage":"https://yuku.fyi","bugs":{"url":"https://github.com/yuku-toolchain/yuku-ast/issues"},"dist":{"shasum":"12e365df7d52294b99b1bb27b14aff2a91155f6d","tarball":"http://registry-npm.ywwl.com/yuku-ast/download/yuku-ast-0.1.2.tgz","fileCount":6,"integrity":"sha512-m+8UDMxAWImW36S0Z0S8jK4It5IyQKDzZ2QPa1/inceI7rV9oPeE30nV5OfbI1uLMMnAYa1zGOyuq2VwB95ZpQ==","signatures":[{"sig":"MEYCIQD2TlNRCZd/RLawo3DHasWk+ztNtrC9J2PA856Np8cnEwIhAJE2X1NagkyUJmfZQhLzhUnln2sKiN5goUeL6ua7DakR","keyid":"SHA256:DhQ8wR5APBvFHLF/+Tc+AYvPOdTpcIDqOhxsBHRwC7U"}],"attestations":{"url":"https://registry.npmjs.org/-/npm/v1/attestations/yuku-ast@0.1.2","provenance":{"predicateType":"https://slsa.dev/provenance/v1"}},"unpackedSize":110446,"size":21787,"noattachment":false,"key":"/yuku-ast/-/yuku-ast-0.1.2.tgz"},"type":"module","_from":"file:yuku-ast-0.1.2.tgz","types":"./dist/index.d.mts","exports":{".":"./dist/index.mjs","./identifier":"./dist/identifier.mjs","./package.json":"./package.json"},"funding":"https://github.com/sponsors/arshad-yaseen","scripts":{"lint":"oxlint","test":"bun test","build":"tsdown","format":"oxfmt","release":"bumpp -r --commit 'npm %s' --push --tag","type-check":"tsc --noEmit"},"_npmUser":{"name":"GitHub Actions","email":"npm-oidc-no-reply@github.com","trustedPublisher":{"id":"github","oidcConfigId":"oidc:39a0ff63-ab74-44d9-9f72-b98e55af95aa"}},"_resolved":"/home/runner/work/yuku-ast/yuku-ast/yuku-ast-0.1.2.tgz","_integrity":"sha512-m+8UDMxAWImW36S0Z0S8jK4It5IyQKDzZ2QPa1/inceI7rV9oPeE30nV5OfbI1uLMMnAYa1zGOyuq2VwB95ZpQ==","repository":{"url":"git+https://github.com/yuku-toolchain/yuku-ast.git","type":"git"},"_npmVersion":"11.15.0","description":"A fast, typed AST toolkit for yuku-parser's ESTree / TypeScript-ESTree AST: node builders, type guards, a walker, and identifier validators.","directories":{},"_nodeVersion":"24.16.0","_hasShrinkwrap":false,"devDependencies":{"bumpp":"^11.1.0","oxfmt":"^0.51.0","oxlint":"^1.66.0","tsdown":"^0.22.0","@types/bun":"latest","typescript":"^6.0.3","yuku-parser":"^0.5.18","yuku-codegen":"^0.5.18"},"peerDependencies":{"yuku-parser":">=0.5.18"},"_npmOperationalInternal":{"tmp":"tmp/yuku-ast_0.1.2_1779871469520_0.522850880418908","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":1779871469665,"_cnpm_publish_time":1779871469665,"dist-tags":{"latest":"0.19.0"}}