{"name":"yuku-parser","version":"0.2.16","author":"Arshad Yaseen <arshadpyaseen@gmail.com> (https://arshad.fyi)","license":"MIT","_id":"yuku-parser@0.2.16","maintainers":[{"name":"arshadyaseen","email":"arshadpyaseen@gmail.com"}],"homepage":"https://yuku.fyi","dist":{"shasum":"d247f93c814ab31e915dfe60e2c87edee2d9df63","tarball":"http://registry-npm.ywwl.com/yuku-parser/download/yuku-parser-0.2.16.tgz","fileCount":2,"integrity":"sha512-TFhXcT+JDALbO/Ev+GyyUJVt0Ybw1qaiz4yvgUCWIigoxpiE3QFp4DHZvD59KqGP2EwYbiZv+Cd7rh859vV/hw==","signatures":[{"sig":"MEQCIFo0sgll1C7sTsLWQElBM2yC9mx0SLNIBWlRLS3QOtAhAiB5aqZo4tXmRs17/myKYjIC1DKMg3s3u14WYdrTkyKb2g==","keyid":"SHA256:DhQ8wR5APBvFHLF/+Tc+AYvPOdTpcIDqOhxsBHRwC7U"}],"unpackedSize":1043,"size":593,"noattachment":false,"key":"/yuku-parser/-/yuku-parser-0.2.16.tgz"},"type":"module","module":"./dist/index.js","shasum":"d247f93c814ab31e915dfe60e2c87edee2d9df63","exports":{".":{"import":"./dist/index.js"},"./package.json":"./package.json"},"funding":"https://github.com/sponsors/arshad-yaseen","scripts":{"build":"bunup --exports"},"_npmUser":{"name":"arshadyaseen","email":"arshadpyaseen@gmail.com"},"_integrity":"sha512-TFhXcT+JDALbO/Ev+GyyUJVt0Ybw1qaiz4yvgUCWIigoxpiE3QFp4DHZvD59KqGP2EwYbiZv+Cd7rh859vV/hw==","repository":{"url":"git+https://github.com/yuku-toolchain/yuku.git","type":"git"},"_npmVersion":"10.8.3","description":"Yuku, the JavaScript/TypeScript parser written in Zig.","directories":{},"_nodeVersion":"24.3.0","_hasShrinkwrap":false,"devDependencies":{"bunup":"^0.16.22"},"peerDependencies":{"typescript":">=4.5.0"},"peerDependenciesMeta":{"typescript":{"optional":true}},"_npmOperationalInternal":{"tmp":"tmp/yuku-parser_0.2.16_1772252668832_0.4578710557242296","host":"s3://npm-registry-packages-npm-production"},"readme":"# yuku-parser\n\nA fast, spec-compliant JavaScript and TypeScript parser, part of [Yuku](https://yuku.fyi).\n\n- [Install](#install)\n- [Usage](#usage)\n- [The AST](#the-ast)\n- [Options](#options)\n- [Result](#result)\n- [Diagnostics](#diagnostics)\n- [Comments](#comments)\n- [Tokens](#tokens)\n\n## Install\n\n```bash\nnpm install yuku-parser\n```\n\nIt runs on Yuku's native core, installed for your platform. In browsers and edge runtimes, load the WebAssembly core from [`@yuku-core/wasm`](https://www.npmjs.com/package/@yuku-core/wasm) and pass it in as `core`.\n\n## Usage\n\n```js\nimport { parse } from \"yuku-parser\";\n\nconst { program, comments, diagnostics } = parse(\"const x = 1 + 2;\");\n```\n\n## The AST\n\n[ESTree](https://github.com/estree/estree) for JavaScript and JSX, identical to [Acorn](https://www.npmjs.com/package/acorn), and [TypeScript-ESTree](https://www.npmjs.com/package/@typescript-eslint/typescript-estree) for TypeScript, matching [Oxc](https://oxc.rs) for both. On top of the specs, it carries stage 3 [decorators](https://github.com/tc39/proposal-decorators), [import defer](https://github.com/tc39/proposal-defer-import-eval) and [import source](https://github.com/tc39/proposal-source-phase-imports) as `phase` on imports and `ImportExpression`, and the `hashbang` of `Program`.\n\nEvery node type is exported, listed in the [type definitions](https://github.com/yuku-toolchain/yuku/blob/main/npm/yuku-types/index.d.ts).\n\n```ts\nimport type { Expression, Identifier, Node, Statement } from \"yuku-parser\";\n```\n\nTo walk, build, and check nodes, use [`yuku-ast`](https://www.npmjs.com/package/yuku-ast). For scopes and bindings, use [`yuku-analyzer`](https://www.npmjs.com/package/yuku-analyzer).\n\n## Options\n\n```js\nparse(source, { path: \"src/app.tsx\" });\n```\n\n| Option           | Values                                    | Default    | Description                                                                                        |\n| ---------------- | ----------------------------------------- | ---------- | -------------------------------------------------------------------------------------------------- |\n| `path`           | a file path                               | none       | Names the file in diagnostics. `lang` and `sourceType` default from its extension.                 |\n| `lang`           | `\"js\"`, `\"jsx\"`, `\"ts\"`, `\"tsx\"`, `\"dts\"` | `\"js\"`     | The syntax to parse. `.d.ts`, `.tsx`, `.ts`, and `.jsx` paths select their own.                    |\n| `sourceType`     | `\"module\"`, `\"script\"`, `\"commonjs\"`      | `\"module\"` | `\"commonjs\"` allows top-level `return`, and `.cjs` and `.cts` paths select it.                     |\n| `preserveParens` | `true`, `false`                           | `true`     | Keep `ParenthesizedExpression` nodes.                                                              |\n| `semanticErrors` | `true`, `false`                           | `false`    | Also report the early errors that need scopes, such as redeclarations and `break` outside a loop.  |\n| `attachComments` | `true`, `false`                           | `false`    | Also attach each comment to its node. See [Comments](#comments).                                   |\n| `tokens`         | `true`, `false`                           | `false`    | Keep every token. See [Tokens](#tokens).                                                           |\n| `core`           | a loaded core                             | native     | The core that parses, see [`@yuku-core/wasm`](https://www.npmjs.com/package/@yuku-core/wasm).      |\n\nAn unknown `lang` or `sourceType` throws a `TypeError`. `langFromPath(path)` and `sourceTypeFromPath(path)` return the values a path implies.\n\n## Result\n\n```ts\ninterface ParseResult {\n  program: Program;\n  comments: Comment[];\n  tokens?: TokenList; // with tokens: true\n  diagnostics: Diagnostic[];\n}\n```\n\nThe parser recovers from errors, so a result with diagnostics still holds a tree of everything it could read.\n\n## Diagnostics\n\nEvery Yuku package reports diagnostics in one shape.\n\n```ts\ninterface Diagnostic {\n  severity: \"error\" | \"warning\" | \"hint\" | \"info\";\n  message: string;\n  path: string | null; // the path option\n  start: number;       // UTF-16 offsets, like nodes\n  end: number;\n  labels: { start: number; end: number; message: string }[];\n  help: string | null;\n}\n```\n\n## Comments\n\n```js\nconst { comments } = parse(`// a line comment\\nconst x = 1; /* a block comment */`);\n// [\n//   { type: \"Line\", value: \" a line comment\", start: 0, end: 17 },\n//   { type: \"Block\", value: \" a block comment \", start: 31, end: 52 },\n// ]\n```\n\n`attachComments: true` also hangs each comment on the node it sits next to, which [`yuku-codegen`](https://www.npmjs.com/package/yuku-codegen#comments) prints from, so comments move with their nodes.\n\n```js\nconst { program } = parse(`// header\\nfunction foo() {} // trailing`, { attachComments: true });\n\nprogram.body[0].comments;\n// [\n//   { type: \"Line\", position: \"before\", sameLine: false, value: \" header\" },\n//   { type: \"Line\", position: \"after\", sameLine: true, value: \" trailing\" },\n// ]\n```\n\n`position` is `\"before\"`, `\"after\"`, or `\"inside\"` an otherwise empty node, such as `function f() { /* hi */ }`.\n\n## Tokens\n\n`tokens: true` keeps every token in a `TokenList`, a view over the parser's token table where a token is an index.\n\n```js\nimport { parse, TokenKind } from \"yuku-parser\";\n\nconst { tokens } = parse(source, { tokens: true });\n\nfor (let i = 0; i < tokens.length; i++) {\n  if (tokens.kind(i) === TokenKind.Arrow) console.log(tokens.start(i), tokens.text(i));\n}\n```\n\n```js\ntokens.kind(i)                      // one of TokenKind\ntokens.text(i)                      // its source text\ntokens.start(i)                     // UTF-16 offsets\ntokens.end(i)\n\ntokens.isKeyword(i)\ntokens.isReserved(i)                // reserved unconditionally or in strict mode\ntokens.isUnconditionallyReserved(i)\ntokens.isStrictModeReserved(i)\ntokens.isIdentifierLike(i)          // an identifier or any keyword\ntokens.isNumericLiteral(i)\ntokens.isBinaryOperator(i)\ntokens.isLogicalOperator(i)\ntokens.isUnaryOperator(i)\ntokens.isAssignmentOperator(i)\ntokens.precedence(i)                // operator precedence, 0 when none\n\ntokens.newlineBefore(i)             // what ASI reads\ntokens.escaped(i)\ntokens.invalidEscape(i)             // a template chunk whose cooked value is undefined\ntokens.loneSurrogate(i)\n\ntokens.range(node)                  // [from, to) of the tokens inside a node\ntokens.first(node)\ntokens.last(node)\ntokens.before(nodeOrOffset)         // the last token ending at or before it\ntokens.after(nodeOrOffset)          // the first token starting at or after it\ntokens.at(offset)                   // the token containing an offset\n```\n\nThe queries answer with an index, or `-1` when there is none. Tokens are as the parser resolved them, so a regex is one `RegexLiteral`, and comments are not tokens. The kinds are listed in [tokens.d.ts](https://github.com/yuku-toolchain/yuku/blob/main/npm/yuku-types/tokens.d.ts).\n\n## License\n\nMIT\n","publish_time":1772252668999,"_cnpm_publish_time":1772252668999,"dist-tags":{"beta":"0.5.0-beta.2","latest":"0.19.0"}}