yuku-parser 0.2.18 → 0.3.0
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/binding.js +63 -0
- package/decode.js +237 -0
- package/index.d.ts +621 -0
- package/index.js +12 -0
- package/package.json +21 -38
- package/dist/index.js +0 -2
package/binding.js
ADDED
|
@@ -0,0 +1,63 @@
|
|
|
1
|
+
import { createRequire } from 'node:module';
|
|
2
|
+
import { readFileSync } from 'node:fs';
|
|
3
|
+
import { execSync } from 'node:child_process';
|
|
4
|
+
import { fileURLToPath } from 'node:url';
|
|
5
|
+
import { dirname, join } from 'node:path';
|
|
6
|
+
|
|
7
|
+
const require = createRequire(import.meta.url);
|
|
8
|
+
const __dirname = dirname(fileURLToPath(import.meta.url));
|
|
9
|
+
const { platform, arch } = process;
|
|
10
|
+
|
|
11
|
+
const isFileMusl = (f) => f.includes('libc.musl-') || f.includes('ld-musl-');
|
|
12
|
+
|
|
13
|
+
function isMusl() {
|
|
14
|
+
if (platform !== 'linux') return false;
|
|
15
|
+
|
|
16
|
+
try {
|
|
17
|
+
if (readFileSync('/usr/bin/ldd', 'utf-8').includes('musl')) return true;
|
|
18
|
+
} catch {}
|
|
19
|
+
|
|
20
|
+
try {
|
|
21
|
+
const report = typeof process.report?.getReport === 'function'
|
|
22
|
+
? process.report.getReport()
|
|
23
|
+
: null;
|
|
24
|
+
if (report) {
|
|
25
|
+
const header = typeof report === 'string' ? JSON.parse(report).header : report.header;
|
|
26
|
+
if (header?.glibcVersionRuntime) return false;
|
|
27
|
+
if (Array.isArray(report.sharedObjects) && report.sharedObjects.some(isFileMusl)) return true;
|
|
28
|
+
}
|
|
29
|
+
} catch {}
|
|
30
|
+
|
|
31
|
+
try {
|
|
32
|
+
return execSync('ldd --version', { encoding: 'utf8' }).includes('musl');
|
|
33
|
+
} catch {}
|
|
34
|
+
|
|
35
|
+
return false;
|
|
36
|
+
}
|
|
37
|
+
|
|
38
|
+
function loadBinding() {
|
|
39
|
+
const errors = [];
|
|
40
|
+
const libc = platform === 'linux' ? (isMusl() ? '-musl' : '-gnu') : '';
|
|
41
|
+
const suffix = `${platform}-${arch}${libc}`;
|
|
42
|
+
|
|
43
|
+
try {
|
|
44
|
+
return require(join(__dirname, '@yuku-parser', 'binding-' + suffix, 'yuku-parser.node'));
|
|
45
|
+
} catch (e) {
|
|
46
|
+
errors.push(e);
|
|
47
|
+
}
|
|
48
|
+
|
|
49
|
+
try {
|
|
50
|
+
return require('@yuku-parser/binding-' + suffix + '/yuku-parser.node');
|
|
51
|
+
} catch (e) {
|
|
52
|
+
errors.push(e);
|
|
53
|
+
}
|
|
54
|
+
|
|
55
|
+
throw new Error(
|
|
56
|
+
`Failed to load native binding for ${platform}-${arch}.\n` +
|
|
57
|
+
`If this persists, try removing node_modules and reinstalling.\n` +
|
|
58
|
+
errors.map(e => ` - ${e.message}`).join('\n'),
|
|
59
|
+
{ cause: errors[errors.length - 1] }
|
|
60
|
+
);
|
|
61
|
+
}
|
|
62
|
+
|
|
63
|
+
export default loadBinding();
|
package/decode.js
ADDED
|
@@ -0,0 +1,237 @@
|
|
|
1
|
+
// generated by tools/gen_estree_decoder.zig, do not edit
|
|
2
|
+
const NULL = 0xFFFFFFFF;
|
|
3
|
+
const BINARY_OPS = ["==", "!=", "===", "!==", "<", "<=", ">", ">=", "+", "-", "*", "/", "%", "**", "|", "^", "&", "<<", ">>", ">>>", "in", "instanceof"];
|
|
4
|
+
const LOGICAL_OPS = ["&&", "||", "??"];
|
|
5
|
+
const UNARY_OPS = ["-", "+", "!", "~", "typeof", "void", "delete"];
|
|
6
|
+
const UPDATE_OPS = ["++", "--"];
|
|
7
|
+
const ASSIGNMENT_OPS = ["=", "+=", "-=", "*=", "/=", "%=", "**=", "<<=", ">>=", ">>>=", "|=", "^=", "&=", "||=", "&&=", "??="];
|
|
8
|
+
const VAR_KINDS = ["var", "let", "const", "using", "await using"];
|
|
9
|
+
const PROPERTY_KINDS = ["init", "get", "set"];
|
|
10
|
+
const METHOD_KINDS = ["constructor", "method", "get", "set"];
|
|
11
|
+
const FUNCTION_TYPES = ["FunctionDeclaration", "FunctionExpression", "TSDeclareFunction", "TSEmptyBodyFunctionExpression"];
|
|
12
|
+
const CLASS_TYPES = ["ClassDeclaration", "ClassExpression"];
|
|
13
|
+
const COMMENT_TYPES = ["Line", "Block"];
|
|
14
|
+
const SEVERITY = ["error", "warning", "hint", "info"];
|
|
15
|
+
const _td = new TextDecoder("utf-8", { ignoreBOM: true });
|
|
16
|
+
let _u8, _u32, _src, _srcLen, _nodesOff, _extraBase, _spOff, _p, str;
|
|
17
|
+
function _poolDecode(s, e) {
|
|
18
|
+
const a = _spOff + s - _srcLen, b = _spOff + e - _srcLen;
|
|
19
|
+
let hasEd = false;
|
|
20
|
+
for (let i = a; i < b; i++) if (_u8[i] === 0xED) { hasEd = true; break; }
|
|
21
|
+
if (!hasEd) return _td.decode(_u8.subarray(a, b));
|
|
22
|
+
let r = "";
|
|
23
|
+
for (let i = a; i < b; ) {
|
|
24
|
+
const c = _u8[i];
|
|
25
|
+
if (c < 0x80) { r += String.fromCharCode(c); i++; }
|
|
26
|
+
else if (c < 0xE0) { r += String.fromCharCode(((c & 0x1F) << 6) | (_u8[i+1] & 0x3F)); i += 2; }
|
|
27
|
+
else if (c < 0xF0) { r += String.fromCharCode(((c & 0x0F) << 12) | ((_u8[i+1] & 0x3F) << 6) | (_u8[i+2] & 0x3F)); i += 3; }
|
|
28
|
+
else { r += String.fromCodePoint(((c & 0x07) << 18) | ((_u8[i+1] & 0x3F) << 12) | ((_u8[i+2] & 0x3F) << 6) | (_u8[i+3] & 0x3F)); i += 4; }
|
|
29
|
+
}
|
|
30
|
+
return r;
|
|
31
|
+
}
|
|
32
|
+
function node(i) {
|
|
33
|
+
const o = _nodesOff + i * 32;
|
|
34
|
+
const tag = _u8[o];
|
|
35
|
+
const flags = _u8[o + 1];
|
|
36
|
+
const f0 = _u8[o + 2] | (_u8[o + 3] << 8);
|
|
37
|
+
const b = o >> 2;
|
|
38
|
+
const f1 = _u32[b + 1], f2 = _u32[b + 2], f3 = _u32[b + 3], f4 = _u32[b + 4], f5 = _u32[b + 5];
|
|
39
|
+
const start = _p(_u32[b + 6]), end = _p(_u32[b + 7]);
|
|
40
|
+
switch (tag) {
|
|
41
|
+
case 0: return { type: "SequenceExpression", start, end, expressions: nodeArr(f1, f0) };
|
|
42
|
+
case 1: return { type: "ParenthesizedExpression", start, end, expression: f1 !== NULL ? node(f1) : null };
|
|
43
|
+
case 2: return { type: "ArrowFunctionExpression", start, end, id: null, generator: false, async: !!(flags & 2), params: f1 !== NULL ? fnParams(f1) : [], body: node(f2), expression: !!(flags & 1) };
|
|
44
|
+
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 (ft === 2) r.declare = true; return r; }
|
|
45
|
+
case 4: return { type: "BlockStatement", start, end, body: nodeArr(f1, f0) };
|
|
46
|
+
case 5: return { type: "BlockStatement", start, end, body: nodeArr(f1, f0) };
|
|
47
|
+
case 6: return { params: fnParams(i) };
|
|
48
|
+
case 7: return node(f1);
|
|
49
|
+
case 8: return { type: "BinaryExpression", start, end, left: f1 !== NULL ? node(f1) : null, right: f2 !== NULL ? node(f2) : null, operator: BINARY_OPS[flags & 31] };
|
|
50
|
+
case 9: return { type: "LogicalExpression", start, end, left: f1 !== NULL ? node(f1) : null, right: f2 !== NULL ? node(f2) : null, operator: LOGICAL_OPS[flags & 3] };
|
|
51
|
+
case 10: return { type: "ConditionalExpression", start, end, test: f1 !== NULL ? node(f1) : null, consequent: f2 !== NULL ? node(f2) : null, alternate: f3 !== NULL ? node(f3) : null };
|
|
52
|
+
case 11: return { type: "UnaryExpression", start, end, operator: UNARY_OPS[flags & 7], prefix: true, argument: f1 !== NULL ? node(f1) : null };
|
|
53
|
+
case 12: return { type: "UpdateExpression", start, end, argument: f1 !== NULL ? node(f1) : null, operator: UPDATE_OPS[flags & 1], prefix: !!(flags & 2) };
|
|
54
|
+
case 13: return { type: "AssignmentExpression", start, end, left: f1 !== NULL ? node(f1) : null, right: f2 !== NULL ? node(f2) : null, operator: ASSIGNMENT_OPS[flags & 15] };
|
|
55
|
+
case 14: return { type: "ArrayExpression", start, end, elements: nodeArrHoles(f1, f0) };
|
|
56
|
+
case 15: return { type: "ObjectExpression", start, end, properties: nodeArr(f1, f0) };
|
|
57
|
+
case 16: return { type: "SpreadElement", start, end, argument: f1 !== NULL ? node(f1) : null };
|
|
58
|
+
case 17: return { type: "Property", start, end, key: f1 !== NULL ? node(f1) : null, value: f2 !== NULL ? node(f2) : null, kind: PROPERTY_KINDS[flags & 3], method: !!(flags & 4), shorthand: !!(flags & 8), computed: !!(flags & 16) };
|
|
59
|
+
case 18: return { type: "MemberExpression", start, end, object: f1 !== NULL ? node(f1) : null, property: f2 !== NULL ? node(f2) : null, computed: !!(flags & 1), optional: !!(flags & 2) };
|
|
60
|
+
case 19: return { type: "CallExpression", start, end, callee: f1 !== NULL ? node(f1) : null, arguments: nodeArr(f2, f0), optional: !!(flags & 1) };
|
|
61
|
+
case 20: return { type: "ChainExpression", start, end, expression: f1 !== NULL ? node(f1) : null };
|
|
62
|
+
case 21: return { type: "TaggedTemplateExpression", start, end, tag: f1 !== NULL ? node(f1) : null, quasi: f2 !== NULL ? node(f2) : null };
|
|
63
|
+
case 22: return { type: "NewExpression", start, end, callee: f1 !== NULL ? node(f1) : null, arguments: nodeArr(f2, f0) };
|
|
64
|
+
case 23: return { type: "AwaitExpression", start, end, argument: f1 !== NULL ? node(f1) : null };
|
|
65
|
+
case 24: return { type: "YieldExpression", start, end, argument: f1 !== NULL ? node(f1) : null, delegate: !!(flags & 1) };
|
|
66
|
+
case 25: return { type: "MetaProperty", start, end, meta: f1 !== NULL ? node(f1) : null, property: f2 !== NULL ? node(f2) : null };
|
|
67
|
+
case 26: return { type: "Decorator", start, end, expression: f1 !== NULL ? node(f1) : null };
|
|
68
|
+
case 27: return { 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) };
|
|
69
|
+
case 28: return { type: "ClassBody", start, end, body: nodeArr(f1, f0) };
|
|
70
|
+
case 29: return { type: "MethodDefinition", start, end, decorators: nodeArr(f1, f0), key: f2 !== NULL ? node(f2) : null, value: f3 !== NULL ? node(f3) : null, kind: METHOD_KINDS[flags & 3], computed: !!(flags & 4), static: !!(flags & 8) };
|
|
71
|
+
case 30: return { type: (flags & 4) ? "AccessorProperty" : "PropertyDefinition", start, end, decorators: nodeArr(f1, f0), key: node(f2), value: f3 !== NULL ? node(f3) : null, computed: !!(flags & 1), static: !!(flags & 2) };
|
|
72
|
+
case 31: return { type: "StaticBlock", start, end, body: nodeArr(f1, f0) };
|
|
73
|
+
case 32: return { type: "Super", start, end };
|
|
74
|
+
case 33: return { type: "Literal", start, end, value: str(f1, f2), raw: _src.slice(start, end) };
|
|
75
|
+
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 }; }
|
|
76
|
+
case 35: { const r = _src.slice(start, end); const d = str(f1, f2).replace(/_/g, ""); const v = BigInt(d); return { type: "Literal", start, end, value: v, raw: r, bigint: v.toString() }; }
|
|
77
|
+
case 36: { const v = !!(flags & 1); return { type: "Literal", start, end, value: v, raw: v ? "true" : "false" }; }
|
|
78
|
+
case 37: return { type: "Literal", start, end, value: null, raw: "null" };
|
|
79
|
+
case 38: return { type: "ThisExpression", start, end };
|
|
80
|
+
case 39: { const p = str(f1, f2), fl = str(f3, f4); let v = null; try { v = new RegExp(p, fl); } catch {} return { type: "Literal", start, end, value: v, raw: "/" + p + "/" + fl, regex: { pattern: p, flags: fl.split("").sort().join("") } }; }
|
|
81
|
+
case 40: return { type: "TemplateLiteral", start, end, quasis: nodeArr(f1, f0), expressions: nodeArr(f2, f3) };
|
|
82
|
+
case 41: { const r = _src.slice(start, end).replace(/\r\n?/g, "\n"); return { type: "TemplateElement", start, end, value: { raw: r, cooked: (flags & 2) ? null : str(f1, f2) }, tail: !!(flags & 1) }; }
|
|
83
|
+
case 42: return { type: "Identifier", start, end, name: str(f1, f2) };
|
|
84
|
+
case 43: return { type: "PrivateIdentifier", start, end, name: str(f1, f2) };
|
|
85
|
+
case 44: return { type: "Identifier", start, end, name: str(f1, f2) };
|
|
86
|
+
case 45: return { type: "Identifier", start, end, name: str(f1, f2) };
|
|
87
|
+
case 46: return { type: "Identifier", start, end, name: str(f1, f2) };
|
|
88
|
+
case 47: return { type: "ExpressionStatement", start, end, expression: f1 !== NULL ? node(f1) : null };
|
|
89
|
+
case 48: return { type: "IfStatement", start, end, test: f1 !== NULL ? node(f1) : null, consequent: f2 !== NULL ? node(f2) : null, alternate: f3 !== NULL ? node(f3) : null };
|
|
90
|
+
case 49: return { type: "SwitchStatement", start, end, discriminant: f1 !== NULL ? node(f1) : null, cases: nodeArr(f2, f0) };
|
|
91
|
+
case 50: return { type: "SwitchCase", start, end, test: f1 !== NULL ? node(f1) : null, consequent: nodeArr(f2, f0) };
|
|
92
|
+
case 51: return { type: "ForStatement", start, end, init: f1 !== NULL ? node(f1) : null, test: f2 !== NULL ? node(f2) : null, update: f3 !== NULL ? node(f3) : null, body: f4 !== NULL ? node(f4) : null };
|
|
93
|
+
case 52: return { type: "ForInStatement", start, end, left: f1 !== NULL ? node(f1) : null, right: f2 !== NULL ? node(f2) : null, body: f3 !== NULL ? node(f3) : null };
|
|
94
|
+
case 53: return { type: "ForOfStatement", start, end, left: f1 !== NULL ? node(f1) : null, right: f2 !== NULL ? node(f2) : null, body: f3 !== NULL ? node(f3) : null, await: !!(flags & 1) };
|
|
95
|
+
case 54: return { type: "WhileStatement", start, end, test: f1 !== NULL ? node(f1) : null, body: f2 !== NULL ? node(f2) : null };
|
|
96
|
+
case 55: return { type: "DoWhileStatement", start, end, body: f1 !== NULL ? node(f1) : null, test: f2 !== NULL ? node(f2) : null };
|
|
97
|
+
case 56: return { type: "BreakStatement", start, end, label: f1 !== NULL ? node(f1) : null };
|
|
98
|
+
case 57: return { type: "ContinueStatement", start, end, label: f1 !== NULL ? node(f1) : null };
|
|
99
|
+
case 58: return { type: "LabeledStatement", start, end, label: f1 !== NULL ? node(f1) : null, body: f2 !== NULL ? node(f2) : null };
|
|
100
|
+
case 59: return { type: "WithStatement", start, end, object: f1 !== NULL ? node(f1) : null, body: f2 !== NULL ? node(f2) : null };
|
|
101
|
+
case 60: return { type: "ReturnStatement", start, end, argument: f1 !== NULL ? node(f1) : null };
|
|
102
|
+
case 61: return { type: "ThrowStatement", start, end, argument: f1 !== NULL ? node(f1) : null };
|
|
103
|
+
case 62: return { type: "TryStatement", start, end, block: f1 !== NULL ? node(f1) : null, handler: f2 !== NULL ? node(f2) : null, finalizer: f3 !== NULL ? node(f3) : null };
|
|
104
|
+
case 63: return { type: "CatchClause", start, end, param: f1 !== NULL ? node(f1) : null, body: f2 !== NULL ? node(f2) : null };
|
|
105
|
+
case 64: return { type: "DebuggerStatement", start, end };
|
|
106
|
+
case 65: return { type: "EmptyStatement", start, end };
|
|
107
|
+
case 66: return { type: "VariableDeclaration", start, end, kind: VAR_KINDS[flags & 7], declarations: nodeArr(f1, f0) };
|
|
108
|
+
case 67: return { type: "VariableDeclarator", start, end, id: f1 !== NULL ? node(f1) : null, init: f2 !== NULL ? node(f2) : null };
|
|
109
|
+
case 68: return { type: "ExpressionStatement", start, end, expression: node(f1), directive: str(f2, f3) };
|
|
110
|
+
case 69: return { type: "AssignmentPattern", start, end, left: f1 !== NULL ? node(f1) : null, right: f2 !== NULL ? node(f2) : null };
|
|
111
|
+
case 70: return { type: "RestElement", start, end, argument: f1 !== NULL ? node(f1) : null };
|
|
112
|
+
case 71: { const el = nodeArrHoles(f1, f0); if (f2 !== NULL) el.push(node(f2)); return { type: "ArrayPattern", start, end, elements: el }; }
|
|
113
|
+
case 72: { const pr = nodeArr(f1, f0); if (f2 !== NULL) pr.push(node(f2)); return { type: "ObjectPattern", start, end, properties: pr }; }
|
|
114
|
+
case 73: return { type: "Property", start, end, kind: "init", key: node(f1), value: node(f2), method: false, shorthand: !!(flags & 1), computed: !!(flags & 2) };
|
|
115
|
+
case 74: return { type: "Program", start, end, sourceType: (flags & 1) ? "module" : "script", hashbang: (flags & 2) ? str(f2, f3) : null, body: nodeArr(f1, f0) };
|
|
116
|
+
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 };
|
|
117
|
+
case 76: return { 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 };
|
|
118
|
+
case 77: return { type: "ImportSpecifier", start, end, imported: f1 !== NULL ? node(f1) : null, local: f2 !== NULL ? node(f2) : null };
|
|
119
|
+
case 78: return { type: "ImportDefaultSpecifier", start, end, local: f1 !== NULL ? node(f1) : null };
|
|
120
|
+
case 79: return { type: "ImportNamespaceSpecifier", start, end, local: f1 !== NULL ? node(f1) : null };
|
|
121
|
+
case 80: return { type: "ImportAttribute", start, end, key: f1 !== NULL ? node(f1) : null, value: f2 !== NULL ? node(f2) : null };
|
|
122
|
+
case 81: return { type: "ExportNamedDeclaration", start, end, declaration: f1 !== NULL ? node(f1) : null, specifiers: nodeArr(f2, f0), source: f3 !== NULL ? node(f3) : null, attributes: nodeArr(f4, f5) };
|
|
123
|
+
case 82: return { type: "ExportDefaultDeclaration", start, end, declaration: f1 !== NULL ? node(f1) : null };
|
|
124
|
+
case 83: return { type: "ExportAllDeclaration", start, end, exported: f1 !== NULL ? node(f1) : null, source: f2 !== NULL ? node(f2) : null, attributes: nodeArr(f3, f0) };
|
|
125
|
+
case 84: return { type: "ExportSpecifier", start, end, local: f1 !== NULL ? node(f1) : null, exported: f2 !== NULL ? node(f2) : null };
|
|
126
|
+
case 85: return { type: "TSExportAssignment", start, end, expression: f1 !== NULL ? node(f1) : null };
|
|
127
|
+
case 86: return { type: "TSNamespaceExportDeclaration", start, end, id: f1 !== NULL ? node(f1) : null };
|
|
128
|
+
case 87: return { type: "JSXElement", start, end, openingElement: f1 !== NULL ? node(f1) : null, children: nodeArr(f2, f0), closingElement: f3 !== NULL ? node(f3) : null };
|
|
129
|
+
case 88: return { type: "JSXOpeningElement", start, end, name: f1 !== NULL ? node(f1) : null, attributes: nodeArr(f2, f0), selfClosing: !!(flags & 1) };
|
|
130
|
+
case 89: return { type: "JSXClosingElement", start, end, name: f1 !== NULL ? node(f1) : null };
|
|
131
|
+
case 90: return { type: "JSXFragment", start, end, openingFragment: f1 !== NULL ? node(f1) : null, children: nodeArr(f2, f0), closingFragment: f3 !== NULL ? node(f3) : null };
|
|
132
|
+
case 91: return { type: "JSXOpeningFragment", start, end, attributes: [], selfClosing: false };
|
|
133
|
+
case 92: return { type: "JSXClosingFragment", start, end };
|
|
134
|
+
case 93: return { type: "JSXIdentifier", start, end, name: str(f1, f2) };
|
|
135
|
+
case 94: return { type: "JSXNamespacedName", start, end, namespace: f1 !== NULL ? node(f1) : null, name: f2 !== NULL ? node(f2) : null };
|
|
136
|
+
case 95: return { type: "JSXMemberExpression", start, end, object: f1 !== NULL ? node(f1) : null, property: f2 !== NULL ? node(f2) : null };
|
|
137
|
+
case 96: return { type: "JSXAttribute", start, end, name: f1 !== NULL ? node(f1) : null, value: f2 !== NULL ? node(f2) : null };
|
|
138
|
+
case 97: return { type: "JSXSpreadAttribute", start, end, argument: f1 !== NULL ? node(f1) : null };
|
|
139
|
+
case 98: return { type: "JSXExpressionContainer", start, end, expression: f1 !== NULL ? node(f1) : null };
|
|
140
|
+
case 99: return { type: "JSXEmptyExpression", start, end };
|
|
141
|
+
case 100: { const t = str(f1, f2); return { type: "JSXText", start, end, value: t, raw: t }; }
|
|
142
|
+
case 101: return { type: "JSXSpreadChild", start, end, expression: f1 !== NULL ? node(f1) : null };
|
|
143
|
+
}
|
|
144
|
+
}
|
|
145
|
+
function nodeArr(s, len) {
|
|
146
|
+
const r = new Array(len);
|
|
147
|
+
for (let j = 0; j < len; j++) r[j] = node(_u32[_extraBase + s + j]);
|
|
148
|
+
return r;
|
|
149
|
+
}
|
|
150
|
+
function nodeArrHoles(s, len) {
|
|
151
|
+
const r = new Array(len);
|
|
152
|
+
for (let j = 0; j < len; j++) { const x = _u32[_extraBase + s + j]; r[j] = x !== NULL ? node(x) : null; }
|
|
153
|
+
return r;
|
|
154
|
+
}
|
|
155
|
+
function fnParams(idx) {
|
|
156
|
+
const po = _nodesOff + idx * 32;
|
|
157
|
+
const len = _u8[po + 2] | (_u8[po + 3] << 8);
|
|
158
|
+
const pb = po >> 2;
|
|
159
|
+
const iStart = _u32[pb + 1], rest = _u32[pb + 2];
|
|
160
|
+
const p = [];
|
|
161
|
+
for (let j = 0; j < len; j++) {
|
|
162
|
+
const fi = _u32[_extraBase + iStart + j];
|
|
163
|
+
p.push(node(_u32[(_nodesOff + fi * 32 >> 2) + 1]));
|
|
164
|
+
}
|
|
165
|
+
if (rest !== NULL) p.push(node(rest));
|
|
166
|
+
return p;
|
|
167
|
+
}
|
|
168
|
+
function buildPosMap(src, byteLen) {
|
|
169
|
+
const m = new Uint32Array(byteLen + 1);
|
|
170
|
+
let bp = 0, u16p = 0;
|
|
171
|
+
for (let i = 0; i < src.length; i++) {
|
|
172
|
+
m[bp] = u16p;
|
|
173
|
+
const cp = src.codePointAt(i);
|
|
174
|
+
let bytes = 1;
|
|
175
|
+
if (cp > 0x7F) { if (cp > 0x7FF) { bytes = cp > 0xFFFF ? 4 : 3; } else { bytes = 2; } }
|
|
176
|
+
const units = bytes === 4 ? 2 : 1;
|
|
177
|
+
u16p += units;
|
|
178
|
+
for (let k = 1; k < bytes; k++) m[bp + k] = u16p;
|
|
179
|
+
bp += bytes;
|
|
180
|
+
if (bytes === 4) i++;
|
|
181
|
+
}
|
|
182
|
+
m[byteLen] = u16p;
|
|
183
|
+
return m;
|
|
184
|
+
}
|
|
185
|
+
function decode(buffer, source) {
|
|
186
|
+
_u8 = new Uint8Array(buffer);
|
|
187
|
+
const aLen = (buffer.byteLength >> 2) << 2;
|
|
188
|
+
_u32 = new Uint32Array(buffer, 0, aLen >> 2);
|
|
189
|
+
_src = source;
|
|
190
|
+
_srcLen = _u32[5];
|
|
191
|
+
const nodeCount = _u32[2], extraCount = _u32[3], spLen = _u32[4];
|
|
192
|
+
const commentCount = _u32[6], diagCount = _u32[7], progIdx = _u32[8];
|
|
193
|
+
const pm = (_u32[9] & 1) ? null : buildPosMap(source, _srcLen);
|
|
194
|
+
if (pm) {
|
|
195
|
+
_p = function(v) { return pm[v]; };
|
|
196
|
+
str = function(s, e) { if (s === e) return ""; if (s < _srcLen) return _src.slice(pm[s], pm[e]); return _poolDecode(s, e); };
|
|
197
|
+
} else {
|
|
198
|
+
_p = function(v) { return v; };
|
|
199
|
+
str = function(s, e) { if (s === e) return ""; if (s < _srcLen) return _src.slice(s, e); return _poolDecode(s, e); };
|
|
200
|
+
}
|
|
201
|
+
_nodesOff = 40;
|
|
202
|
+
const eOff = _nodesOff + nodeCount * 32;
|
|
203
|
+
_extraBase = eOff >> 2;
|
|
204
|
+
_spOff = eOff + extraCount * 4;
|
|
205
|
+
const cOff = _spOff + spLen, dOff = cOff + commentCount * 20;
|
|
206
|
+
const dv = new DataView(buffer);
|
|
207
|
+
const comments = new Array(commentCount);
|
|
208
|
+
for (let j = 0; j < commentCount; j++) {
|
|
209
|
+
const o = cOff + j * 20;
|
|
210
|
+
comments[j] = { type: COMMENT_TYPES[_u8[o]], value: str(dv.getUint32(o + 12, true), dv.getUint32(o + 16, true)), start: _p(dv.getUint32(o + 4, true)), end: _p(dv.getUint32(o + 8, true)) };
|
|
211
|
+
}
|
|
212
|
+
const diagnostics = new Array(diagCount);
|
|
213
|
+
let dp = dOff;
|
|
214
|
+
for (let j = 0; j < diagCount; j++) {
|
|
215
|
+
const sev = SEVERITY[_u8[dp]]; dp++;
|
|
216
|
+
const ds = _p(dv.getUint32(dp, true)); dp += 4;
|
|
217
|
+
const de = _p(dv.getUint32(dp, true)); dp += 4;
|
|
218
|
+
const ml = dv.getUint32(dp, true); dp += 4;
|
|
219
|
+
const msg = _td.decode(_u8.subarray(dp, dp + ml)); dp += ml;
|
|
220
|
+
const hh = _u8[dp]; dp++;
|
|
221
|
+
let help = null;
|
|
222
|
+
if (hh) { const hl = dv.getUint32(dp, true); dp += 4; help = _td.decode(_u8.subarray(dp, dp + hl)); dp += hl; }
|
|
223
|
+
const lc = dv.getUint32(dp, true); dp += 4;
|
|
224
|
+
const labels = new Array(lc);
|
|
225
|
+
for (let k = 0; k < lc; k++) {
|
|
226
|
+
const ls = _p(dv.getUint32(dp, true)); dp += 4;
|
|
227
|
+
const le = _p(dv.getUint32(dp, true)); dp += 4;
|
|
228
|
+
const lml = dv.getUint32(dp, true); dp += 4;
|
|
229
|
+
labels[k] = { start: ls, end: le, message: _td.decode(_u8.subarray(dp, dp + lml)) }; dp += lml;
|
|
230
|
+
}
|
|
231
|
+
diagnostics[j] = { severity: sev, message: msg, start: ds, end: de, help, labels };
|
|
232
|
+
}
|
|
233
|
+
const program = node(progIdx);
|
|
234
|
+
_u8 = _u32 = _src = null;
|
|
235
|
+
return { program, comments, diagnostics };
|
|
236
|
+
}
|
|
237
|
+
export { decode };
|
package/index.d.ts
ADDED
|
@@ -0,0 +1,621 @@
|
|
|
1
|
+
/** How the source code should be parsed. */
|
|
2
|
+
type SourceType = "script" | "module";
|
|
3
|
+
|
|
4
|
+
/** Language variant of the source code. */
|
|
5
|
+
type SourceLang = "js" | "ts" | "jsx" | "tsx" | "dts";
|
|
6
|
+
|
|
7
|
+
/** Options for configuring the parser. */
|
|
8
|
+
interface ParseOptions {
|
|
9
|
+
/**
|
|
10
|
+
* Parse as a classic script or an ES module.
|
|
11
|
+
* Module mode enables `import`/`export`, `import.meta`, top-level `await`,
|
|
12
|
+
* and strict mode.
|
|
13
|
+
* @default "module"
|
|
14
|
+
*/
|
|
15
|
+
sourceType?: SourceType;
|
|
16
|
+
/**
|
|
17
|
+
* Language variant controls which syntax extensions are enabled.
|
|
18
|
+
* @default "js"
|
|
19
|
+
*/
|
|
20
|
+
lang?: SourceLang;
|
|
21
|
+
/**
|
|
22
|
+
* Run semantic analysis after parsing and include semantic errors
|
|
23
|
+
* (e.g. duplicate declarations, invalid `break`/`continue` targets)
|
|
24
|
+
* alongside syntax errors. This requires a separate AST pass and may
|
|
25
|
+
* affect performance slightly.
|
|
26
|
+
* @default false
|
|
27
|
+
*/
|
|
28
|
+
semanticErrors?: boolean;
|
|
29
|
+
}
|
|
30
|
+
|
|
31
|
+
/** A source code comment. */
|
|
32
|
+
interface Comment {
|
|
33
|
+
type: "Line" | "Block";
|
|
34
|
+
/** Comment text without the delimiters. */
|
|
35
|
+
value: string;
|
|
36
|
+
/** Byte offset. */
|
|
37
|
+
start: number;
|
|
38
|
+
/** Byte offset. */
|
|
39
|
+
end: number;
|
|
40
|
+
}
|
|
41
|
+
|
|
42
|
+
/** A labeled source span attached to a {@link Diagnostic}. */
|
|
43
|
+
interface DiagnosticLabel {
|
|
44
|
+
/** Byte offset. */
|
|
45
|
+
start: number;
|
|
46
|
+
/** Byte offset. */
|
|
47
|
+
end: number;
|
|
48
|
+
message: string;
|
|
49
|
+
}
|
|
50
|
+
|
|
51
|
+
/**
|
|
52
|
+
* A diagnostic produced during parsing or semantic analysis.
|
|
53
|
+
* The parser is error tolerant: an AST is always produced even when diagnostics exist.
|
|
54
|
+
*/
|
|
55
|
+
interface Diagnostic {
|
|
56
|
+
severity: "error" | "warning" | "hint" | "info";
|
|
57
|
+
message: string;
|
|
58
|
+
/** Fix suggestion, or `null` if unavailable. */
|
|
59
|
+
help: string | null;
|
|
60
|
+
/** Byte offset. */
|
|
61
|
+
start: number;
|
|
62
|
+
/** Byte offset. */
|
|
63
|
+
end: number;
|
|
64
|
+
/** Additional source spans providing context. */
|
|
65
|
+
labels: DiagnosticLabel[];
|
|
66
|
+
}
|
|
67
|
+
|
|
68
|
+
/** The result returned by the parser. */
|
|
69
|
+
interface ParseResult {
|
|
70
|
+
/** Root ESTree/TypeScript-ESTree AST node. */
|
|
71
|
+
program: Program;
|
|
72
|
+
/** All comments in source order. */
|
|
73
|
+
comments: Comment[];
|
|
74
|
+
/** Syntax diagnostics, and semantic diagnostics when {@link ParseOptions.semanticErrors} is enabled. */
|
|
75
|
+
diagnostics: Diagnostic[];
|
|
76
|
+
}
|
|
77
|
+
|
|
78
|
+
/**
|
|
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.
|
|
85
|
+
*/
|
|
86
|
+
export function parse(source: string, options?: ParseOptions): Promise<ParseResult>;
|
|
87
|
+
|
|
88
|
+
// AST node types
|
|
89
|
+
|
|
90
|
+
interface BaseNode {
|
|
91
|
+
start: number;
|
|
92
|
+
end: number;
|
|
93
|
+
}
|
|
94
|
+
|
|
95
|
+
type BinaryOperator = "==" | "!=" | "===" | "!==" | "<" | "<=" | ">" | ">=" | "+" | "-" | "*" | "/" | "%" | "**" | "|" | "^" | "&" | "<<" | ">>" | ">>>" | "in" | "instanceof";
|
|
96
|
+
type LogicalOperator = "&&" | "||" | "??";
|
|
97
|
+
type UnaryOperator = "-" | "+" | "!" | "~" | "typeof" | "void" | "delete";
|
|
98
|
+
type UpdateOperator = "++" | "--";
|
|
99
|
+
type AssignmentOperator = "=" | "+=" | "-=" | "*=" | "/=" | "%=" | "**=" | "<<=" | ">>=" | ">>>=" | "|=" | "^=" | "&=" | "||=" | "&&=" | "??=";
|
|
100
|
+
|
|
101
|
+
interface Identifier extends BaseNode {
|
|
102
|
+
type: "Identifier";
|
|
103
|
+
name: string;
|
|
104
|
+
}
|
|
105
|
+
interface PrivateIdentifier extends BaseNode {
|
|
106
|
+
type: "PrivateIdentifier";
|
|
107
|
+
name: string;
|
|
108
|
+
}
|
|
109
|
+
interface StringLiteral extends BaseNode {
|
|
110
|
+
type: "Literal";
|
|
111
|
+
value: string;
|
|
112
|
+
raw: string;
|
|
113
|
+
}
|
|
114
|
+
interface NumericLiteral extends BaseNode {
|
|
115
|
+
type: "Literal";
|
|
116
|
+
value: number | null;
|
|
117
|
+
raw: string;
|
|
118
|
+
}
|
|
119
|
+
interface BigIntLiteral extends BaseNode {
|
|
120
|
+
type: "Literal";
|
|
121
|
+
value: bigint;
|
|
122
|
+
raw: string;
|
|
123
|
+
bigint: string;
|
|
124
|
+
}
|
|
125
|
+
interface BooleanLiteral extends BaseNode {
|
|
126
|
+
type: "Literal";
|
|
127
|
+
value: boolean;
|
|
128
|
+
raw: string;
|
|
129
|
+
}
|
|
130
|
+
interface NullLiteral extends BaseNode {
|
|
131
|
+
type: "Literal";
|
|
132
|
+
value: null;
|
|
133
|
+
raw: "null";
|
|
134
|
+
}
|
|
135
|
+
interface RegExpLiteral extends BaseNode {
|
|
136
|
+
type: "Literal";
|
|
137
|
+
value: RegExp | null;
|
|
138
|
+
raw: string;
|
|
139
|
+
regex: {
|
|
140
|
+
pattern: string;
|
|
141
|
+
flags: string;
|
|
142
|
+
};
|
|
143
|
+
}
|
|
144
|
+
type Literal = StringLiteral | NumericLiteral | BigIntLiteral | BooleanLiteral | NullLiteral | RegExpLiteral;
|
|
145
|
+
interface ArrayPattern extends BaseNode {
|
|
146
|
+
type: "ArrayPattern";
|
|
147
|
+
elements: Array<BindingPattern | RestElement | null>;
|
|
148
|
+
}
|
|
149
|
+
interface ObjectPattern extends BaseNode {
|
|
150
|
+
type: "ObjectPattern";
|
|
151
|
+
properties: Array<Property | RestElement>;
|
|
152
|
+
}
|
|
153
|
+
interface AssignmentPattern extends BaseNode {
|
|
154
|
+
type: "AssignmentPattern";
|
|
155
|
+
left: BindingPattern;
|
|
156
|
+
right: Expression;
|
|
157
|
+
}
|
|
158
|
+
interface RestElement extends BaseNode {
|
|
159
|
+
type: "RestElement";
|
|
160
|
+
argument: BindingPattern;
|
|
161
|
+
}
|
|
162
|
+
type BindingPattern = Identifier | ArrayPattern | ObjectPattern | AssignmentPattern;
|
|
163
|
+
type FunctionParameter = BindingPattern | RestElement;
|
|
164
|
+
interface Property extends BaseNode {
|
|
165
|
+
type: "Property";
|
|
166
|
+
kind: "init" | "get" | "set";
|
|
167
|
+
key: Expression;
|
|
168
|
+
value: Expression | BindingPattern;
|
|
169
|
+
method: boolean;
|
|
170
|
+
shorthand: boolean;
|
|
171
|
+
computed: boolean;
|
|
172
|
+
}
|
|
173
|
+
interface SequenceExpression extends BaseNode {
|
|
174
|
+
type: "SequenceExpression";
|
|
175
|
+
expressions: Expression[];
|
|
176
|
+
}
|
|
177
|
+
interface ParenthesizedExpression extends BaseNode {
|
|
178
|
+
type: "ParenthesizedExpression";
|
|
179
|
+
expression: Expression;
|
|
180
|
+
}
|
|
181
|
+
interface BinaryExpression extends BaseNode {
|
|
182
|
+
type: "BinaryExpression";
|
|
183
|
+
left: Expression;
|
|
184
|
+
operator: BinaryOperator;
|
|
185
|
+
right: Expression;
|
|
186
|
+
}
|
|
187
|
+
interface LogicalExpression extends BaseNode {
|
|
188
|
+
type: "LogicalExpression";
|
|
189
|
+
left: Expression;
|
|
190
|
+
operator: LogicalOperator;
|
|
191
|
+
right: Expression;
|
|
192
|
+
}
|
|
193
|
+
interface ConditionalExpression extends BaseNode {
|
|
194
|
+
type: "ConditionalExpression";
|
|
195
|
+
test: Expression;
|
|
196
|
+
consequent: Expression;
|
|
197
|
+
alternate: Expression;
|
|
198
|
+
}
|
|
199
|
+
interface UnaryExpression extends BaseNode {
|
|
200
|
+
type: "UnaryExpression";
|
|
201
|
+
operator: UnaryOperator;
|
|
202
|
+
prefix: true;
|
|
203
|
+
argument: Expression;
|
|
204
|
+
}
|
|
205
|
+
interface UpdateExpression extends BaseNode {
|
|
206
|
+
type: "UpdateExpression";
|
|
207
|
+
operator: UpdateOperator;
|
|
208
|
+
prefix: boolean;
|
|
209
|
+
argument: Expression;
|
|
210
|
+
}
|
|
211
|
+
interface AssignmentExpression extends BaseNode {
|
|
212
|
+
type: "AssignmentExpression";
|
|
213
|
+
operator: AssignmentOperator;
|
|
214
|
+
left: Expression | BindingPattern;
|
|
215
|
+
right: Expression;
|
|
216
|
+
}
|
|
217
|
+
interface YieldExpression extends BaseNode {
|
|
218
|
+
type: "YieldExpression";
|
|
219
|
+
delegate: boolean;
|
|
220
|
+
argument: Expression | null;
|
|
221
|
+
}
|
|
222
|
+
interface AwaitExpression extends BaseNode {
|
|
223
|
+
type: "AwaitExpression";
|
|
224
|
+
argument: Expression;
|
|
225
|
+
}
|
|
226
|
+
interface ArrayExpression extends BaseNode {
|
|
227
|
+
type: "ArrayExpression";
|
|
228
|
+
elements: Array<Expression | SpreadElement | null>;
|
|
229
|
+
}
|
|
230
|
+
interface ObjectExpression extends BaseNode {
|
|
231
|
+
type: "ObjectExpression";
|
|
232
|
+
properties: Array<Property | SpreadElement>;
|
|
233
|
+
}
|
|
234
|
+
interface SpreadElement extends BaseNode {
|
|
235
|
+
type: "SpreadElement";
|
|
236
|
+
argument: Expression;
|
|
237
|
+
}
|
|
238
|
+
interface MemberExpression extends BaseNode {
|
|
239
|
+
type: "MemberExpression";
|
|
240
|
+
object: Expression | Super;
|
|
241
|
+
property: Expression | PrivateIdentifier;
|
|
242
|
+
computed: boolean;
|
|
243
|
+
optional: boolean;
|
|
244
|
+
}
|
|
245
|
+
interface CallExpression extends BaseNode {
|
|
246
|
+
type: "CallExpression";
|
|
247
|
+
callee: Expression | Super;
|
|
248
|
+
arguments: Array<Expression | SpreadElement>;
|
|
249
|
+
optional: boolean;
|
|
250
|
+
}
|
|
251
|
+
interface ChainExpression extends BaseNode {
|
|
252
|
+
type: "ChainExpression";
|
|
253
|
+
expression: CallExpression | MemberExpression;
|
|
254
|
+
}
|
|
255
|
+
interface TaggedTemplateExpression extends BaseNode {
|
|
256
|
+
type: "TaggedTemplateExpression";
|
|
257
|
+
tag: Expression;
|
|
258
|
+
quasi: TemplateLiteral;
|
|
259
|
+
}
|
|
260
|
+
interface NewExpression extends BaseNode {
|
|
261
|
+
type: "NewExpression";
|
|
262
|
+
callee: Expression;
|
|
263
|
+
arguments: Array<Expression | SpreadElement>;
|
|
264
|
+
}
|
|
265
|
+
interface MetaProperty extends BaseNode {
|
|
266
|
+
type: "MetaProperty";
|
|
267
|
+
meta: Identifier;
|
|
268
|
+
property: Identifier;
|
|
269
|
+
}
|
|
270
|
+
interface ImportExpression extends BaseNode {
|
|
271
|
+
type: "ImportExpression";
|
|
272
|
+
source: Expression;
|
|
273
|
+
options: Expression | null;
|
|
274
|
+
phase: "source" | "defer" | null;
|
|
275
|
+
}
|
|
276
|
+
interface TemplateLiteral extends BaseNode {
|
|
277
|
+
type: "TemplateLiteral";
|
|
278
|
+
quasis: TemplateElement[];
|
|
279
|
+
expressions: Expression[];
|
|
280
|
+
}
|
|
281
|
+
interface TemplateElement extends BaseNode {
|
|
282
|
+
type: "TemplateElement";
|
|
283
|
+
value: {
|
|
284
|
+
raw: string;
|
|
285
|
+
cooked: string | null;
|
|
286
|
+
};
|
|
287
|
+
tail: boolean;
|
|
288
|
+
}
|
|
289
|
+
interface Super extends BaseNode {
|
|
290
|
+
type: "Super";
|
|
291
|
+
}
|
|
292
|
+
interface ThisExpression extends BaseNode {
|
|
293
|
+
type: "ThisExpression";
|
|
294
|
+
}
|
|
295
|
+
interface ExpressionStatement extends BaseNode {
|
|
296
|
+
type: "ExpressionStatement";
|
|
297
|
+
expression: Expression;
|
|
298
|
+
directive?: string;
|
|
299
|
+
}
|
|
300
|
+
interface BlockStatement extends BaseNode {
|
|
301
|
+
type: "BlockStatement";
|
|
302
|
+
body: Statement[];
|
|
303
|
+
}
|
|
304
|
+
interface IfStatement extends BaseNode {
|
|
305
|
+
type: "IfStatement";
|
|
306
|
+
test: Expression;
|
|
307
|
+
consequent: Statement;
|
|
308
|
+
alternate: Statement | null;
|
|
309
|
+
}
|
|
310
|
+
interface SwitchStatement extends BaseNode {
|
|
311
|
+
type: "SwitchStatement";
|
|
312
|
+
discriminant: Expression;
|
|
313
|
+
cases: SwitchCase[];
|
|
314
|
+
}
|
|
315
|
+
interface SwitchCase extends BaseNode {
|
|
316
|
+
type: "SwitchCase";
|
|
317
|
+
test: Expression | null;
|
|
318
|
+
consequent: Statement[];
|
|
319
|
+
}
|
|
320
|
+
interface ForStatement extends BaseNode {
|
|
321
|
+
type: "ForStatement";
|
|
322
|
+
init: VariableDeclaration | Expression | null;
|
|
323
|
+
test: Expression | null;
|
|
324
|
+
update: Expression | null;
|
|
325
|
+
body: Statement;
|
|
326
|
+
}
|
|
327
|
+
interface ForInStatement extends BaseNode {
|
|
328
|
+
type: "ForInStatement";
|
|
329
|
+
left: VariableDeclaration | Expression;
|
|
330
|
+
right: Expression;
|
|
331
|
+
body: Statement;
|
|
332
|
+
}
|
|
333
|
+
interface ForOfStatement extends BaseNode {
|
|
334
|
+
type: "ForOfStatement";
|
|
335
|
+
left: VariableDeclaration | Expression;
|
|
336
|
+
right: Expression;
|
|
337
|
+
body: Statement;
|
|
338
|
+
await: boolean;
|
|
339
|
+
}
|
|
340
|
+
interface WhileStatement extends BaseNode {
|
|
341
|
+
type: "WhileStatement";
|
|
342
|
+
test: Expression;
|
|
343
|
+
body: Statement;
|
|
344
|
+
}
|
|
345
|
+
interface DoWhileStatement extends BaseNode {
|
|
346
|
+
type: "DoWhileStatement";
|
|
347
|
+
body: Statement;
|
|
348
|
+
test: Expression;
|
|
349
|
+
}
|
|
350
|
+
interface BreakStatement extends BaseNode {
|
|
351
|
+
type: "BreakStatement";
|
|
352
|
+
label: Identifier | null;
|
|
353
|
+
}
|
|
354
|
+
interface ContinueStatement extends BaseNode {
|
|
355
|
+
type: "ContinueStatement";
|
|
356
|
+
label: Identifier | null;
|
|
357
|
+
}
|
|
358
|
+
interface LabeledStatement extends BaseNode {
|
|
359
|
+
type: "LabeledStatement";
|
|
360
|
+
label: Identifier;
|
|
361
|
+
body: Statement;
|
|
362
|
+
}
|
|
363
|
+
interface WithStatement extends BaseNode {
|
|
364
|
+
type: "WithStatement";
|
|
365
|
+
object: Expression;
|
|
366
|
+
body: Statement;
|
|
367
|
+
}
|
|
368
|
+
interface ReturnStatement extends BaseNode {
|
|
369
|
+
type: "ReturnStatement";
|
|
370
|
+
argument: Expression | null;
|
|
371
|
+
}
|
|
372
|
+
interface ThrowStatement extends BaseNode {
|
|
373
|
+
type: "ThrowStatement";
|
|
374
|
+
argument: Expression;
|
|
375
|
+
}
|
|
376
|
+
interface TryStatement extends BaseNode {
|
|
377
|
+
type: "TryStatement";
|
|
378
|
+
block: BlockStatement;
|
|
379
|
+
handler: CatchClause | null;
|
|
380
|
+
finalizer: BlockStatement | null;
|
|
381
|
+
}
|
|
382
|
+
interface CatchClause extends BaseNode {
|
|
383
|
+
type: "CatchClause";
|
|
384
|
+
param: BindingPattern | null;
|
|
385
|
+
body: BlockStatement;
|
|
386
|
+
}
|
|
387
|
+
interface DebuggerStatement extends BaseNode {
|
|
388
|
+
type: "DebuggerStatement";
|
|
389
|
+
}
|
|
390
|
+
interface EmptyStatement extends BaseNode {
|
|
391
|
+
type: "EmptyStatement";
|
|
392
|
+
}
|
|
393
|
+
interface VariableDeclaration extends BaseNode {
|
|
394
|
+
type: "VariableDeclaration";
|
|
395
|
+
kind: "var" | "let" | "const" | "using" | "await using";
|
|
396
|
+
declarations: VariableDeclarator[];
|
|
397
|
+
}
|
|
398
|
+
interface VariableDeclarator extends BaseNode {
|
|
399
|
+
type: "VariableDeclarator";
|
|
400
|
+
id: BindingPattern;
|
|
401
|
+
init: Expression | null;
|
|
402
|
+
}
|
|
403
|
+
interface FunctionNodeBase extends BaseNode {
|
|
404
|
+
id: Identifier | null;
|
|
405
|
+
generator: boolean;
|
|
406
|
+
async: boolean;
|
|
407
|
+
declare?: boolean;
|
|
408
|
+
params: FunctionParameter[];
|
|
409
|
+
body: BlockStatement | null;
|
|
410
|
+
expression: false;
|
|
411
|
+
}
|
|
412
|
+
interface FunctionDeclaration extends FunctionNodeBase {
|
|
413
|
+
type: "FunctionDeclaration";
|
|
414
|
+
}
|
|
415
|
+
interface FunctionExpression extends FunctionNodeBase {
|
|
416
|
+
type: "FunctionExpression";
|
|
417
|
+
}
|
|
418
|
+
interface TSDeclareFunction extends FunctionNodeBase {
|
|
419
|
+
type: "TSDeclareFunction";
|
|
420
|
+
}
|
|
421
|
+
interface TSEmptyBodyFunctionExpression extends FunctionNodeBase {
|
|
422
|
+
type: "TSEmptyBodyFunctionExpression";
|
|
423
|
+
}
|
|
424
|
+
interface ArrowFunctionExpression extends BaseNode {
|
|
425
|
+
type: "ArrowFunctionExpression";
|
|
426
|
+
id: null;
|
|
427
|
+
generator: false;
|
|
428
|
+
async: boolean;
|
|
429
|
+
params: FunctionParameter[];
|
|
430
|
+
body: BlockStatement | Expression;
|
|
431
|
+
expression: boolean;
|
|
432
|
+
}
|
|
433
|
+
interface ClassNodeBase extends BaseNode {
|
|
434
|
+
decorators: Decorator[];
|
|
435
|
+
id: Identifier | null;
|
|
436
|
+
superClass: Expression | null;
|
|
437
|
+
body: ClassBody;
|
|
438
|
+
}
|
|
439
|
+
interface ClassDeclaration extends ClassNodeBase {
|
|
440
|
+
type: "ClassDeclaration";
|
|
441
|
+
}
|
|
442
|
+
interface ClassExpression extends ClassNodeBase {
|
|
443
|
+
type: "ClassExpression";
|
|
444
|
+
}
|
|
445
|
+
interface ClassBody extends BaseNode {
|
|
446
|
+
type: "ClassBody";
|
|
447
|
+
body: ClassElement[];
|
|
448
|
+
}
|
|
449
|
+
interface MethodDefinition extends BaseNode {
|
|
450
|
+
type: "MethodDefinition";
|
|
451
|
+
decorators: Decorator[];
|
|
452
|
+
key: Expression | PrivateIdentifier;
|
|
453
|
+
value: FunctionExpression | TSEmptyBodyFunctionExpression;
|
|
454
|
+
kind: "constructor" | "method" | "get" | "set";
|
|
455
|
+
computed: boolean;
|
|
456
|
+
static: boolean;
|
|
457
|
+
}
|
|
458
|
+
interface PropertyDefinition extends BaseNode {
|
|
459
|
+
type: "PropertyDefinition";
|
|
460
|
+
decorators: Decorator[];
|
|
461
|
+
key: Expression | PrivateIdentifier;
|
|
462
|
+
value: Expression | null;
|
|
463
|
+
computed: boolean;
|
|
464
|
+
static: boolean;
|
|
465
|
+
}
|
|
466
|
+
interface AccessorProperty extends BaseNode {
|
|
467
|
+
type: "AccessorProperty";
|
|
468
|
+
decorators: Decorator[];
|
|
469
|
+
key: Expression | PrivateIdentifier;
|
|
470
|
+
value: Expression | null;
|
|
471
|
+
computed: boolean;
|
|
472
|
+
static: boolean;
|
|
473
|
+
}
|
|
474
|
+
interface StaticBlock extends BaseNode {
|
|
475
|
+
type: "StaticBlock";
|
|
476
|
+
body: Statement[];
|
|
477
|
+
}
|
|
478
|
+
interface Decorator extends BaseNode {
|
|
479
|
+
type: "Decorator";
|
|
480
|
+
expression: Expression;
|
|
481
|
+
}
|
|
482
|
+
type ClassElement = MethodDefinition | PropertyDefinition | AccessorProperty | StaticBlock;
|
|
483
|
+
interface ImportDeclaration extends BaseNode {
|
|
484
|
+
type: "ImportDeclaration";
|
|
485
|
+
specifiers: Array<ImportSpecifier | ImportDefaultSpecifier | ImportNamespaceSpecifier>;
|
|
486
|
+
source: StringLiteral;
|
|
487
|
+
phase: "source" | "defer" | null;
|
|
488
|
+
attributes: ImportAttribute[];
|
|
489
|
+
}
|
|
490
|
+
interface ImportSpecifier extends BaseNode {
|
|
491
|
+
type: "ImportSpecifier";
|
|
492
|
+
imported: Identifier | StringLiteral;
|
|
493
|
+
local: Identifier;
|
|
494
|
+
}
|
|
495
|
+
interface ImportDefaultSpecifier extends BaseNode {
|
|
496
|
+
type: "ImportDefaultSpecifier";
|
|
497
|
+
local: Identifier;
|
|
498
|
+
}
|
|
499
|
+
interface ImportNamespaceSpecifier extends BaseNode {
|
|
500
|
+
type: "ImportNamespaceSpecifier";
|
|
501
|
+
local: Identifier;
|
|
502
|
+
}
|
|
503
|
+
interface ImportAttribute extends BaseNode {
|
|
504
|
+
type: "ImportAttribute";
|
|
505
|
+
key: Identifier | StringLiteral;
|
|
506
|
+
value: StringLiteral;
|
|
507
|
+
}
|
|
508
|
+
interface ExportNamedDeclaration extends BaseNode {
|
|
509
|
+
type: "ExportNamedDeclaration";
|
|
510
|
+
declaration: Declaration | null;
|
|
511
|
+
specifiers: ExportSpecifier[];
|
|
512
|
+
source: StringLiteral | null;
|
|
513
|
+
attributes: ImportAttribute[];
|
|
514
|
+
}
|
|
515
|
+
interface ExportDefaultDeclaration extends BaseNode {
|
|
516
|
+
type: "ExportDefaultDeclaration";
|
|
517
|
+
declaration: Declaration | Expression;
|
|
518
|
+
}
|
|
519
|
+
interface ExportAllDeclaration extends BaseNode {
|
|
520
|
+
type: "ExportAllDeclaration";
|
|
521
|
+
exported: Identifier | StringLiteral | null;
|
|
522
|
+
source: StringLiteral;
|
|
523
|
+
attributes: ImportAttribute[];
|
|
524
|
+
}
|
|
525
|
+
interface ExportSpecifier extends BaseNode {
|
|
526
|
+
type: "ExportSpecifier";
|
|
527
|
+
local: Identifier | StringLiteral;
|
|
528
|
+
exported: Identifier | StringLiteral;
|
|
529
|
+
}
|
|
530
|
+
interface TSExportAssignment extends BaseNode {
|
|
531
|
+
type: "TSExportAssignment";
|
|
532
|
+
expression: Expression;
|
|
533
|
+
}
|
|
534
|
+
interface TSNamespaceExportDeclaration extends BaseNode {
|
|
535
|
+
type: "TSNamespaceExportDeclaration";
|
|
536
|
+
id: Identifier;
|
|
537
|
+
}
|
|
538
|
+
interface JSXElement extends BaseNode {
|
|
539
|
+
type: "JSXElement";
|
|
540
|
+
openingElement: JSXOpeningElement;
|
|
541
|
+
children: JSXChild[];
|
|
542
|
+
closingElement: JSXClosingElement | null;
|
|
543
|
+
}
|
|
544
|
+
interface JSXOpeningElement extends BaseNode {
|
|
545
|
+
type: "JSXOpeningElement";
|
|
546
|
+
name: JSXTagName;
|
|
547
|
+
attributes: Array<JSXAttribute | JSXSpreadAttribute>;
|
|
548
|
+
selfClosing: boolean;
|
|
549
|
+
}
|
|
550
|
+
interface JSXClosingElement extends BaseNode {
|
|
551
|
+
type: "JSXClosingElement";
|
|
552
|
+
name: JSXTagName;
|
|
553
|
+
}
|
|
554
|
+
interface JSXFragment extends BaseNode {
|
|
555
|
+
type: "JSXFragment";
|
|
556
|
+
openingFragment: JSXOpeningFragment;
|
|
557
|
+
children: JSXChild[];
|
|
558
|
+
closingFragment: JSXClosingFragment;
|
|
559
|
+
}
|
|
560
|
+
interface JSXOpeningFragment extends BaseNode {
|
|
561
|
+
type: "JSXOpeningFragment";
|
|
562
|
+
attributes: [];
|
|
563
|
+
selfClosing: false;
|
|
564
|
+
}
|
|
565
|
+
interface JSXClosingFragment extends BaseNode {
|
|
566
|
+
type: "JSXClosingFragment";
|
|
567
|
+
}
|
|
568
|
+
interface JSXIdentifier extends BaseNode {
|
|
569
|
+
type: "JSXIdentifier";
|
|
570
|
+
name: string;
|
|
571
|
+
}
|
|
572
|
+
interface JSXNamespacedName extends BaseNode {
|
|
573
|
+
type: "JSXNamespacedName";
|
|
574
|
+
namespace: JSXIdentifier;
|
|
575
|
+
name: JSXIdentifier;
|
|
576
|
+
}
|
|
577
|
+
interface JSXMemberExpression extends BaseNode {
|
|
578
|
+
type: "JSXMemberExpression";
|
|
579
|
+
object: JSXIdentifier | JSXMemberExpression;
|
|
580
|
+
property: JSXIdentifier;
|
|
581
|
+
}
|
|
582
|
+
interface JSXAttribute extends BaseNode {
|
|
583
|
+
type: "JSXAttribute";
|
|
584
|
+
name: JSXIdentifier | JSXNamespacedName;
|
|
585
|
+
value: StringLiteral | JSXExpressionContainer | JSXElement | JSXFragment | null;
|
|
586
|
+
}
|
|
587
|
+
interface JSXSpreadAttribute extends BaseNode {
|
|
588
|
+
type: "JSXSpreadAttribute";
|
|
589
|
+
argument: Expression;
|
|
590
|
+
}
|
|
591
|
+
interface JSXExpressionContainer extends BaseNode {
|
|
592
|
+
type: "JSXExpressionContainer";
|
|
593
|
+
expression: Expression | JSXEmptyExpression;
|
|
594
|
+
}
|
|
595
|
+
interface JSXEmptyExpression extends BaseNode {
|
|
596
|
+
type: "JSXEmptyExpression";
|
|
597
|
+
}
|
|
598
|
+
interface JSXText extends BaseNode {
|
|
599
|
+
type: "JSXText";
|
|
600
|
+
value: string;
|
|
601
|
+
raw: string;
|
|
602
|
+
}
|
|
603
|
+
interface JSXSpreadChild extends BaseNode {
|
|
604
|
+
type: "JSXSpreadChild";
|
|
605
|
+
expression: Expression;
|
|
606
|
+
}
|
|
607
|
+
type JSXTagName = JSXIdentifier | JSXNamespacedName | JSXMemberExpression;
|
|
608
|
+
type JSXChild = JSXText | JSXElement | JSXFragment | JSXExpressionContainer | JSXSpreadChild;
|
|
609
|
+
interface Program extends BaseNode {
|
|
610
|
+
type: "Program";
|
|
611
|
+
sourceType: "module" | "script";
|
|
612
|
+
hashbang: string | null;
|
|
613
|
+
body: Array<Statement | ModuleDeclaration>;
|
|
614
|
+
}
|
|
615
|
+
type Declaration = FunctionDeclaration | ClassDeclaration | VariableDeclaration | TSDeclareFunction;
|
|
616
|
+
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;
|
|
617
|
+
type Statement = ExpressionStatement | BlockStatement | EmptyStatement | DebuggerStatement | ReturnStatement | LabeledStatement | BreakStatement | ContinueStatement | IfStatement | SwitchStatement | ThrowStatement | TryStatement | WhileStatement | DoWhileStatement | ForStatement | ForInStatement | ForOfStatement | WithStatement | Declaration;
|
|
618
|
+
type ModuleDeclaration = ImportDeclaration | ExportNamedDeclaration | ExportDefaultDeclaration | ExportAllDeclaration | TSExportAssignment | TSNamespaceExportDeclaration;
|
|
619
|
+
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
|
+
|
|
621
|
+
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
ADDED
|
@@ -0,0 +1,12 @@
|
|
|
1
|
+
import binding from './binding.js';
|
|
2
|
+
import { decode } from './decode.js';
|
|
3
|
+
|
|
4
|
+
export function parseSync(source, options) {
|
|
5
|
+
const buffer = binding.parseSync(source, options ?? {});
|
|
6
|
+
return decode(buffer, source);
|
|
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,45 +1,28 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "yuku-parser",
|
|
3
|
-
"
|
|
4
|
-
"
|
|
3
|
+
"version": "0.3.0",
|
|
4
|
+
"description": "High-performance JavaScript/TypeScript parser",
|
|
5
|
+
"license": "MIT",
|
|
5
6
|
"type": "module",
|
|
7
|
+
"main": "index.js",
|
|
8
|
+
"types": "index.d.ts",
|
|
6
9
|
"files": [
|
|
7
|
-
"
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
".": {
|
|
12
|
-
"import": "./dist/index.js"
|
|
13
|
-
},
|
|
14
|
-
"./package.json": "./package.json"
|
|
15
|
-
},
|
|
16
|
-
"funding": "https://github.com/sponsors/arshad-yaseen",
|
|
17
|
-
"homepage": "https://yuku.fyi",
|
|
18
|
-
"license": "MIT",
|
|
19
|
-
"maintainers": [
|
|
20
|
-
{
|
|
21
|
-
"name": "Arshad Yaseen",
|
|
22
|
-
"email": "arshadpyaseen@gmail.com",
|
|
23
|
-
"url": "https://arshad.fyi"
|
|
24
|
-
}
|
|
10
|
+
"index.js",
|
|
11
|
+
"index.d.ts",
|
|
12
|
+
"binding.js",
|
|
13
|
+
"decode.js"
|
|
25
14
|
],
|
|
26
|
-
"
|
|
27
|
-
"
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
"
|
|
31
|
-
|
|
32
|
-
|
|
33
|
-
"
|
|
34
|
-
|
|
35
|
-
|
|
36
|
-
|
|
37
|
-
|
|
38
|
-
"repository": {
|
|
39
|
-
"type": "git",
|
|
40
|
-
"url": "git+https://github.com/yuku-toolchain/yuku.git"
|
|
41
|
-
},
|
|
42
|
-
"devDependencies": {
|
|
43
|
-
"bunup": "^0.16.22"
|
|
15
|
+
"optionalDependencies": {
|
|
16
|
+
"@yuku-parser/binding-linux-x64-gnu": "0.3.0",
|
|
17
|
+
"@yuku-parser/binding-linux-arm64-gnu": "0.3.0",
|
|
18
|
+
"@yuku-parser/binding-linux-arm-gnu": "0.3.0",
|
|
19
|
+
"@yuku-parser/binding-linux-x64-musl": "0.3.0",
|
|
20
|
+
"@yuku-parser/binding-linux-arm64-musl": "0.3.0",
|
|
21
|
+
"@yuku-parser/binding-linux-arm-musl": "0.3.0",
|
|
22
|
+
"@yuku-parser/binding-darwin-x64": "0.3.0",
|
|
23
|
+
"@yuku-parser/binding-darwin-arm64": "0.3.0",
|
|
24
|
+
"@yuku-parser/binding-win32-x64": "0.3.0",
|
|
25
|
+
"@yuku-parser/binding-win32-arm64": "0.3.0",
|
|
26
|
+
"@yuku-parser/binding-freebsd-x64": "0.3.0"
|
|
44
27
|
}
|
|
45
28
|
}
|
package/dist/index.js
DELETED