yuku-parser 0.5.3 → 0.5.4

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.
package/README.md CHANGED
@@ -44,6 +44,19 @@ import type { Node, Statement, Expression, Identifier } from "yuku-parser";
44
44
 
45
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
46
 
47
+ ## Path helpers
48
+
49
+ Two small helpers are exported for resolving the `lang` and `sourceType` options from a file path:
50
+
51
+ ```ts
52
+ import { langFromPath, sourceTypeFromPath } from "yuku-parser";
53
+
54
+ langFromPath("foo.tsx"); // "tsx"
55
+ langFromPath("types.d.ts"); // "dts"
56
+ sourceTypeFromPath("foo.cjs"); // "script"
57
+ sourceTypeFromPath("foo.mjs"); // "module"
58
+ ```
59
+
47
60
  ## Walking the AST
48
61
 
49
62
  The AST is standard ESTree, so any ESTree-compatible walker works. For example, with [zimmerframe](https://github.com/sveltejs/zimmerframe):
package/decode.js CHANGED
@@ -103,8 +103,8 @@ function decode(buffer, source) {
103
103
  case 1: return { type: "ParenthesizedExpression", start, end, expression: f1 !== NULL ? node(f1) : null };
104
104
  case 2: { const r = { type: "ArrowFunctionExpression", start, end, id: null, generator: false, async: !!(flags & 2), params: f1 !== NULL ? fnParams(f1) : [], body: node(f2), expression: !!(flags & 1) }; if (_isTs) { r.typeParameters = f3 !== NULL ? node(f3) : null; r.returnType = f4 !== NULL ? node(f4) : null; } return r; }
105
105
  case 3: { const ft = flags & 3; const r = { type: FUNCTION_TYPES[ft], start, end, id: f1 !== NULL ? node(f1) : null, generator: !!(flags & 4), async: !!(flags & 8), params: f2 !== NULL ? fnParams(f2) : [], body: f3 !== NULL ? node(f3) : null, expression: false }; if (_isTs) { r.typeParameters = f4 !== NULL ? node(f4) : null; r.returnType = f5 !== NULL ? node(f5) : null; r.declare = !!(flags & 16); } return r; }
106
- case 4: return { type: "BlockStatement", start, end, body: nodeArr(f1, f0) };
107
- case 5: return { type: "BlockStatement", start, end, body: nodeArr(f1, f0) };
106
+ case 4: return { type: "BlockStatement", start, end, get body() { const v = nodeArr(f1, f0); _def(this, "body", v); return v; } };
107
+ case 5: return { type: "BlockStatement", start, end, get body() { const v = nodeArr(f1, f0); _def(this, "body", v); return v; } };
108
108
  case 6: return { params: fnParams(i) };
109
109
  case 7: return node(f1);
110
110
  case 8: return { type: "BinaryExpression", start, end, left: f1 !== NULL ? node(f1) : null, right: f2 !== NULL ? node(f2) : null, operator: BINARY_OPS[flags & 31] };
@@ -127,10 +127,10 @@ function decode(buffer, source) {
127
127
  case 25: return { type: "MetaProperty", start, end, meta: f1 !== NULL ? node(f1) : null, property: f2 !== NULL ? node(f2) : null };
128
128
  case 26: return { type: "Decorator", start, end, expression: f1 !== NULL ? node(f1) : null };
129
129
  case 27: { const r = { type: CLASS_TYPES[flags & 1], start, end, decorators: nodeArr(f1, f0), id: f2 !== NULL ? node(f2) : null, superClass: f3 !== NULL ? node(f3) : null, body: node(f4) }; if (_isTs) { r.typeParameters = f5 !== NULL ? node(f5) : null; r.superTypeArguments = f6 !== NULL ? node(f6) : null; r.implements = nodeArr(f7, f8); r.abstract = !!(flags & 2); r.declare = !!(flags & 4); } return r; }
130
- case 28: return { type: "ClassBody", start, end, body: nodeArr(f1, f0) };
130
+ case 28: return { type: "ClassBody", start, end, get body() { const v = nodeArr(f1, f0); _def(this, "body", v); return v; } };
131
131
  case 29: { const r = { type: "MethodDefinition", start, end, decorators: nodeArr(f1, f0), key: node(f2), value: node(f3), kind: METHOD_KINDS[flags & 3], computed: !!(flags & 4), static: !!(flags & 8) }; if (_isTs) { r.override = !!(flags & 16); r.optional = !!(flags & 32); const _abs = !!(flags & 64); r.accessibility = ACCESSIBILITY[(flags >> 7) & 3]; if (_abs) r.type = "TSAbstractMethodDefinition"; } return r; }
132
132
  case 30: { const _acc = !!(flags & 4); const r = { type: _acc ? "AccessorProperty" : "PropertyDefinition", start, end, decorators: nodeArr(f1, f0), key: node(f2), value: f3 !== NULL ? node(f3) : null, computed: !!(flags & 1), static: !!(flags & 2) }; if (_isTs) { r.typeAnnotation = f4 !== NULL ? node(f4) : null; r.declare = !!(flags & 8); r.override = !!(flags & 16); r.optional = !!(flags & 32); r.definite = !!(flags & 64); r.readonly = !!(flags & 128); const _abs = !!(flags & 256); r.accessibility = ACCESSIBILITY[(flags >> 9) & 3]; if (_abs) r.type = _acc ? "TSAbstractAccessorProperty" : "TSAbstractPropertyDefinition"; } return r; }
133
- case 31: return { type: "StaticBlock", start, end, body: nodeArr(f1, f0) };
133
+ case 31: return { type: "StaticBlock", start, end, get body() { const v = nodeArr(f1, f0); _def(this, "body", v); return v; } };
134
134
  case 32: return { type: "Super", start, end };
135
135
  case 33: return { type: "Literal", start, end, value: str(f1, f2), raw: _src.slice(start, end) };
136
136
  case 34: { const r = _src.slice(start, end); const s = r.indexOf("_") === -1 ? r : r.replace(/_/g, ""); const v = (flags & 3) === 2 && s[1] !== "o" && s[1] !== "O" ? parseInt(s.slice(1), 8) : +s; return { type: "Literal", start, end, value: v === v && isFinite(v) ? v : null, raw: r }; }
@@ -173,7 +173,7 @@ function decode(buffer, source) {
173
173
  case 71: { const el = nodeArrHoles(f1, f0); if (f2 !== NULL) el.push(node(f2)); const r = { type: "ArrayPattern", start, end, elements: el }; if (_isTs) { r.decorators = nodeArr(f3, f4); r.optional = !!(flags & 1); r.typeAnnotation = f5 !== NULL ? node(f5) : null; } return r; }
174
174
  case 72: { const pr = nodeArr(f1, f0); if (f2 !== NULL) pr.push(node(f2)); const r = { type: "ObjectPattern", start, end, properties: pr }; if (_isTs) { r.decorators = nodeArr(f3, f4); r.optional = !!(flags & 1); r.typeAnnotation = f5 !== NULL ? node(f5) : null; } return r; }
175
175
  case 73: { const r = { type: "Property", start, end, kind: "init", key: node(f1), value: node(f2), method: false, shorthand: !!(flags & 1), computed: !!(flags & 2) }; if (_isTs) { r.optional = false; } return r; }
176
- case 74: return { type: "Program", start, end, sourceType: (flags & 1) ? "module" : "script", hashbang: (flags & 2) ? { type: "Hashbang", start: _p(f2 - 2), end: _p(f3), value: str(f2, f3) } : null, body: nodeArr(f1, f0) };
176
+ case 74: return { type: "Program", start, end, sourceType: (flags & 1) ? "module" : "script", hashbang: (flags & 2) ? { type: "Hashbang", start: _p(f2 - 2), end: _p(f3), value: str(f2, f3) } : null, get body() { const v = nodeArr(f1, f0); _def(this, "body", v); return v; } };
177
177
  case 75: return { type: "ImportExpression", start, end, source: f1 !== NULL ? node(f1) : null, options: f2 !== NULL ? node(f2) : null, phase: (flags & 1) ? ["source", "defer"][(flags >> 1) & 1] : null };
178
178
  case 76: { const r = { type: "ImportDeclaration", start, end, specifiers: nodeArr(f1, f0), source: f2 !== NULL ? node(f2) : null, attributes: nodeArr(f3, f4), phase: (flags & 1) ? ["source", "defer"][(flags >> 1) & 1] : null }; if (_isTs) { r.importKind = IMPORT_EXPORT_KINDS[(flags >> 2) & 1]; } return r; }
179
179
  case 77: { const r = { type: "ImportSpecifier", start, end, imported: f1 !== NULL ? node(f1) : null, local: f2 !== NULL ? node(f2) : null }; if (_isTs) { r.importKind = IMPORT_EXPORT_KINDS[flags & 1]; } return r; }
@@ -235,14 +235,14 @@ function decode(buffer, source) {
235
235
  case 133: { const r = { type: "TSIndexSignature", start, end, parameters: nodeArr(f1, f0), typeAnnotation: f2 !== NULL ? node(f2) : null, readonly: !!(flags & 1) }; if (_isTs) { r.static = !!(flags & 2); r.accessibility = null; } return r; }
236
236
  case 134: return { type: "TSTypeAliasDeclaration", start, end, id: f1 !== NULL ? node(f1) : null, typeParameters: f2 !== NULL ? node(f2) : null, typeAnnotation: f3 !== NULL ? node(f3) : null, declare: !!(flags & 1) };
237
237
  case 135: return { type: "TSInterfaceDeclaration", start, end, id: f1 !== NULL ? node(f1) : null, typeParameters: f2 !== NULL ? node(f2) : null, extends: nodeArr(f3, f0), body: f4 !== NULL ? node(f4) : null, declare: !!(flags & 1) };
238
- case 136: return { type: "TSInterfaceBody", start, end, body: nodeArr(f1, f0) };
238
+ case 136: return { type: "TSInterfaceBody", start, end, get body() { const v = nodeArr(f1, f0); _def(this, "body", v); return v; } };
239
239
  case 137: return { type: "TSInterfaceHeritage", start, end, expression: f1 !== NULL ? node(f1) : null, typeArguments: f2 !== NULL ? node(f2) : null };
240
240
  case 138: return { type: "TSClassImplements", start, end, expression: f1 !== NULL ? node(f1) : null, typeArguments: f2 !== NULL ? node(f2) : null };
241
241
  case 139: return { type: "TSEnumDeclaration", start, end, id: f1 !== NULL ? node(f1) : null, body: f2 !== NULL ? node(f2) : null, const: !!(flags & 1), declare: !!(flags & 2) };
242
- case 140: return { type: "TSEnumBody", start, end, members: nodeArr(f1, f0) };
242
+ case 140: return { type: "TSEnumBody", start, end, get members() { const v = nodeArr(f1, f0); _def(this, "members", v); return v; } };
243
243
  case 141: return { type: "TSEnumMember", start, end, id: f1 !== NULL ? node(f1) : null, initializer: f2 !== NULL ? node(f2) : null, computed: !!(flags & 1) };
244
244
  case 142: { const r = { type: "TSModuleDeclaration", start, end, id: node(f1), kind: TS_MODULE_KINDS[(flags >> 0) & 1], declare: !!(flags & 2), global: false }; if (f2 !== NULL) r.body = node(f2); return r; }
245
- case 143: return { type: "TSModuleBlock", start, end, body: nodeArr(f1, f0) };
245
+ case 143: return { type: "TSModuleBlock", start, end, get body() { const v = nodeArr(f1, f0); _def(this, "body", v); return v; } };
246
246
  case 144: return { type: "TSModuleDeclaration", start, end, id: node(f1), body: node(f2), kind: "global", declare: !!(flags & 1), global: true };
247
247
  case 145: { const r = { type: "TSParameterProperty", start, end, decorators: nodeArr(f1, f0), parameter: f2 !== NULL ? node(f2) : null, override: !!(flags & 1), readonly: !!(flags & 2), accessibility: ACCESSIBILITY[(flags >> 2) & 3] }; if (_isTs) { r.static = false; } return r; }
248
248
  case 146: return { type: "Identifier", start, end, decorators: [], name: "this", optional: false, typeAnnotation: f1 !== NULL ? node(f1) : null };
@@ -300,6 +300,7 @@ function decode(buffer, source) {
300
300
  }
301
301
  diagnostics[j] = { severity: sev, message: msg, start: ds, end: de, help, labels };
302
302
  }
303
+ function _def(o, k, v) { Object.defineProperty(o, k, { value: v, writable: true, enumerable: true, configurable: true }); }
303
304
  return { program: node(progIdx), comments, diagnostics };
304
305
  }
305
306
  export { decode };
package/index.d.ts CHANGED
@@ -100,6 +100,25 @@ interface ParseResult {
100
100
  */
101
101
  export function parse(source: string, options?: ParseOptions): ParseResult;
102
102
 
103
+ /**
104
+ * Resolves a {@link SourceLang} from a file path's extension.
105
+ *
106
+ * - `.d.ts`, `.d.mts`, `.d.cts` → `"dts"`
107
+ * - `.tsx` → `"tsx"`
108
+ * - `.ts`, `.mts`, `.cts` → `"ts"`
109
+ * - `.jsx` → `"jsx"`
110
+ * - everything else → `"js"`
111
+ */
112
+ export function langFromPath(path: string): SourceLang;
113
+
114
+ /**
115
+ * Resolves a {@link SourceType} from a file path's extension.
116
+ *
117
+ * - `.cjs`, `.cts` → `"script"`
118
+ * - everything else → `"module"`
119
+ */
120
+ export function sourceTypeFromPath(path: string): SourceType;
121
+
103
122
  // AST node types
104
123
 
105
124
  interface BaseNode {
package/index.js CHANGED
@@ -24,3 +24,16 @@ export function parse(source, options) {
24
24
  },
25
25
  };
26
26
  }
27
+
28
+ export function langFromPath(path) {
29
+ if (path.endsWith(".d.ts") || path.endsWith(".d.mts") || path.endsWith(".d.cts")) return "dts";
30
+ if (path.endsWith(".tsx")) return "tsx";
31
+ if (path.endsWith(".ts") || path.endsWith(".mts") || path.endsWith(".cts")) return "ts";
32
+ if (path.endsWith(".jsx")) return "jsx";
33
+ return "js";
34
+ }
35
+
36
+ export function sourceTypeFromPath(path) {
37
+ if (path.endsWith(".cjs") || path.endsWith(".cts")) return "script";
38
+ return "module";
39
+ }
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "yuku-parser",
3
- "version": "0.5.3",
3
+ "version": "0.5.4",
4
4
  "description": "High-performance JavaScript/TypeScript parser",
5
5
  "license": "MIT",
6
6
  "type": "module",
@@ -13,17 +13,17 @@
13
13
  "decode.js"
14
14
  ],
15
15
  "optionalDependencies": {
16
- "@yuku-parser/binding-linux-x64-gnu": "0.5.3",
17
- "@yuku-parser/binding-linux-arm64-gnu": "0.5.3",
18
- "@yuku-parser/binding-linux-arm-gnu": "0.5.3",
19
- "@yuku-parser/binding-linux-x64-musl": "0.5.3",
20
- "@yuku-parser/binding-linux-arm64-musl": "0.5.3",
21
- "@yuku-parser/binding-linux-arm-musl": "0.5.3",
22
- "@yuku-parser/binding-darwin-x64": "0.5.3",
23
- "@yuku-parser/binding-darwin-arm64": "0.5.3",
24
- "@yuku-parser/binding-win32-x64": "0.5.3",
25
- "@yuku-parser/binding-win32-arm64": "0.5.3",
26
- "@yuku-parser/binding-freebsd-x64": "0.5.3"
16
+ "@yuku-parser/binding-linux-x64-gnu": "0.5.4",
17
+ "@yuku-parser/binding-linux-arm64-gnu": "0.5.4",
18
+ "@yuku-parser/binding-linux-arm-gnu": "0.5.4",
19
+ "@yuku-parser/binding-linux-x64-musl": "0.5.4",
20
+ "@yuku-parser/binding-linux-arm64-musl": "0.5.4",
21
+ "@yuku-parser/binding-linux-arm-musl": "0.5.4",
22
+ "@yuku-parser/binding-darwin-x64": "0.5.4",
23
+ "@yuku-parser/binding-darwin-arm64": "0.5.4",
24
+ "@yuku-parser/binding-win32-x64": "0.5.4",
25
+ "@yuku-parser/binding-win32-arm64": "0.5.4",
26
+ "@yuku-parser/binding-freebsd-x64": "0.5.4"
27
27
  },
28
28
  "keywords": [
29
29
  "parser",