yuku-parser 0.4.1 → 0.4.3

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (4) hide show
  1. package/README.md +137 -0
  2. package/index.d.ts +14 -7
  3. package/index.js +1 -6
  4. package/package.json +12 -12
package/README.md ADDED
@@ -0,0 +1,137 @@
1
+ # yuku-parser
2
+
3
+ A high-performance, spec-compliant JavaScript/TypeScript parser written in Zig, powered by [Yuku](https://github.com/yuku-toolchain/yuku).
4
+
5
+ ## Install
6
+
7
+ ```bash
8
+ npm install yuku-parser
9
+ ```
10
+
11
+ ## Usage
12
+
13
+ ```js
14
+ import { parse } from "yuku-parser";
15
+
16
+ const result = parse("const x = 1 + 2;");
17
+
18
+ console.log(result.program); // ESTree / TypeScript-ESTree Program node
19
+ console.log(result.comments); // all comments
20
+ console.log(result.diagnostics); // errors and warnings
21
+ ```
22
+
23
+ ## ESTree / TypeScript-ESTree
24
+
25
+ For JavaScript and JSX, the AST is fully conformant with the [ESTree](https://github.com/estree/estree) specification, identical to what [Acorn](https://www.npmjs.com/package/acorn) produces.
26
+
27
+ For TypeScript, the AST conforms to the [TypeScript-ESTree](https://www.npmjs.com/package/@typescript-eslint/typescript-estree) format used by `@typescript-eslint`.
28
+
29
+ The only differences from the base ESTree / TypeScript-ESTree specifications are:
30
+
31
+ - Support for Stage 3 [decorators](https://github.com/tc39/proposal-decorators).
32
+ - Support for Stage 3 [import defer](https://github.com/tc39/proposal-defer-import-eval) and [import source](https://github.com/tc39/proposal-source-phase-imports). Dynamic forms (`import.defer(...)`, `import.source(...)`) are represented as an `ImportExpression` with a `phase` field set to `"defer"` or `"source"`, following the ESTree convention.
33
+ - A non-standard `hashbang` field on `Program` for `#!/usr/bin/env node` lines.
34
+
35
+ Any other deviation from Acorn's ESTree or `@typescript-eslint`'s TypeScript-ESTree would be considered a bug.
36
+
37
+ ## AST Types
38
+
39
+ All AST node types are exported directly from this package:
40
+
41
+ ```ts
42
+ import type { Node, Statement, Expression, Identifier } from "yuku-parser";
43
+ ```
44
+
45
+ The `Node` union type covers every possible AST node. Individual types like `Statement`, `Expression`, `Declaration`, etc. are also available. See the full list in the [type definitions](https://github.com/yuku-toolchain/yuku/blob/main/npm/yuku-parser/index.d.ts).
46
+
47
+ ## Walking the AST
48
+
49
+ The AST is standard ESTree, so any ESTree-compatible walker works. For example, with [zimmerframe](https://github.com/sveltejs/zimmerframe):
50
+
51
+ ```ts
52
+ import { parse, type Node } from "yuku-parser";
53
+ import { walk } from "zimmerframe";
54
+
55
+ const { program } = parse(`
56
+ const message = "hello";
57
+ console.log(message);
58
+ `);
59
+
60
+ walk(program as Node, null, {
61
+ Identifier(node) {
62
+ console.log(node.name);
63
+ },
64
+ VariableDeclaration(node) {
65
+ console.log(node.kind);
66
+ },
67
+ });
68
+ ```
69
+
70
+ ## Options
71
+
72
+ All options are optional.
73
+
74
+ ```js
75
+ const result = parse(source, {
76
+ sourceType: "module",
77
+ lang: "jsx",
78
+ preserveParens: true,
79
+ semanticErrors: false,
80
+ });
81
+ ```
82
+
83
+ | Option | Values | Default | Description |
84
+ |--------|--------|---------|-------------|
85
+ | `sourceType` | `"module"`, `"script"` | `"module"` | Module mode enables `import`/`export`, `import.meta`, top-level `await`, and strict mode. |
86
+ | `lang` | `"js"`, `"ts"`, `"jsx"`, `"tsx"`, `"dts"` | `"js"` | Language variant controls which syntax extensions are enabled. |
87
+ | `preserveParens` | `true`, `false` | `true` | Keep `ParenthesizedExpression` nodes in the AST. When false, parentheses are stripped and only the inner expression is kept. |
88
+ | `semanticErrors` | `true`, `false` | `false` | Run semantic analysis and report semantic errors alongside syntax errors. |
89
+
90
+ ## Result
91
+
92
+ `parse` returns a `ParseResult`:
93
+
94
+ ```ts
95
+ interface ParseResult {
96
+ program: Program;
97
+ comments: Comment[];
98
+ diagnostics: Diagnostic[];
99
+ }
100
+ ```
101
+
102
+ The parser is error-tolerant, an AST is always produced even when diagnostics are present.
103
+
104
+ ### Diagnostics
105
+
106
+ Diagnostics cover both syntax errors found during parsing and, when `semanticErrors` is enabled, semantic errors that require scope and binding information (e.g. duplicate `let` declarations, `break` outside a loop, unresolved private fields).
107
+
108
+ Each diagnostic includes:
109
+
110
+ - `severity`: `"error"`, `"warning"`, `"hint"`, or `"info"`
111
+ - `message`: description of the issue
112
+ - `help`: fix suggestion, or `null`
113
+ - `start` / `end`: byte offsets into the source
114
+ - `labels`: additional source spans with messages for context
115
+
116
+ ### Semantic Errors
117
+
118
+ By default, the parser only reports syntax errors. Semantic errors require resolving scopes and bindings, which is done in a separate AST pass. Enable this with the `semanticErrors` option:
119
+
120
+ ```js
121
+ const result = parse(`let x = 1; let x = 2;`, { semanticErrors: true });
122
+ // result.diagnostics will include "Identifier `x` has already been declared", etc.
123
+ ```
124
+
125
+ This incurs a very small performance overhead. If your build pipeline already handles semantic validation (e.g. through a linter or type checker), you can leave this off for faster parsing.
126
+
127
+ ### Comments
128
+
129
+ Each comment includes:
130
+
131
+ - `type`: `"Line"` or `"Block"`
132
+ - `value`: comment text without delimiters (`//`, `/*`, `*/`)
133
+ - `start` / `end`: byte offsets
134
+
135
+ ## License
136
+
137
+ MIT
package/index.d.ts CHANGED
@@ -18,6 +18,13 @@ interface ParseOptions {
18
18
  * @default "js"
19
19
  */
20
20
  lang?: SourceLang;
21
+ /**
22
+ * When true, parenthesized expressions are represented as
23
+ * `ParenthesizedExpression` nodes in the AST. When false,
24
+ * parentheses are stripped and only the inner expression is kept.
25
+ * @default false
26
+ */
27
+ preserveParens?: boolean;
21
28
  /**
22
29
  * Run semantic analysis after parsing and include semantic errors
23
30
  * (e.g. duplicate declarations, invalid `break`/`continue` targets)
@@ -76,14 +83,9 @@ interface ParseResult {
76
83
  }
77
84
 
78
85
  /**
79
- * Parse JS/TS source code synchronously on the current thread.
80
- */
81
- export function parseSync(source: string, options?: ParseOptions): ParseResult;
82
-
83
- /**
84
- * Parse JS/TS source code asynchronously on a background thread.
86
+ * Parse JS/TS source code and return an ESTree / TypeScript-ESTree compatible AST.
85
87
  */
86
- export function parse(source: string, options?: ParseOptions): Promise<ParseResult>;
88
+ export function parse(source: string, options?: ParseOptions): ParseResult;
87
89
 
88
90
  // AST node types
89
91
 
@@ -612,10 +614,15 @@ interface Program extends BaseNode {
612
614
  hashbang: string | null;
613
615
  body: Array<Statement | ModuleDeclaration>;
614
616
  }
617
+
615
618
  type Declaration = FunctionDeclaration | ClassDeclaration | VariableDeclaration | TSDeclareFunction;
619
+
616
620
  type Expression = Identifier | Literal | ThisExpression | Super | ArrayExpression | ObjectExpression | FunctionExpression | ArrowFunctionExpression | ClassExpression | TaggedTemplateExpression | TemplateLiteral | MemberExpression | CallExpression | NewExpression | ChainExpression | SequenceExpression | ParenthesizedExpression | BinaryExpression | LogicalExpression | ConditionalExpression | UnaryExpression | UpdateExpression | AssignmentExpression | YieldExpression | AwaitExpression | ImportExpression | MetaProperty | SpreadElement | TSEmptyBodyFunctionExpression | JSXElement | JSXFragment;
621
+
617
622
  type Statement = ExpressionStatement | BlockStatement | EmptyStatement | DebuggerStatement | ReturnStatement | LabeledStatement | BreakStatement | ContinueStatement | IfStatement | SwitchStatement | ThrowStatement | TryStatement | WhileStatement | DoWhileStatement | ForStatement | ForInStatement | ForOfStatement | WithStatement | Declaration;
623
+
618
624
  type ModuleDeclaration = ImportDeclaration | ExportNamedDeclaration | ExportDefaultDeclaration | ExportAllDeclaration | TSExportAssignment | TSNamespaceExportDeclaration;
625
+
619
626
  type Node = Program | Statement | Expression | ModuleDeclaration | Property | PrivateIdentifier | TemplateElement | VariableDeclarator | CatchClause | SwitchCase | RestElement | ArrayPattern | ObjectPattern | AssignmentPattern | ClassBody | MethodDefinition | PropertyDefinition | AccessorProperty | StaticBlock | Decorator | ImportSpecifier | ImportDefaultSpecifier | ImportNamespaceSpecifier | ImportAttribute | ExportSpecifier | JSXOpeningElement | JSXClosingElement | JSXOpeningFragment | JSXClosingFragment | JSXIdentifier | JSXNamespacedName | JSXMemberExpression | JSXAttribute | JSXSpreadAttribute | JSXExpressionContainer | JSXEmptyExpression | JSXText | JSXSpreadChild;
620
627
 
621
628
  export type { ParseOptions, ParseResult, Comment, Diagnostic, DiagnosticLabel, SourceType, SourceLang, BaseNode, Program, Statement, Expression, Declaration, ModuleDeclaration, Node, Identifier, PrivateIdentifier, Literal, StringLiteral, NumericLiteral, BigIntLiteral, BooleanLiteral, NullLiteral, RegExpLiteral, BindingPattern, FunctionParameter, Property, ArrayPattern, ObjectPattern, AssignmentPattern, RestElement, SequenceExpression, ParenthesizedExpression, BinaryExpression, LogicalExpression, ConditionalExpression, UnaryExpression, UpdateExpression, AssignmentExpression, YieldExpression, AwaitExpression, ArrayExpression, ObjectExpression, SpreadElement, MemberExpression, CallExpression, ChainExpression, TaggedTemplateExpression, NewExpression, MetaProperty, ImportExpression, TemplateLiteral, TemplateElement, Super, ThisExpression, ExpressionStatement, BlockStatement, IfStatement, SwitchStatement, SwitchCase, ForStatement, ForInStatement, ForOfStatement, WhileStatement, DoWhileStatement, BreakStatement, ContinueStatement, LabeledStatement, WithStatement, ReturnStatement, ThrowStatement, TryStatement, CatchClause, DebuggerStatement, EmptyStatement, VariableDeclaration, VariableDeclarator, FunctionDeclaration, FunctionExpression, TSDeclareFunction, TSEmptyBodyFunctionExpression, ArrowFunctionExpression, ClassDeclaration, ClassExpression, ClassBody, MethodDefinition, PropertyDefinition, AccessorProperty, StaticBlock, Decorator, ClassElement, ImportDeclaration, ImportSpecifier, ImportDefaultSpecifier, ImportNamespaceSpecifier, ImportAttribute, ExportNamedDeclaration, ExportDefaultDeclaration, ExportAllDeclaration, ExportSpecifier, TSExportAssignment, TSNamespaceExportDeclaration, JSXElement, JSXOpeningElement, JSXClosingElement, JSXFragment, JSXOpeningFragment, JSXClosingFragment, JSXIdentifier, JSXNamespacedName, JSXMemberExpression, JSXAttribute, JSXSpreadAttribute, JSXExpressionContainer, JSXEmptyExpression, JSXText, JSXSpreadChild, JSXTagName, JSXChild, BinaryOperator, LogicalOperator, UnaryOperator, UpdateOperator, AssignmentOperator, FunctionNodeBase, ClassNodeBase };
package/index.js CHANGED
@@ -1,12 +1,7 @@
1
1
  import binding from './binding.js';
2
2
  import { decode } from './decode.js';
3
3
 
4
- export function parseSync(source, options) {
4
+ export function parse(source, options) {
5
5
  const buffer = binding.parseSync(source, options ?? {});
6
6
  return decode(buffer, source);
7
7
  }
8
-
9
- export async function parse(source, options) {
10
- const buffer = await binding.parse(source, options ?? {});
11
- return decode(buffer, source);
12
- }
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "yuku-parser",
3
- "version": "0.4.1",
3
+ "version": "0.4.3",
4
4
  "description": "High-performance JavaScript/TypeScript parser",
5
5
  "repository": {
6
6
  "type": "git",
@@ -17,16 +17,16 @@
17
17
  "decode.js"
18
18
  ],
19
19
  "optionalDependencies": {
20
- "@yuku-parser/binding-linux-x64-gnu": "0.4.1",
21
- "@yuku-parser/binding-linux-arm64-gnu": "0.4.1",
22
- "@yuku-parser/binding-linux-arm-gnu": "0.4.1",
23
- "@yuku-parser/binding-linux-x64-musl": "0.4.1",
24
- "@yuku-parser/binding-linux-arm64-musl": "0.4.1",
25
- "@yuku-parser/binding-linux-arm-musl": "0.4.1",
26
- "@yuku-parser/binding-darwin-x64": "0.4.1",
27
- "@yuku-parser/binding-darwin-arm64": "0.4.1",
28
- "@yuku-parser/binding-win32-x64": "0.4.1",
29
- "@yuku-parser/binding-win32-arm64": "0.4.1",
30
- "@yuku-parser/binding-freebsd-x64": "0.4.1"
20
+ "@yuku-parser/binding-linux-x64-gnu": "0.4.3",
21
+ "@yuku-parser/binding-linux-arm64-gnu": "0.4.3",
22
+ "@yuku-parser/binding-linux-arm-gnu": "0.4.3",
23
+ "@yuku-parser/binding-linux-x64-musl": "0.4.3",
24
+ "@yuku-parser/binding-linux-arm64-musl": "0.4.3",
25
+ "@yuku-parser/binding-linux-arm-musl": "0.4.3",
26
+ "@yuku-parser/binding-darwin-x64": "0.4.3",
27
+ "@yuku-parser/binding-darwin-arm64": "0.4.3",
28
+ "@yuku-parser/binding-win32-x64": "0.4.3",
29
+ "@yuku-parser/binding-win32-arm64": "0.4.3",
30
+ "@yuku-parser/binding-freebsd-x64": "0.4.3"
31
31
  }
32
32
  }