@rsvelte/vite-plugin-svelte-native 0.2.1 → 0.2.2
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/index.cjs +8 -0
- package/package.json +7 -6
- package/parse-envelope.js +1836 -0
|
@@ -0,0 +1,1836 @@
|
|
|
1
|
+
// Decoder for the parse envelope (`napi_raw_parse.rs`).
|
|
2
|
+
//
|
|
3
|
+
// Reads the rsvelte raw-transfer buffer and reconstructs the same JS
|
|
4
|
+
// object graph that `JSON.parse(napi.parse(source))` produces. Heavy
|
|
5
|
+
// nodes (Expression sub-trees, the CSS StyleSheet, SvelteOptions)
|
|
6
|
+
// still travel as inline JSON behind `TAG_JSON` — those slices are
|
|
7
|
+
// handed to `JSON.parse` on the spot. Everything else is read by
|
|
8
|
+
// `DataView`, so we skip the per-byte tokenization that JSON.parse
|
|
9
|
+
// performs on most of the AST.
|
|
10
|
+
//
|
|
11
|
+
// Wire format: see `napi_raw_parse.rs` for the canonical reference.
|
|
12
|
+
|
|
13
|
+
'use strict';
|
|
14
|
+
|
|
15
|
+
const MAGIC = 0x3156_5052; // "RPV1" little-endian
|
|
16
|
+
const VERSION = 1;
|
|
17
|
+
const HEADER_LEN = 24;
|
|
18
|
+
|
|
19
|
+
// Tags — must mirror napi_raw_parse.rs.
|
|
20
|
+
const TAG_JSON = 0x00;
|
|
21
|
+
const TAG_ROOT = 0x01;
|
|
22
|
+
const TAG_FRAGMENT = 0x02;
|
|
23
|
+
const TAG_JS_COMMENT = 0x03;
|
|
24
|
+
|
|
25
|
+
const TAG_TEXT = 0x10;
|
|
26
|
+
const TAG_COMMENT = 0x11;
|
|
27
|
+
const TAG_EXPRESSION_TAG = 0x12;
|
|
28
|
+
const TAG_HTML_TAG = 0x13;
|
|
29
|
+
const TAG_CONST_TAG = 0x14;
|
|
30
|
+
const TAG_DEBUG_TAG = 0x15;
|
|
31
|
+
const TAG_RENDER_TAG = 0x16;
|
|
32
|
+
const TAG_ATTACH_TAG = 0x17;
|
|
33
|
+
|
|
34
|
+
const TAG_REGULAR_ELEMENT = 0x20;
|
|
35
|
+
const TAG_COMPONENT = 0x21;
|
|
36
|
+
const TAG_TITLE_ELEMENT = 0x22;
|
|
37
|
+
const TAG_SLOT_ELEMENT = 0x23;
|
|
38
|
+
const TAG_SVELTE_BODY = 0x24;
|
|
39
|
+
const TAG_SVELTE_DOCUMENT = 0x25;
|
|
40
|
+
const TAG_SVELTE_FRAGMENT = 0x26;
|
|
41
|
+
const TAG_SVELTE_BOUNDARY = 0x27;
|
|
42
|
+
const TAG_SVELTE_HEAD = 0x28;
|
|
43
|
+
const TAG_SVELTE_OPTIONS_EL = 0x29;
|
|
44
|
+
const TAG_SVELTE_SELF = 0x2a;
|
|
45
|
+
const TAG_SVELTE_WINDOW = 0x2b;
|
|
46
|
+
const TAG_SVELTE_COMPONENT = 0x2c;
|
|
47
|
+
const TAG_SVELTE_ELEMENT = 0x2d;
|
|
48
|
+
|
|
49
|
+
const TAG_SCRIPT = 0x40;
|
|
50
|
+
// const TAG_SVELTE_OPTIONS = 0x41; — currently encoded as TAG_JSON.
|
|
51
|
+
|
|
52
|
+
const TAG_ATTRIBUTE = 0x50;
|
|
53
|
+
const TAG_SPREAD_ATTRIBUTE = 0x51;
|
|
54
|
+
const TAG_BIND_DIRECTIVE = 0x52;
|
|
55
|
+
const TAG_ON_DIRECTIVE = 0x53;
|
|
56
|
+
const TAG_CLASS_DIRECTIVE = 0x54;
|
|
57
|
+
const TAG_STYLE_DIRECTIVE = 0x55;
|
|
58
|
+
const TAG_TRANSITION_DIRECTIVE = 0x56;
|
|
59
|
+
const TAG_ANIMATE_DIRECTIVE = 0x57;
|
|
60
|
+
const TAG_USE_DIRECTIVE = 0x58;
|
|
61
|
+
const TAG_LET_DIRECTIVE = 0x59;
|
|
62
|
+
|
|
63
|
+
const ATTRVAL_TRUE = 0x60;
|
|
64
|
+
const ATTRVAL_EXPRESSION = 0x61;
|
|
65
|
+
const ATTRVAL_SEQUENCE = 0x62;
|
|
66
|
+
|
|
67
|
+
// Header flag bits — see napi_raw_parse::FLAG_*.
|
|
68
|
+
const FLAG_JSNODE_NO_LOC = 1 << 0;
|
|
69
|
+
const FLAG_CSS_STUB_ONLY = 1 << 1;
|
|
70
|
+
|
|
71
|
+
const TAG_IF_BLOCK = 0x70;
|
|
72
|
+
const TAG_EACH_BLOCK = 0x71;
|
|
73
|
+
const TAG_AWAIT_BLOCK = 0x72;
|
|
74
|
+
const TAG_KEY_BLOCK = 0x73;
|
|
75
|
+
const TAG_SNIPPET_BLOCK = 0x74;
|
|
76
|
+
|
|
77
|
+
// JsNode (estree) tags — 0x80..0xCB. Mirrors `napi_raw_parse.rs`.
|
|
78
|
+
const JS_IDENTIFIER = 0x80;
|
|
79
|
+
const JS_PRIVATE_IDENTIFIER = 0x81;
|
|
80
|
+
const JS_LITERAL = 0x82;
|
|
81
|
+
const JS_BINARY_EXPRESSION = 0x83;
|
|
82
|
+
const JS_LOGICAL_EXPRESSION = 0x84;
|
|
83
|
+
const JS_UNARY_EXPRESSION = 0x85;
|
|
84
|
+
const JS_CONDITIONAL_EXPRESSION = 0x86;
|
|
85
|
+
const JS_CALL_EXPRESSION = 0x87;
|
|
86
|
+
const JS_MEMBER_EXPRESSION = 0x88;
|
|
87
|
+
const JS_NEW_EXPRESSION = 0x89;
|
|
88
|
+
const JS_FUNCTION_EXPRESSION = 0x8a;
|
|
89
|
+
const JS_CLASS_EXPRESSION = 0x8b;
|
|
90
|
+
const JS_ARROW_FUNCTION_EXPRESSION = 0x8c;
|
|
91
|
+
const JS_ASSIGNMENT_EXPRESSION = 0x8d;
|
|
92
|
+
const JS_UPDATE_EXPRESSION = 0x8e;
|
|
93
|
+
const JS_SEQUENCE_EXPRESSION = 0x8f;
|
|
94
|
+
const JS_ARRAY_EXPRESSION = 0x90;
|
|
95
|
+
const JS_OBJECT_EXPRESSION = 0x91;
|
|
96
|
+
const JS_TEMPLATE_LITERAL = 0x92;
|
|
97
|
+
const JS_TAGGED_TEMPLATE_EXPRESSION = 0x93;
|
|
98
|
+
const JS_TEMPLATE_ELEMENT = 0x94;
|
|
99
|
+
const JS_THIS_EXPRESSION = 0x95;
|
|
100
|
+
const JS_SUPER = 0x96;
|
|
101
|
+
const JS_IMPORT_EXPRESSION = 0x97;
|
|
102
|
+
const JS_AWAIT_EXPRESSION = 0x98;
|
|
103
|
+
const JS_YIELD_EXPRESSION = 0x99;
|
|
104
|
+
const JS_CHAIN_EXPRESSION = 0x9a;
|
|
105
|
+
const JS_META_PROPERTY = 0x9b;
|
|
106
|
+
const JS_SPREAD_ELEMENT = 0x9c;
|
|
107
|
+
const JS_OBJECT_PATTERN = 0x9d;
|
|
108
|
+
const JS_ARRAY_PATTERN = 0x9e;
|
|
109
|
+
const JS_ASSIGNMENT_PATTERN = 0x9f;
|
|
110
|
+
const JS_REST_ELEMENT = 0xa0;
|
|
111
|
+
const JS_PROPERTY = 0xa1;
|
|
112
|
+
const JS_PROGRAM = 0xa2;
|
|
113
|
+
const JS_EXPRESSION_STATEMENT = 0xa3;
|
|
114
|
+
const JS_BLOCK_STATEMENT = 0xa4;
|
|
115
|
+
const JS_VARIABLE_DECLARATION = 0xa5;
|
|
116
|
+
const JS_VARIABLE_DECLARATOR = 0xa6;
|
|
117
|
+
const JS_FUNCTION_DECLARATION = 0xa7;
|
|
118
|
+
const JS_CLASS_DECLARATION = 0xa8;
|
|
119
|
+
const JS_RETURN_STATEMENT = 0xa9;
|
|
120
|
+
const JS_THROW_STATEMENT = 0xaa;
|
|
121
|
+
const JS_IF_STATEMENT = 0xab;
|
|
122
|
+
const JS_FOR_STATEMENT = 0xac;
|
|
123
|
+
const JS_FOR_OF_STATEMENT = 0xad;
|
|
124
|
+
const JS_FOR_IN_STATEMENT = 0xae;
|
|
125
|
+
const JS_WHILE_STATEMENT = 0xaf;
|
|
126
|
+
const JS_DO_WHILE_STATEMENT = 0xb0;
|
|
127
|
+
const JS_TRY_STATEMENT = 0xb1;
|
|
128
|
+
const JS_CATCH_CLAUSE = 0xb2;
|
|
129
|
+
const JS_SWITCH_STATEMENT = 0xb3;
|
|
130
|
+
const JS_SWITCH_CASE = 0xb4;
|
|
131
|
+
const JS_LABELED_STATEMENT = 0xb5;
|
|
132
|
+
const JS_BREAK_STATEMENT = 0xb6;
|
|
133
|
+
const JS_CONTINUE_STATEMENT = 0xb7;
|
|
134
|
+
const JS_EMPTY_STATEMENT = 0xb8;
|
|
135
|
+
const JS_DEBUGGER_STATEMENT = 0xb9;
|
|
136
|
+
const JS_IMPORT_DECLARATION = 0xba;
|
|
137
|
+
const JS_IMPORT_SPECIFIER = 0xbb;
|
|
138
|
+
const JS_IMPORT_DEFAULT_SPECIFIER = 0xbc;
|
|
139
|
+
const JS_IMPORT_NAMESPACE_SPECIFIER = 0xbd;
|
|
140
|
+
const JS_EXPORT_NAMED_DECLARATION = 0xbe;
|
|
141
|
+
const JS_EXPORT_DEFAULT_DECLARATION = 0xbf;
|
|
142
|
+
const JS_EXPORT_SPECIFIER = 0xc0;
|
|
143
|
+
const JS_CLASS_BODY = 0xc1;
|
|
144
|
+
const JS_METHOD_DEFINITION = 0xc2;
|
|
145
|
+
const JS_PROPERTY_DEFINITION = 0xc3;
|
|
146
|
+
const JS_STATIC_BLOCK = 0xc4;
|
|
147
|
+
const JS_DECORATOR = 0xc5;
|
|
148
|
+
const JS_TS_TYPE_ANNOTATION = 0xc6;
|
|
149
|
+
const JS_TS_ENUM_DECLARATION = 0xc7;
|
|
150
|
+
const JS_TS_MODULE_DECLARATION = 0xc8;
|
|
151
|
+
const JS_COMMENT = 0xc9;
|
|
152
|
+
const JS_NULL = 0xca;
|
|
153
|
+
const JS_RAW_JSON = 0xcb;
|
|
154
|
+
|
|
155
|
+
// LiteralValue inner tag (within a JS_LITERAL payload).
|
|
156
|
+
const LV_NULL = 0;
|
|
157
|
+
const LV_BOOL_FALSE = 1;
|
|
158
|
+
const LV_BOOL_TRUE = 2;
|
|
159
|
+
const LV_NUMBER_I64 = 3;
|
|
160
|
+
const LV_NUMBER_F64 = 4;
|
|
161
|
+
const LV_STRING = 5;
|
|
162
|
+
const LV_REGEX = 6;
|
|
163
|
+
|
|
164
|
+
// Element tag -> serialised `type` string. The decoder uses this
|
|
165
|
+
// table for the shared element shape (RegularElement, Component, …).
|
|
166
|
+
const ELEMENT_TYPE_NAMES = Object.create(null);
|
|
167
|
+
ELEMENT_TYPE_NAMES[TAG_REGULAR_ELEMENT] = 'RegularElement';
|
|
168
|
+
ELEMENT_TYPE_NAMES[TAG_COMPONENT] = 'Component';
|
|
169
|
+
ELEMENT_TYPE_NAMES[TAG_TITLE_ELEMENT] = 'TitleElement';
|
|
170
|
+
ELEMENT_TYPE_NAMES[TAG_SLOT_ELEMENT] = 'SlotElement';
|
|
171
|
+
ELEMENT_TYPE_NAMES[TAG_SVELTE_BODY] = 'SvelteBody';
|
|
172
|
+
ELEMENT_TYPE_NAMES[TAG_SVELTE_DOCUMENT] = 'SvelteDocument';
|
|
173
|
+
ELEMENT_TYPE_NAMES[TAG_SVELTE_FRAGMENT] = 'SvelteFragment';
|
|
174
|
+
ELEMENT_TYPE_NAMES[TAG_SVELTE_BOUNDARY] = 'SvelteBoundary';
|
|
175
|
+
ELEMENT_TYPE_NAMES[TAG_SVELTE_HEAD] = 'SvelteHead';
|
|
176
|
+
ELEMENT_TYPE_NAMES[TAG_SVELTE_OPTIONS_EL] = 'SvelteOptions';
|
|
177
|
+
ELEMENT_TYPE_NAMES[TAG_SVELTE_SELF] = 'SvelteSelf';
|
|
178
|
+
ELEMENT_TYPE_NAMES[TAG_SVELTE_WINDOW] = 'SvelteWindow';
|
|
179
|
+
ELEMENT_TYPE_NAMES[TAG_SVELTE_COMPONENT] = 'SvelteComponent';
|
|
180
|
+
ELEMENT_TYPE_NAMES[TAG_SVELTE_ELEMENT] = 'SvelteElement';
|
|
181
|
+
|
|
182
|
+
const textDecoder = new TextDecoder('utf-8');
|
|
183
|
+
|
|
184
|
+
// Node's `Buffer.prototype.toString('utf8', start, end)` is ~2× faster
|
|
185
|
+
// than `TextDecoder.decode(subarray)` for the short ASCII strings that
|
|
186
|
+
// dominate an AST (identifier names, operators, literal raw strings).
|
|
187
|
+
// Use it when the input is a Node Buffer; fall back to `TextDecoder`
|
|
188
|
+
// for plain `Uint8Array` (browser / non-Node callers).
|
|
189
|
+
const hasBuffer = typeof Buffer !== 'undefined';
|
|
190
|
+
|
|
191
|
+
class EnvelopeError extends Error {}
|
|
192
|
+
|
|
193
|
+
/**
|
|
194
|
+
* Decode a parse envelope produced by `napi.parseEnvelope(source)`.
|
|
195
|
+
*
|
|
196
|
+
* @param {Buffer | Uint8Array} buf
|
|
197
|
+
* @returns {object} the root AST node, shape-compatible with `JSON.parse(napi.parse(source))`.
|
|
198
|
+
*/
|
|
199
|
+
function decodeParseEnvelope(buf) {
|
|
200
|
+
if (!buf || typeof buf.byteLength !== 'number' || buf.byteLength < HEADER_LEN) {
|
|
201
|
+
throw new EnvelopeError('parse envelope: buffer too small');
|
|
202
|
+
}
|
|
203
|
+
const view = new DataView(buf.buffer, buf.byteOffset, buf.byteLength);
|
|
204
|
+
const magic = view.getUint32(0, true);
|
|
205
|
+
if (magic !== MAGIC) {
|
|
206
|
+
throw new EnvelopeError(
|
|
207
|
+
`parse envelope: bad magic 0x${magic.toString(16)} (expected 0x${MAGIC.toString(16)})`,
|
|
208
|
+
);
|
|
209
|
+
}
|
|
210
|
+
const version = view.getUint32(4, true);
|
|
211
|
+
if (version !== VERSION) {
|
|
212
|
+
throw new EnvelopeError(
|
|
213
|
+
`parse envelope: unsupported version ${version} (expected ${VERSION})`,
|
|
214
|
+
);
|
|
215
|
+
}
|
|
216
|
+
const totalLen = view.getUint32(8, true);
|
|
217
|
+
if (totalLen !== buf.byteLength) {
|
|
218
|
+
throw new EnvelopeError(
|
|
219
|
+
`parse envelope: total_len ${totalLen} != buffer.byteLength ${buf.byteLength}`,
|
|
220
|
+
);
|
|
221
|
+
}
|
|
222
|
+
const rootOffset = view.getUint32(12, true);
|
|
223
|
+
const flags = view.getUint32(20, true);
|
|
224
|
+
const skipJsNodeLoc = (flags & FLAG_JSNODE_NO_LOC) !== 0;
|
|
225
|
+
const cssStubOnly = (flags & FLAG_CSS_STUB_ONLY) !== 0;
|
|
226
|
+
const bytes =
|
|
227
|
+
buf instanceof Uint8Array
|
|
228
|
+
? buf
|
|
229
|
+
: new Uint8Array(buf.buffer, buf.byteOffset, buf.byteLength);
|
|
230
|
+
// Stash the fastest string-decode primitive directly on `ctx`. Node
|
|
231
|
+
// `Buffer.toString('utf8', start, end)` is ~2× faster than
|
|
232
|
+
// `TextDecoder.decode(typedArray.subarray(start, end))` for the
|
|
233
|
+
// short ASCII strings that dominate an AST.
|
|
234
|
+
const isBuffer = hasBuffer && Buffer.isBuffer(bytes);
|
|
235
|
+
const ctx = { view, bytes, pos: rootOffset, isBuffer, skipJsNodeLoc, cssStubOnly };
|
|
236
|
+
return readNode(ctx);
|
|
237
|
+
}
|
|
238
|
+
|
|
239
|
+
// ---------------------------------------------------------------------------
|
|
240
|
+
// Cursor helpers — every read advances `ctx.pos`.
|
|
241
|
+
// ---------------------------------------------------------------------------
|
|
242
|
+
|
|
243
|
+
function readU8(ctx) {
|
|
244
|
+
const v = ctx.view.getUint8(ctx.pos);
|
|
245
|
+
ctx.pos += 1;
|
|
246
|
+
return v;
|
|
247
|
+
}
|
|
248
|
+
function readU32(ctx) {
|
|
249
|
+
const v = ctx.view.getUint32(ctx.pos, true);
|
|
250
|
+
ctx.pos += 4;
|
|
251
|
+
return v;
|
|
252
|
+
}
|
|
253
|
+
function readBool(ctx) {
|
|
254
|
+
return readU8(ctx) !== 0;
|
|
255
|
+
}
|
|
256
|
+
function readStr(ctx) {
|
|
257
|
+
const len = readU32(ctx);
|
|
258
|
+
const start = ctx.pos;
|
|
259
|
+
const end = start + len;
|
|
260
|
+
ctx.pos = end;
|
|
261
|
+
// Branch is monomorphic per envelope: V8 specialises after warmup.
|
|
262
|
+
return ctx.isBuffer
|
|
263
|
+
? ctx.bytes.toString('utf8', start, end)
|
|
264
|
+
: textDecoder.decode(ctx.bytes.subarray(start, end));
|
|
265
|
+
}
|
|
266
|
+
function readOptStr(ctx) {
|
|
267
|
+
return readU8(ctx) === 0 ? null : readStr(ctx);
|
|
268
|
+
}
|
|
269
|
+
function readSourceLocation(ctx) {
|
|
270
|
+
return {
|
|
271
|
+
start: { line: readU32(ctx), column: readU32(ctx), character: readU32(ctx) },
|
|
272
|
+
end: { line: readU32(ctx), column: readU32(ctx), character: readU32(ctx) },
|
|
273
|
+
};
|
|
274
|
+
}
|
|
275
|
+
function readOptSourceLocation(ctx) {
|
|
276
|
+
return readU8(ctx) === 0 ? null : readSourceLocation(ctx);
|
|
277
|
+
}
|
|
278
|
+
function readModifiers(ctx) {
|
|
279
|
+
const count = readU32(ctx);
|
|
280
|
+
const out = new Array(count);
|
|
281
|
+
for (let i = 0; i < count; i++) out[i] = readStr(ctx);
|
|
282
|
+
return out;
|
|
283
|
+
}
|
|
284
|
+
|
|
285
|
+
/** Read a length-prefixed JSON payload (no preamble). */
|
|
286
|
+
function readInlineJson(ctx) {
|
|
287
|
+
const len = readU32(ctx);
|
|
288
|
+
const start = ctx.pos;
|
|
289
|
+
const end = start + len;
|
|
290
|
+
ctx.pos = end;
|
|
291
|
+
if (len === 0) return null;
|
|
292
|
+
return JSON.parse(
|
|
293
|
+
ctx.isBuffer
|
|
294
|
+
? ctx.bytes.toString('utf8', start, end)
|
|
295
|
+
: textDecoder.decode(ctx.bytes.subarray(start, end)),
|
|
296
|
+
);
|
|
297
|
+
}
|
|
298
|
+
|
|
299
|
+
/** Read the TAG_JSON variant — preamble already consumed. */
|
|
300
|
+
function readJsonNodePayload(ctx) {
|
|
301
|
+
return readInlineJson(ctx);
|
|
302
|
+
}
|
|
303
|
+
|
|
304
|
+
// ---------------------------------------------------------------------------
|
|
305
|
+
// Dispatch on tag
|
|
306
|
+
// ---------------------------------------------------------------------------
|
|
307
|
+
|
|
308
|
+
function readNode(ctx) {
|
|
309
|
+
const tag = readU8(ctx);
|
|
310
|
+
const start = readU32(ctx);
|
|
311
|
+
const end = readU32(ctx);
|
|
312
|
+
return readNodeBody(ctx, tag, start, end);
|
|
313
|
+
}
|
|
314
|
+
|
|
315
|
+
function readNodeBody(ctx, tag, start, end) {
|
|
316
|
+
switch (tag) {
|
|
317
|
+
case TAG_JSON:
|
|
318
|
+
return readJsonNodePayload(ctx);
|
|
319
|
+
|
|
320
|
+
case TAG_ROOT:
|
|
321
|
+
return readRoot(ctx, start, end);
|
|
322
|
+
case TAG_FRAGMENT:
|
|
323
|
+
return readFragment(ctx); // start/end of fragment are sentinels
|
|
324
|
+
case TAG_JS_COMMENT:
|
|
325
|
+
return readJsComment(ctx, start, end);
|
|
326
|
+
|
|
327
|
+
case TAG_TEXT:
|
|
328
|
+
return readText(ctx, start, end);
|
|
329
|
+
case TAG_COMMENT:
|
|
330
|
+
return readComment(ctx, start, end);
|
|
331
|
+
case TAG_EXPRESSION_TAG:
|
|
332
|
+
return readExpressionTag(ctx, start, end);
|
|
333
|
+
case TAG_HTML_TAG:
|
|
334
|
+
return readSingleExprTag(ctx, 'HtmlTag', start, end);
|
|
335
|
+
case TAG_CONST_TAG:
|
|
336
|
+
return readConstTag(ctx, start, end);
|
|
337
|
+
case TAG_DEBUG_TAG:
|
|
338
|
+
return readDebugTag(ctx, start, end);
|
|
339
|
+
case TAG_RENDER_TAG:
|
|
340
|
+
return readSingleExprTag(ctx, 'RenderTag', start, end);
|
|
341
|
+
case TAG_ATTACH_TAG:
|
|
342
|
+
return readSingleExprTag(ctx, 'AttachTag', start, end);
|
|
343
|
+
|
|
344
|
+
case TAG_REGULAR_ELEMENT:
|
|
345
|
+
case TAG_COMPONENT:
|
|
346
|
+
case TAG_TITLE_ELEMENT:
|
|
347
|
+
case TAG_SLOT_ELEMENT:
|
|
348
|
+
case TAG_SVELTE_BODY:
|
|
349
|
+
case TAG_SVELTE_DOCUMENT:
|
|
350
|
+
case TAG_SVELTE_FRAGMENT:
|
|
351
|
+
case TAG_SVELTE_BOUNDARY:
|
|
352
|
+
case TAG_SVELTE_HEAD:
|
|
353
|
+
case TAG_SVELTE_OPTIONS_EL:
|
|
354
|
+
case TAG_SVELTE_SELF:
|
|
355
|
+
case TAG_SVELTE_WINDOW:
|
|
356
|
+
return readElement(ctx, ELEMENT_TYPE_NAMES[tag], start, end);
|
|
357
|
+
case TAG_SVELTE_COMPONENT:
|
|
358
|
+
return readSvelteComponentElement(ctx, start, end);
|
|
359
|
+
case TAG_SVELTE_ELEMENT:
|
|
360
|
+
return readSvelteDynamicElement(ctx, start, end);
|
|
361
|
+
|
|
362
|
+
case TAG_SCRIPT:
|
|
363
|
+
return readScript(ctx, start, end);
|
|
364
|
+
|
|
365
|
+
case TAG_ATTRIBUTE:
|
|
366
|
+
return readAttributeNode(ctx, start, end);
|
|
367
|
+
case TAG_SPREAD_ATTRIBUTE:
|
|
368
|
+
return readSpreadAttribute(ctx, start, end);
|
|
369
|
+
case TAG_BIND_DIRECTIVE:
|
|
370
|
+
return readBindDirective(ctx, start, end);
|
|
371
|
+
case TAG_ON_DIRECTIVE:
|
|
372
|
+
return readOnDirective(ctx, start, end);
|
|
373
|
+
case TAG_CLASS_DIRECTIVE:
|
|
374
|
+
return readClassDirective(ctx, start, end);
|
|
375
|
+
case TAG_STYLE_DIRECTIVE:
|
|
376
|
+
return readStyleDirective(ctx, start, end);
|
|
377
|
+
case TAG_TRANSITION_DIRECTIVE:
|
|
378
|
+
return readTransitionDirective(ctx, start, end);
|
|
379
|
+
case TAG_ANIMATE_DIRECTIVE:
|
|
380
|
+
return readAnimateDirective(ctx, start, end);
|
|
381
|
+
case TAG_USE_DIRECTIVE:
|
|
382
|
+
return readUseDirective(ctx, start, end);
|
|
383
|
+
case TAG_LET_DIRECTIVE:
|
|
384
|
+
return readLetDirective(ctx, start, end);
|
|
385
|
+
|
|
386
|
+
case TAG_IF_BLOCK:
|
|
387
|
+
return readIfBlock(ctx, start, end);
|
|
388
|
+
case TAG_EACH_BLOCK:
|
|
389
|
+
return readEachBlock(ctx, start, end);
|
|
390
|
+
case TAG_AWAIT_BLOCK:
|
|
391
|
+
return readAwaitBlock(ctx, start, end);
|
|
392
|
+
case TAG_KEY_BLOCK:
|
|
393
|
+
return readKeyBlock(ctx, start, end);
|
|
394
|
+
case TAG_SNIPPET_BLOCK:
|
|
395
|
+
return readSnippetBlock(ctx, start, end);
|
|
396
|
+
|
|
397
|
+
// ----- JsNode (estree) ----------------------------------------
|
|
398
|
+
case JS_NULL:
|
|
399
|
+
return null;
|
|
400
|
+
case JS_RAW_JSON:
|
|
401
|
+
return readJsonNodePayload(ctx);
|
|
402
|
+
case JS_IDENTIFIER:
|
|
403
|
+
return readJsIdentifier(ctx, start, end);
|
|
404
|
+
case JS_PRIVATE_IDENTIFIER:
|
|
405
|
+
return readJsPrivateIdentifier(ctx, start, end);
|
|
406
|
+
case JS_LITERAL:
|
|
407
|
+
return readJsLiteral(ctx, start, end);
|
|
408
|
+
case JS_BINARY_EXPRESSION:
|
|
409
|
+
return readJsBinaryLike(ctx, 'BinaryExpression', start, end);
|
|
410
|
+
case JS_LOGICAL_EXPRESSION:
|
|
411
|
+
return readJsBinaryLike(ctx, 'LogicalExpression', start, end);
|
|
412
|
+
case JS_UNARY_EXPRESSION:
|
|
413
|
+
return readJsUnaryExpression(ctx, start, end);
|
|
414
|
+
case JS_CONDITIONAL_EXPRESSION:
|
|
415
|
+
return readJsConditionalExpression(ctx, start, end);
|
|
416
|
+
case JS_CALL_EXPRESSION:
|
|
417
|
+
return readJsCallExpression(ctx, start, end);
|
|
418
|
+
case JS_MEMBER_EXPRESSION:
|
|
419
|
+
return readJsMemberExpression(ctx, start, end);
|
|
420
|
+
case JS_NEW_EXPRESSION:
|
|
421
|
+
return readJsNewExpression(ctx, start, end);
|
|
422
|
+
case JS_FUNCTION_EXPRESSION:
|
|
423
|
+
return readJsFunctionExpression(ctx, start, end);
|
|
424
|
+
case JS_CLASS_EXPRESSION:
|
|
425
|
+
return readJsClassExpression(ctx, start, end);
|
|
426
|
+
case JS_ARROW_FUNCTION_EXPRESSION:
|
|
427
|
+
return readJsArrowFunctionExpression(ctx, start, end);
|
|
428
|
+
case JS_ASSIGNMENT_EXPRESSION:
|
|
429
|
+
return readJsAssignmentExpression(ctx, start, end);
|
|
430
|
+
case JS_UPDATE_EXPRESSION:
|
|
431
|
+
return readJsUpdateExpression(ctx, start, end);
|
|
432
|
+
case JS_SEQUENCE_EXPRESSION:
|
|
433
|
+
return readJsSequenceExpression(ctx, start, end);
|
|
434
|
+
case JS_ARRAY_EXPRESSION:
|
|
435
|
+
return readJsArrayExpression(ctx, start, end);
|
|
436
|
+
case JS_OBJECT_EXPRESSION:
|
|
437
|
+
return readJsObjectExpression(ctx, start, end);
|
|
438
|
+
case JS_TEMPLATE_LITERAL:
|
|
439
|
+
return readJsTemplateLiteral(ctx, start, end);
|
|
440
|
+
case JS_TAGGED_TEMPLATE_EXPRESSION:
|
|
441
|
+
return readJsTaggedTemplateExpression(ctx, start, end);
|
|
442
|
+
case JS_TEMPLATE_ELEMENT:
|
|
443
|
+
return readJsTemplateElement(ctx, start, end);
|
|
444
|
+
case JS_THIS_EXPRESSION:
|
|
445
|
+
return readJsBareExpr(ctx, 'ThisExpression', start, end);
|
|
446
|
+
case JS_SUPER:
|
|
447
|
+
return readJsBareExpr(ctx, 'Super', start, end);
|
|
448
|
+
case JS_IMPORT_EXPRESSION:
|
|
449
|
+
return readJsImportExpression(ctx, start, end);
|
|
450
|
+
case JS_AWAIT_EXPRESSION:
|
|
451
|
+
return readJsAwaitExpression(ctx, start, end);
|
|
452
|
+
case JS_YIELD_EXPRESSION:
|
|
453
|
+
return readJsYieldExpression(ctx, start, end);
|
|
454
|
+
case JS_CHAIN_EXPRESSION:
|
|
455
|
+
return readJsChainExpression(ctx, start, end);
|
|
456
|
+
case JS_META_PROPERTY:
|
|
457
|
+
return readJsMetaProperty(ctx, start, end);
|
|
458
|
+
case JS_SPREAD_ELEMENT:
|
|
459
|
+
return readJsSpreadElement(ctx, start, end);
|
|
460
|
+
case JS_OBJECT_PATTERN:
|
|
461
|
+
return readJsObjectPattern(ctx, start, end);
|
|
462
|
+
case JS_ARRAY_PATTERN:
|
|
463
|
+
return readJsArrayPattern(ctx, start, end);
|
|
464
|
+
case JS_ASSIGNMENT_PATTERN:
|
|
465
|
+
return readJsAssignmentPattern(ctx, start, end);
|
|
466
|
+
case JS_REST_ELEMENT:
|
|
467
|
+
return readJsRestElement(ctx, start, end);
|
|
468
|
+
case JS_PROPERTY:
|
|
469
|
+
return readJsProperty(ctx, start, end);
|
|
470
|
+
case JS_PROGRAM:
|
|
471
|
+
return readJsProgram(ctx, start, end);
|
|
472
|
+
case JS_EXPRESSION_STATEMENT:
|
|
473
|
+
return readJsExpressionStatement(ctx, start, end);
|
|
474
|
+
case JS_BLOCK_STATEMENT:
|
|
475
|
+
return readJsBlockStatement(ctx, start, end);
|
|
476
|
+
case JS_VARIABLE_DECLARATION:
|
|
477
|
+
return readJsVariableDeclaration(ctx, start, end);
|
|
478
|
+
case JS_VARIABLE_DECLARATOR:
|
|
479
|
+
return readJsVariableDeclarator(ctx, start, end);
|
|
480
|
+
case JS_FUNCTION_DECLARATION:
|
|
481
|
+
return readJsFunctionDeclaration(ctx, start, end);
|
|
482
|
+
case JS_CLASS_DECLARATION:
|
|
483
|
+
return readJsClassDeclaration(ctx, start, end);
|
|
484
|
+
case JS_RETURN_STATEMENT:
|
|
485
|
+
return readJsReturnStatement(ctx, start, end);
|
|
486
|
+
case JS_THROW_STATEMENT:
|
|
487
|
+
return readJsThrowStatement(ctx, start, end);
|
|
488
|
+
case JS_IF_STATEMENT:
|
|
489
|
+
return readJsIfStatement(ctx, start, end);
|
|
490
|
+
case JS_FOR_STATEMENT:
|
|
491
|
+
return readJsForStatement(ctx, start, end);
|
|
492
|
+
case JS_FOR_OF_STATEMENT:
|
|
493
|
+
return readJsForOfStatement(ctx, start, end);
|
|
494
|
+
case JS_FOR_IN_STATEMENT:
|
|
495
|
+
return readJsForInStatement(ctx, start, end);
|
|
496
|
+
case JS_WHILE_STATEMENT:
|
|
497
|
+
return readJsWhileStatement(ctx, start, end);
|
|
498
|
+
case JS_DO_WHILE_STATEMENT:
|
|
499
|
+
return readJsDoWhileStatement(ctx, start, end);
|
|
500
|
+
case JS_TRY_STATEMENT:
|
|
501
|
+
return readJsTryStatement(ctx, start, end);
|
|
502
|
+
case JS_CATCH_CLAUSE:
|
|
503
|
+
return readJsCatchClause(ctx, start, end);
|
|
504
|
+
case JS_SWITCH_STATEMENT:
|
|
505
|
+
return readJsSwitchStatement(ctx, start, end);
|
|
506
|
+
case JS_SWITCH_CASE:
|
|
507
|
+
return readJsSwitchCase(ctx, start, end);
|
|
508
|
+
case JS_LABELED_STATEMENT:
|
|
509
|
+
return readJsLabeledStatement(ctx, start, end);
|
|
510
|
+
case JS_BREAK_STATEMENT:
|
|
511
|
+
return readJsBreakStatement(ctx, start, end);
|
|
512
|
+
case JS_CONTINUE_STATEMENT:
|
|
513
|
+
return readJsContinueStatement(ctx, start, end);
|
|
514
|
+
case JS_EMPTY_STATEMENT:
|
|
515
|
+
return readJsBareExpr(ctx, 'EmptyStatement', start, end);
|
|
516
|
+
case JS_DEBUGGER_STATEMENT:
|
|
517
|
+
return readJsBareExpr(ctx, 'DebuggerStatement', start, end);
|
|
518
|
+
case JS_IMPORT_DECLARATION:
|
|
519
|
+
return readJsImportDeclaration(ctx, start, end);
|
|
520
|
+
case JS_IMPORT_SPECIFIER:
|
|
521
|
+
return readJsImportSpecifier(ctx, start, end);
|
|
522
|
+
case JS_IMPORT_DEFAULT_SPECIFIER:
|
|
523
|
+
return readJsImportDefaultSpecifier(ctx, start, end);
|
|
524
|
+
case JS_IMPORT_NAMESPACE_SPECIFIER:
|
|
525
|
+
return readJsImportNamespaceSpecifier(ctx, start, end);
|
|
526
|
+
case JS_EXPORT_NAMED_DECLARATION:
|
|
527
|
+
return readJsExportNamedDeclaration(ctx, start, end);
|
|
528
|
+
case JS_EXPORT_DEFAULT_DECLARATION:
|
|
529
|
+
return readJsExportDefaultDeclaration(ctx, start, end);
|
|
530
|
+
case JS_EXPORT_SPECIFIER:
|
|
531
|
+
return readJsExportSpecifier(ctx, start, end);
|
|
532
|
+
case JS_CLASS_BODY:
|
|
533
|
+
return readJsClassBody(ctx, start, end);
|
|
534
|
+
case JS_METHOD_DEFINITION:
|
|
535
|
+
return readJsMethodDefinition(ctx, start, end);
|
|
536
|
+
case JS_PROPERTY_DEFINITION:
|
|
537
|
+
return readJsPropertyDefinition(ctx, start, end);
|
|
538
|
+
case JS_STATIC_BLOCK:
|
|
539
|
+
return readJsStaticBlock(ctx, start, end);
|
|
540
|
+
case JS_DECORATOR:
|
|
541
|
+
return readJsBareExpr(ctx, 'Decorator', start, end);
|
|
542
|
+
case JS_TS_TYPE_ANNOTATION:
|
|
543
|
+
return readJsTSTypeAnnotation(ctx, start, end);
|
|
544
|
+
case JS_TS_ENUM_DECLARATION:
|
|
545
|
+
return readJsBareExpr(ctx, 'TSEnumDeclaration', start, end);
|
|
546
|
+
case JS_TS_MODULE_DECLARATION:
|
|
547
|
+
return readJsTSModuleDeclaration(ctx, start, end);
|
|
548
|
+
case JS_COMMENT:
|
|
549
|
+
return readJsComment_(ctx, start, end);
|
|
550
|
+
|
|
551
|
+
default:
|
|
552
|
+
throw new EnvelopeError(
|
|
553
|
+
`parse envelope: unknown tag 0x${tag.toString(16)} at offset ${ctx.pos - 9}`,
|
|
554
|
+
);
|
|
555
|
+
}
|
|
556
|
+
}
|
|
557
|
+
|
|
558
|
+
// ---------------------------------------------------------------------------
|
|
559
|
+
// JsNode (estree) decoders
|
|
560
|
+
// ---------------------------------------------------------------------------
|
|
561
|
+
|
|
562
|
+
function readTypedLoc(ctx) {
|
|
563
|
+
// Fast path: when the envelope advertised that no JsNode carries
|
|
564
|
+
// `loc`, the encoder skipped every loc-flag byte. Mirror that here.
|
|
565
|
+
if (ctx.skipJsNodeLoc) return null;
|
|
566
|
+
if (readU8(ctx) === 0) return null;
|
|
567
|
+
const startLine = readU32(ctx);
|
|
568
|
+
const startCol = readU32(ctx);
|
|
569
|
+
const startChar = readU8(ctx) !== 0 ? readU32(ctx) : null;
|
|
570
|
+
const endLine = readU32(ctx);
|
|
571
|
+
const endCol = readU32(ctx);
|
|
572
|
+
const endChar = readU8(ctx) !== 0 ? readU32(ctx) : null;
|
|
573
|
+
const startPos = { line: startLine, column: startCol };
|
|
574
|
+
if (startChar !== null) startPos.character = startChar;
|
|
575
|
+
const endPos = { line: endLine, column: endCol };
|
|
576
|
+
if (endChar !== null) endPos.character = endChar;
|
|
577
|
+
return { start: startPos, end: endPos };
|
|
578
|
+
}
|
|
579
|
+
|
|
580
|
+
function readLiteralValue(ctx) {
|
|
581
|
+
const tag = readU8(ctx);
|
|
582
|
+
switch (tag) {
|
|
583
|
+
case LV_NULL:
|
|
584
|
+
return null;
|
|
585
|
+
case LV_BOOL_FALSE:
|
|
586
|
+
return false;
|
|
587
|
+
case LV_BOOL_TRUE:
|
|
588
|
+
return true;
|
|
589
|
+
case LV_NUMBER_I64: {
|
|
590
|
+
// Two 32-bit reads — BigInt then to Number (preserves sign for negatives within JS-safe range).
|
|
591
|
+
const lo = readU32(ctx);
|
|
592
|
+
const hi = readU32(ctx);
|
|
593
|
+
const sign = hi >>> 31;
|
|
594
|
+
if (sign === 0) return hi * 0x100000000 + lo;
|
|
595
|
+
// Two's complement: subtract 2^64.
|
|
596
|
+
return (hi - 0x100000000) * 0x100000000 + lo;
|
|
597
|
+
}
|
|
598
|
+
case LV_NUMBER_F64: {
|
|
599
|
+
const dv = ctx.view;
|
|
600
|
+
const v = dv.getFloat64(ctx.pos, true);
|
|
601
|
+
ctx.pos += 8;
|
|
602
|
+
return v;
|
|
603
|
+
}
|
|
604
|
+
case LV_STRING:
|
|
605
|
+
return readStr(ctx);
|
|
606
|
+
case LV_REGEX:
|
|
607
|
+
return {}; // Literal.regex value is serialised as an empty object upstream.
|
|
608
|
+
default:
|
|
609
|
+
throw new EnvelopeError(`parse envelope: bad LiteralValue tag 0x${tag.toString(16)}`);
|
|
610
|
+
}
|
|
611
|
+
}
|
|
612
|
+
|
|
613
|
+
function readRegex(ctx) {
|
|
614
|
+
if (readU8(ctx) === 0) return null;
|
|
615
|
+
const pattern = readStr(ctx);
|
|
616
|
+
const flags = readStr(ctx);
|
|
617
|
+
return { pattern, flags };
|
|
618
|
+
}
|
|
619
|
+
|
|
620
|
+
function readTemplateElementValue(ctx) {
|
|
621
|
+
const raw = readStr(ctx);
|
|
622
|
+
const cooked = readU8(ctx) !== 0 ? readStr(ctx) : null;
|
|
623
|
+
return { raw, cooked };
|
|
624
|
+
}
|
|
625
|
+
|
|
626
|
+
function readChildArray(ctx) {
|
|
627
|
+
const count = readU32(ctx);
|
|
628
|
+
const out = new Array(count);
|
|
629
|
+
for (let i = 0; i < count; i++) out[i] = readNode(ctx);
|
|
630
|
+
return out;
|
|
631
|
+
}
|
|
632
|
+
|
|
633
|
+
function readNullableChildArray(ctx) {
|
|
634
|
+
// `Vec<Option<JsNode>>` — element is null when the flag byte is 0.
|
|
635
|
+
const count = readU32(ctx);
|
|
636
|
+
const out = new Array(count);
|
|
637
|
+
for (let i = 0; i < count; i++) {
|
|
638
|
+
out[i] = readU8(ctx) !== 0 ? readNode(ctx) : null;
|
|
639
|
+
}
|
|
640
|
+
return out;
|
|
641
|
+
}
|
|
642
|
+
|
|
643
|
+
function readOptNode(ctx) {
|
|
644
|
+
return readU8(ctx) !== 0 ? readNode(ctx) : null;
|
|
645
|
+
}
|
|
646
|
+
|
|
647
|
+
function readJsIdentifier(ctx, start, end) {
|
|
648
|
+
const loc = readTypedLoc(ctx);
|
|
649
|
+
const name = readStr(ctx);
|
|
650
|
+
const node = { type: 'Identifier', start, end };
|
|
651
|
+
if (loc !== null) node.loc = loc;
|
|
652
|
+
node.name = name;
|
|
653
|
+
return node;
|
|
654
|
+
}
|
|
655
|
+
|
|
656
|
+
function readJsPrivateIdentifier(ctx, start, end) {
|
|
657
|
+
const loc = readTypedLoc(ctx);
|
|
658
|
+
const name = readStr(ctx);
|
|
659
|
+
const node = { type: 'PrivateIdentifier', start, end };
|
|
660
|
+
if (loc !== null) node.loc = loc;
|
|
661
|
+
node.name = name;
|
|
662
|
+
return node;
|
|
663
|
+
}
|
|
664
|
+
|
|
665
|
+
function readJsLiteral(ctx, start, end) {
|
|
666
|
+
const loc = readTypedLoc(ctx);
|
|
667
|
+
const value = readLiteralValue(ctx);
|
|
668
|
+
const raw = readStr(ctx);
|
|
669
|
+
const regex = readRegex(ctx);
|
|
670
|
+
const node = { type: 'Literal', start, end };
|
|
671
|
+
if (loc !== null) node.loc = loc;
|
|
672
|
+
node.value = value;
|
|
673
|
+
node.raw = raw;
|
|
674
|
+
if (regex !== null) node.regex = regex;
|
|
675
|
+
return node;
|
|
676
|
+
}
|
|
677
|
+
|
|
678
|
+
function readJsBinaryLike(ctx, typeName, start, end) {
|
|
679
|
+
const loc = readTypedLoc(ctx);
|
|
680
|
+
const left = readNode(ctx);
|
|
681
|
+
const operator = readStr(ctx);
|
|
682
|
+
const right = readNode(ctx);
|
|
683
|
+
const node = { type: typeName, start, end };
|
|
684
|
+
if (loc !== null) node.loc = loc;
|
|
685
|
+
node.left = left;
|
|
686
|
+
node.operator = operator;
|
|
687
|
+
node.right = right;
|
|
688
|
+
return node;
|
|
689
|
+
}
|
|
690
|
+
|
|
691
|
+
function readJsUnaryExpression(ctx, start, end) {
|
|
692
|
+
const loc = readTypedLoc(ctx);
|
|
693
|
+
const operator = readStr(ctx);
|
|
694
|
+
const prefix = readBool(ctx);
|
|
695
|
+
const argument = readNode(ctx);
|
|
696
|
+
const node = { type: 'UnaryExpression', start, end };
|
|
697
|
+
if (loc !== null) node.loc = loc;
|
|
698
|
+
node.operator = operator;
|
|
699
|
+
node.prefix = prefix;
|
|
700
|
+
node.argument = argument;
|
|
701
|
+
return node;
|
|
702
|
+
}
|
|
703
|
+
|
|
704
|
+
function readJsConditionalExpression(ctx, start, end) {
|
|
705
|
+
const loc = readTypedLoc(ctx);
|
|
706
|
+
const test = readNode(ctx);
|
|
707
|
+
const consequent = readNode(ctx);
|
|
708
|
+
const alternate = readNode(ctx);
|
|
709
|
+
const node = { type: 'ConditionalExpression', start, end };
|
|
710
|
+
if (loc !== null) node.loc = loc;
|
|
711
|
+
node.test = test;
|
|
712
|
+
node.consequent = consequent;
|
|
713
|
+
node.alternate = alternate;
|
|
714
|
+
return node;
|
|
715
|
+
}
|
|
716
|
+
|
|
717
|
+
function readJsCallExpression(ctx, start, end) {
|
|
718
|
+
const loc = readTypedLoc(ctx);
|
|
719
|
+
const callee = readNode(ctx);
|
|
720
|
+
const args = readChildArray(ctx);
|
|
721
|
+
const optional = readBool(ctx);
|
|
722
|
+
const node = { type: 'CallExpression', start, end };
|
|
723
|
+
if (loc !== null) node.loc = loc;
|
|
724
|
+
node.callee = callee;
|
|
725
|
+
node.arguments = args;
|
|
726
|
+
node.optional = optional;
|
|
727
|
+
return node;
|
|
728
|
+
}
|
|
729
|
+
|
|
730
|
+
function readJsMemberExpression(ctx, start, end) {
|
|
731
|
+
const loc = readTypedLoc(ctx);
|
|
732
|
+
const object = readNode(ctx);
|
|
733
|
+
const property = readNode(ctx);
|
|
734
|
+
const computed = readBool(ctx);
|
|
735
|
+
const optional = readBool(ctx);
|
|
736
|
+
const node = { type: 'MemberExpression', start, end };
|
|
737
|
+
if (loc !== null) node.loc = loc;
|
|
738
|
+
node.object = object;
|
|
739
|
+
node.property = property;
|
|
740
|
+
node.computed = computed;
|
|
741
|
+
node.optional = optional;
|
|
742
|
+
return node;
|
|
743
|
+
}
|
|
744
|
+
|
|
745
|
+
function readJsNewExpression(ctx, start, end) {
|
|
746
|
+
const loc = readTypedLoc(ctx);
|
|
747
|
+
const callee = readNode(ctx);
|
|
748
|
+
const args = readChildArray(ctx);
|
|
749
|
+
const node = { type: 'NewExpression', start, end };
|
|
750
|
+
if (loc !== null) node.loc = loc;
|
|
751
|
+
node.callee = callee;
|
|
752
|
+
node.arguments = args;
|
|
753
|
+
return node;
|
|
754
|
+
}
|
|
755
|
+
|
|
756
|
+
function readJsFunctionExpression(ctx, start, end) {
|
|
757
|
+
const loc = readTypedLoc(ctx);
|
|
758
|
+
const id = readOptNode(ctx);
|
|
759
|
+
const generator = readBool(ctx);
|
|
760
|
+
const asyncFlag = readBool(ctx);
|
|
761
|
+
const expression = readBool(ctx);
|
|
762
|
+
const params = readChildArray(ctx);
|
|
763
|
+
const body = readOptNode(ctx);
|
|
764
|
+
const node = { type: 'FunctionExpression', start, end };
|
|
765
|
+
if (loc !== null) node.loc = loc;
|
|
766
|
+
node.id = id;
|
|
767
|
+
node.generator = generator;
|
|
768
|
+
node.async = asyncFlag;
|
|
769
|
+
node.expression = expression;
|
|
770
|
+
node.params = params;
|
|
771
|
+
node.body = body;
|
|
772
|
+
return node;
|
|
773
|
+
}
|
|
774
|
+
|
|
775
|
+
function readJsClassExpression(ctx, start, end) {
|
|
776
|
+
const loc = readTypedLoc(ctx);
|
|
777
|
+
const id = readOptNode(ctx);
|
|
778
|
+
const superClass = readOptNode(ctx);
|
|
779
|
+
const body = readNode(ctx);
|
|
780
|
+
const node = { type: 'ClassExpression', start, end };
|
|
781
|
+
if (loc !== null) node.loc = loc;
|
|
782
|
+
node.id = id;
|
|
783
|
+
node.superClass = superClass;
|
|
784
|
+
node.body = body;
|
|
785
|
+
return node;
|
|
786
|
+
}
|
|
787
|
+
|
|
788
|
+
function readJsArrowFunctionExpression(ctx, start, end) {
|
|
789
|
+
const loc = readTypedLoc(ctx);
|
|
790
|
+
const id = readOptNode(ctx);
|
|
791
|
+
const expression = readBool(ctx);
|
|
792
|
+
const generator = readBool(ctx);
|
|
793
|
+
const asyncFlag = readBool(ctx);
|
|
794
|
+
const params = readChildArray(ctx);
|
|
795
|
+
const body = readNode(ctx);
|
|
796
|
+
const node = { type: 'ArrowFunctionExpression', start, end };
|
|
797
|
+
if (loc !== null) node.loc = loc;
|
|
798
|
+
node.id = id;
|
|
799
|
+
node.expression = expression;
|
|
800
|
+
node.generator = generator;
|
|
801
|
+
node.async = asyncFlag;
|
|
802
|
+
node.params = params;
|
|
803
|
+
node.body = body;
|
|
804
|
+
return node;
|
|
805
|
+
}
|
|
806
|
+
|
|
807
|
+
function readJsAssignmentExpression(ctx, start, end) {
|
|
808
|
+
const loc = readTypedLoc(ctx);
|
|
809
|
+
const operator = readStr(ctx);
|
|
810
|
+
const left = readNode(ctx);
|
|
811
|
+
const right = readNode(ctx);
|
|
812
|
+
const node = { type: 'AssignmentExpression', start, end };
|
|
813
|
+
if (loc !== null) node.loc = loc;
|
|
814
|
+
node.operator = operator;
|
|
815
|
+
node.left = left;
|
|
816
|
+
node.right = right;
|
|
817
|
+
return node;
|
|
818
|
+
}
|
|
819
|
+
|
|
820
|
+
function readJsUpdateExpression(ctx, start, end) {
|
|
821
|
+
const loc = readTypedLoc(ctx);
|
|
822
|
+
const operator = readStr(ctx);
|
|
823
|
+
const prefix = readBool(ctx);
|
|
824
|
+
const argument = readNode(ctx);
|
|
825
|
+
const node = { type: 'UpdateExpression', start, end };
|
|
826
|
+
if (loc !== null) node.loc = loc;
|
|
827
|
+
node.operator = operator;
|
|
828
|
+
node.prefix = prefix;
|
|
829
|
+
node.argument = argument;
|
|
830
|
+
return node;
|
|
831
|
+
}
|
|
832
|
+
|
|
833
|
+
function readJsSequenceExpression(ctx, start, end) {
|
|
834
|
+
const loc = readTypedLoc(ctx);
|
|
835
|
+
const expressions = readChildArray(ctx);
|
|
836
|
+
const node = { type: 'SequenceExpression', start, end };
|
|
837
|
+
if (loc !== null) node.loc = loc;
|
|
838
|
+
node.expressions = expressions;
|
|
839
|
+
return node;
|
|
840
|
+
}
|
|
841
|
+
|
|
842
|
+
function readJsArrayExpression(ctx, start, end) {
|
|
843
|
+
const loc = readTypedLoc(ctx);
|
|
844
|
+
const elements = readNullableChildArray(ctx);
|
|
845
|
+
const node = { type: 'ArrayExpression', start, end };
|
|
846
|
+
if (loc !== null) node.loc = loc;
|
|
847
|
+
node.elements = elements;
|
|
848
|
+
return node;
|
|
849
|
+
}
|
|
850
|
+
|
|
851
|
+
function readJsObjectExpression(ctx, start, end) {
|
|
852
|
+
const loc = readTypedLoc(ctx);
|
|
853
|
+
const properties = readChildArray(ctx);
|
|
854
|
+
const node = { type: 'ObjectExpression', start, end };
|
|
855
|
+
if (loc !== null) node.loc = loc;
|
|
856
|
+
node.properties = properties;
|
|
857
|
+
return node;
|
|
858
|
+
}
|
|
859
|
+
|
|
860
|
+
function readJsTemplateLiteral(ctx, start, end) {
|
|
861
|
+
const loc = readTypedLoc(ctx);
|
|
862
|
+
const quasis = readChildArray(ctx);
|
|
863
|
+
const expressions = readChildArray(ctx);
|
|
864
|
+
const node = { type: 'TemplateLiteral', start, end };
|
|
865
|
+
if (loc !== null) node.loc = loc;
|
|
866
|
+
node.quasis = quasis;
|
|
867
|
+
node.expressions = expressions;
|
|
868
|
+
return node;
|
|
869
|
+
}
|
|
870
|
+
|
|
871
|
+
function readJsTaggedTemplateExpression(ctx, start, end) {
|
|
872
|
+
const loc = readTypedLoc(ctx);
|
|
873
|
+
const tag = readNode(ctx);
|
|
874
|
+
const quasi = readNode(ctx);
|
|
875
|
+
const node = { type: 'TaggedTemplateExpression', start, end };
|
|
876
|
+
if (loc !== null) node.loc = loc;
|
|
877
|
+
node.tag = tag;
|
|
878
|
+
node.quasi = quasi;
|
|
879
|
+
return node;
|
|
880
|
+
}
|
|
881
|
+
|
|
882
|
+
function readJsTemplateElement(ctx, start, end) {
|
|
883
|
+
const loc = readTypedLoc(ctx);
|
|
884
|
+
const tail = readBool(ctx);
|
|
885
|
+
const value = readTemplateElementValue(ctx);
|
|
886
|
+
const node = { type: 'TemplateElement', start, end };
|
|
887
|
+
if (loc !== null) node.loc = loc;
|
|
888
|
+
node.tail = tail;
|
|
889
|
+
node.value = value;
|
|
890
|
+
return node;
|
|
891
|
+
}
|
|
892
|
+
|
|
893
|
+
function readJsBareExpr(ctx, typeName, start, end) {
|
|
894
|
+
const loc = readTypedLoc(ctx);
|
|
895
|
+
const node = { type: typeName, start, end };
|
|
896
|
+
if (loc !== null) node.loc = loc;
|
|
897
|
+
return node;
|
|
898
|
+
}
|
|
899
|
+
|
|
900
|
+
function readJsImportExpression(ctx, start, end) {
|
|
901
|
+
const loc = readTypedLoc(ctx);
|
|
902
|
+
const source = readNode(ctx);
|
|
903
|
+
const node = { type: 'ImportExpression', start, end };
|
|
904
|
+
if (loc !== null) node.loc = loc;
|
|
905
|
+
node.source = source;
|
|
906
|
+
// The upstream serializer also adds `options: null`. Match that.
|
|
907
|
+
node.options = null;
|
|
908
|
+
return node;
|
|
909
|
+
}
|
|
910
|
+
|
|
911
|
+
function readJsAwaitExpression(ctx, start, end) {
|
|
912
|
+
const loc = readTypedLoc(ctx);
|
|
913
|
+
const argument = readNode(ctx);
|
|
914
|
+
const node = { type: 'AwaitExpression', start, end };
|
|
915
|
+
if (loc !== null) node.loc = loc;
|
|
916
|
+
node.argument = argument;
|
|
917
|
+
return node;
|
|
918
|
+
}
|
|
919
|
+
|
|
920
|
+
function readJsYieldExpression(ctx, start, end) {
|
|
921
|
+
const loc = readTypedLoc(ctx);
|
|
922
|
+
const delegate = readBool(ctx);
|
|
923
|
+
const argument = readOptNode(ctx);
|
|
924
|
+
const node = { type: 'YieldExpression', start, end };
|
|
925
|
+
if (loc !== null) node.loc = loc;
|
|
926
|
+
node.delegate = delegate;
|
|
927
|
+
node.argument = argument;
|
|
928
|
+
return node;
|
|
929
|
+
}
|
|
930
|
+
|
|
931
|
+
function readJsChainExpression(ctx, start, end) {
|
|
932
|
+
const loc = readTypedLoc(ctx);
|
|
933
|
+
const expression = readNode(ctx);
|
|
934
|
+
const node = { type: 'ChainExpression', start, end };
|
|
935
|
+
if (loc !== null) node.loc = loc;
|
|
936
|
+
node.expression = expression;
|
|
937
|
+
return node;
|
|
938
|
+
}
|
|
939
|
+
|
|
940
|
+
function readJsMetaProperty(ctx, start, end) {
|
|
941
|
+
const loc = readTypedLoc(ctx);
|
|
942
|
+
const meta = readNode(ctx);
|
|
943
|
+
const property = readNode(ctx);
|
|
944
|
+
const node = { type: 'MetaProperty', start, end };
|
|
945
|
+
if (loc !== null) node.loc = loc;
|
|
946
|
+
node.meta = meta;
|
|
947
|
+
node.property = property;
|
|
948
|
+
return node;
|
|
949
|
+
}
|
|
950
|
+
|
|
951
|
+
function readJsSpreadElement(ctx, start, end) {
|
|
952
|
+
const loc = readTypedLoc(ctx);
|
|
953
|
+
const argument = readNode(ctx);
|
|
954
|
+
const node = { type: 'SpreadElement', start, end };
|
|
955
|
+
if (loc !== null) node.loc = loc;
|
|
956
|
+
node.argument = argument;
|
|
957
|
+
return node;
|
|
958
|
+
}
|
|
959
|
+
|
|
960
|
+
function readJsObjectPattern(ctx, start, end) {
|
|
961
|
+
const loc = readTypedLoc(ctx);
|
|
962
|
+
const properties = readChildArray(ctx);
|
|
963
|
+
const node = { type: 'ObjectPattern', start, end };
|
|
964
|
+
if (loc !== null) node.loc = loc;
|
|
965
|
+
node.properties = properties;
|
|
966
|
+
return node;
|
|
967
|
+
}
|
|
968
|
+
|
|
969
|
+
function readJsArrayPattern(ctx, start, end) {
|
|
970
|
+
const loc = readTypedLoc(ctx);
|
|
971
|
+
const elements = readNullableChildArray(ctx);
|
|
972
|
+
const node = { type: 'ArrayPattern', start, end };
|
|
973
|
+
if (loc !== null) node.loc = loc;
|
|
974
|
+
node.elements = elements;
|
|
975
|
+
return node;
|
|
976
|
+
}
|
|
977
|
+
|
|
978
|
+
function readJsAssignmentPattern(ctx, start, end) {
|
|
979
|
+
const loc = readTypedLoc(ctx);
|
|
980
|
+
const left = readNode(ctx);
|
|
981
|
+
const right = readNode(ctx);
|
|
982
|
+
const node = { type: 'AssignmentPattern', start, end };
|
|
983
|
+
if (loc !== null) node.loc = loc;
|
|
984
|
+
node.left = left;
|
|
985
|
+
node.right = right;
|
|
986
|
+
return node;
|
|
987
|
+
}
|
|
988
|
+
|
|
989
|
+
function readJsRestElement(ctx, start, end) {
|
|
990
|
+
const loc = readTypedLoc(ctx);
|
|
991
|
+
const argument = readNode(ctx);
|
|
992
|
+
const node = { type: 'RestElement', start, end };
|
|
993
|
+
if (loc !== null) node.loc = loc;
|
|
994
|
+
node.argument = argument;
|
|
995
|
+
return node;
|
|
996
|
+
}
|
|
997
|
+
|
|
998
|
+
function readJsProperty(ctx, start, end) {
|
|
999
|
+
const loc = readTypedLoc(ctx);
|
|
1000
|
+
const method = readBool(ctx);
|
|
1001
|
+
const shorthand = readBool(ctx);
|
|
1002
|
+
const computed = readBool(ctx);
|
|
1003
|
+
const key = readNode(ctx);
|
|
1004
|
+
const value = readNode(ctx);
|
|
1005
|
+
const kind = readStr(ctx);
|
|
1006
|
+
const node = { type: 'Property', start, end };
|
|
1007
|
+
if (loc !== null) node.loc = loc;
|
|
1008
|
+
node.method = method;
|
|
1009
|
+
node.shorthand = shorthand;
|
|
1010
|
+
node.computed = computed;
|
|
1011
|
+
node.key = key;
|
|
1012
|
+
node.value = value;
|
|
1013
|
+
node.kind = kind;
|
|
1014
|
+
return node;
|
|
1015
|
+
}
|
|
1016
|
+
|
|
1017
|
+
function readJsProgram(ctx, start, end) {
|
|
1018
|
+
const loc = readTypedLoc(ctx);
|
|
1019
|
+
const body = readChildArray(ctx);
|
|
1020
|
+
const sourceType = readStr(ctx);
|
|
1021
|
+
const trailing = readU8(ctx) !== 0 ? readInlineJson(ctx) : undefined;
|
|
1022
|
+
const leading = readU8(ctx) !== 0 ? readInlineJson(ctx) : undefined;
|
|
1023
|
+
const node = { type: 'Program', start, end };
|
|
1024
|
+
if (loc !== null) node.loc = loc;
|
|
1025
|
+
node.body = body;
|
|
1026
|
+
node.sourceType = sourceType;
|
|
1027
|
+
if (trailing !== undefined) node.trailingComments = trailing;
|
|
1028
|
+
if (leading !== undefined) node.leadingComments = leading;
|
|
1029
|
+
return node;
|
|
1030
|
+
}
|
|
1031
|
+
|
|
1032
|
+
function readJsExpressionStatement(ctx, start, end) {
|
|
1033
|
+
const loc = readTypedLoc(ctx);
|
|
1034
|
+
const expression = readNode(ctx);
|
|
1035
|
+
const node = { type: 'ExpressionStatement', start, end };
|
|
1036
|
+
if (loc !== null) node.loc = loc;
|
|
1037
|
+
node.expression = expression;
|
|
1038
|
+
return node;
|
|
1039
|
+
}
|
|
1040
|
+
|
|
1041
|
+
function readJsBlockStatement(ctx, start, end) {
|
|
1042
|
+
const loc = readTypedLoc(ctx);
|
|
1043
|
+
const body = readChildArray(ctx);
|
|
1044
|
+
const node = { type: 'BlockStatement', start, end };
|
|
1045
|
+
if (loc !== null) node.loc = loc;
|
|
1046
|
+
node.body = body;
|
|
1047
|
+
return node;
|
|
1048
|
+
}
|
|
1049
|
+
|
|
1050
|
+
function readJsVariableDeclaration(ctx, start, end) {
|
|
1051
|
+
const loc = readTypedLoc(ctx);
|
|
1052
|
+
const declarations = readChildArray(ctx);
|
|
1053
|
+
const kind = readStr(ctx);
|
|
1054
|
+
const declare = readBool(ctx);
|
|
1055
|
+
const node = { type: 'VariableDeclaration', start, end };
|
|
1056
|
+
if (loc !== null) node.loc = loc;
|
|
1057
|
+
node.declarations = declarations;
|
|
1058
|
+
node.kind = kind;
|
|
1059
|
+
if (declare) node.declare = true;
|
|
1060
|
+
return node;
|
|
1061
|
+
}
|
|
1062
|
+
|
|
1063
|
+
function readJsVariableDeclarator(ctx, start, end) {
|
|
1064
|
+
const loc = readTypedLoc(ctx);
|
|
1065
|
+
const id = readNode(ctx);
|
|
1066
|
+
const init = readOptNode(ctx);
|
|
1067
|
+
const node = { type: 'VariableDeclarator', start, end };
|
|
1068
|
+
if (loc !== null) node.loc = loc;
|
|
1069
|
+
node.id = id;
|
|
1070
|
+
node.init = init;
|
|
1071
|
+
return node;
|
|
1072
|
+
}
|
|
1073
|
+
|
|
1074
|
+
function readJsFunctionDeclaration(ctx, start, end) {
|
|
1075
|
+
const loc = readTypedLoc(ctx);
|
|
1076
|
+
const id = readOptNode(ctx);
|
|
1077
|
+
const generator = readBool(ctx);
|
|
1078
|
+
const asyncFlag = readBool(ctx);
|
|
1079
|
+
const params = readChildArray(ctx);
|
|
1080
|
+
const body = readOptNode(ctx);
|
|
1081
|
+
const node = { type: 'FunctionDeclaration', start, end };
|
|
1082
|
+
if (loc !== null) node.loc = loc;
|
|
1083
|
+
node.id = id;
|
|
1084
|
+
node.generator = generator;
|
|
1085
|
+
node.async = asyncFlag;
|
|
1086
|
+
node.params = params;
|
|
1087
|
+
node.body = body;
|
|
1088
|
+
return node;
|
|
1089
|
+
}
|
|
1090
|
+
|
|
1091
|
+
function readJsClassDeclaration(ctx, start, end) {
|
|
1092
|
+
const loc = readTypedLoc(ctx);
|
|
1093
|
+
const id = readOptNode(ctx);
|
|
1094
|
+
const superClass = readOptNode(ctx);
|
|
1095
|
+
const body = readNode(ctx);
|
|
1096
|
+
const declare = readBool(ctx);
|
|
1097
|
+
const abstractFlag = readBool(ctx);
|
|
1098
|
+
const implementsFlag = readBool(ctx);
|
|
1099
|
+
const decorators = readChildArray(ctx);
|
|
1100
|
+
const node = { type: 'ClassDeclaration', start, end };
|
|
1101
|
+
if (loc !== null) node.loc = loc;
|
|
1102
|
+
node.id = id;
|
|
1103
|
+
node.superClass = superClass;
|
|
1104
|
+
node.body = body;
|
|
1105
|
+
if (declare) node.declare = true;
|
|
1106
|
+
if (abstractFlag) node.abstract = true;
|
|
1107
|
+
if (implementsFlag) node.implements = true;
|
|
1108
|
+
if (decorators.length > 0) node.decorators = decorators;
|
|
1109
|
+
return node;
|
|
1110
|
+
}
|
|
1111
|
+
|
|
1112
|
+
function readJsReturnStatement(ctx, start, end) {
|
|
1113
|
+
const loc = readTypedLoc(ctx);
|
|
1114
|
+
const argument = readOptNode(ctx);
|
|
1115
|
+
const node = { type: 'ReturnStatement', start, end };
|
|
1116
|
+
if (loc !== null) node.loc = loc;
|
|
1117
|
+
node.argument = argument;
|
|
1118
|
+
return node;
|
|
1119
|
+
}
|
|
1120
|
+
|
|
1121
|
+
function readJsThrowStatement(ctx, start, end) {
|
|
1122
|
+
const loc = readTypedLoc(ctx);
|
|
1123
|
+
const argument = readNode(ctx);
|
|
1124
|
+
const node = { type: 'ThrowStatement', start, end };
|
|
1125
|
+
if (loc !== null) node.loc = loc;
|
|
1126
|
+
node.argument = argument;
|
|
1127
|
+
return node;
|
|
1128
|
+
}
|
|
1129
|
+
|
|
1130
|
+
function readJsIfStatement(ctx, start, end) {
|
|
1131
|
+
const loc = readTypedLoc(ctx);
|
|
1132
|
+
const test = readNode(ctx);
|
|
1133
|
+
const consequent = readNode(ctx);
|
|
1134
|
+
const alternate = readOptNode(ctx);
|
|
1135
|
+
const node = { type: 'IfStatement', start, end };
|
|
1136
|
+
if (loc !== null) node.loc = loc;
|
|
1137
|
+
node.test = test;
|
|
1138
|
+
node.consequent = consequent;
|
|
1139
|
+
node.alternate = alternate;
|
|
1140
|
+
return node;
|
|
1141
|
+
}
|
|
1142
|
+
|
|
1143
|
+
function readJsForStatement(ctx, start, end) {
|
|
1144
|
+
const loc = readTypedLoc(ctx);
|
|
1145
|
+
const init = readOptNode(ctx);
|
|
1146
|
+
const test = readOptNode(ctx);
|
|
1147
|
+
const update = readOptNode(ctx);
|
|
1148
|
+
const body = readNode(ctx);
|
|
1149
|
+
const node = { type: 'ForStatement', start, end };
|
|
1150
|
+
if (loc !== null) node.loc = loc;
|
|
1151
|
+
node.init = init;
|
|
1152
|
+
node.test = test;
|
|
1153
|
+
node.update = update;
|
|
1154
|
+
node.body = body;
|
|
1155
|
+
return node;
|
|
1156
|
+
}
|
|
1157
|
+
|
|
1158
|
+
function readJsForOfStatement(ctx, start, end) {
|
|
1159
|
+
const loc = readTypedLoc(ctx);
|
|
1160
|
+
const awaitFlag = readBool(ctx);
|
|
1161
|
+
const left = readNode(ctx);
|
|
1162
|
+
const right = readNode(ctx);
|
|
1163
|
+
const body = readNode(ctx);
|
|
1164
|
+
const node = { type: 'ForOfStatement', start, end };
|
|
1165
|
+
if (loc !== null) node.loc = loc;
|
|
1166
|
+
node.await = awaitFlag;
|
|
1167
|
+
node.left = left;
|
|
1168
|
+
node.right = right;
|
|
1169
|
+
node.body = body;
|
|
1170
|
+
return node;
|
|
1171
|
+
}
|
|
1172
|
+
|
|
1173
|
+
function readJsForInStatement(ctx, start, end) {
|
|
1174
|
+
const loc = readTypedLoc(ctx);
|
|
1175
|
+
const left = readNode(ctx);
|
|
1176
|
+
const right = readNode(ctx);
|
|
1177
|
+
const body = readNode(ctx);
|
|
1178
|
+
const node = { type: 'ForInStatement', start, end };
|
|
1179
|
+
if (loc !== null) node.loc = loc;
|
|
1180
|
+
node.left = left;
|
|
1181
|
+
node.right = right;
|
|
1182
|
+
node.body = body;
|
|
1183
|
+
return node;
|
|
1184
|
+
}
|
|
1185
|
+
|
|
1186
|
+
function readJsWhileStatement(ctx, start, end) {
|
|
1187
|
+
const loc = readTypedLoc(ctx);
|
|
1188
|
+
const test = readNode(ctx);
|
|
1189
|
+
const body = readNode(ctx);
|
|
1190
|
+
const node = { type: 'WhileStatement', start, end };
|
|
1191
|
+
if (loc !== null) node.loc = loc;
|
|
1192
|
+
node.test = test;
|
|
1193
|
+
node.body = body;
|
|
1194
|
+
return node;
|
|
1195
|
+
}
|
|
1196
|
+
|
|
1197
|
+
function readJsDoWhileStatement(ctx, start, end) {
|
|
1198
|
+
const loc = readTypedLoc(ctx);
|
|
1199
|
+
const test = readNode(ctx);
|
|
1200
|
+
const body = readNode(ctx);
|
|
1201
|
+
const node = { type: 'DoWhileStatement', start, end };
|
|
1202
|
+
if (loc !== null) node.loc = loc;
|
|
1203
|
+
node.test = test;
|
|
1204
|
+
node.body = body;
|
|
1205
|
+
return node;
|
|
1206
|
+
}
|
|
1207
|
+
|
|
1208
|
+
function readJsTryStatement(ctx, start, end) {
|
|
1209
|
+
const loc = readTypedLoc(ctx);
|
|
1210
|
+
const block = readNode(ctx);
|
|
1211
|
+
const handler = readOptNode(ctx);
|
|
1212
|
+
const finalizer = readOptNode(ctx);
|
|
1213
|
+
const node = { type: 'TryStatement', start, end };
|
|
1214
|
+
if (loc !== null) node.loc = loc;
|
|
1215
|
+
node.block = block;
|
|
1216
|
+
node.handler = handler;
|
|
1217
|
+
node.finalizer = finalizer;
|
|
1218
|
+
return node;
|
|
1219
|
+
}
|
|
1220
|
+
|
|
1221
|
+
function readJsCatchClause(ctx, start, end) {
|
|
1222
|
+
const loc = readTypedLoc(ctx);
|
|
1223
|
+
const param = readOptNode(ctx);
|
|
1224
|
+
const body = readNode(ctx);
|
|
1225
|
+
const node = { type: 'CatchClause', start, end };
|
|
1226
|
+
if (loc !== null) node.loc = loc;
|
|
1227
|
+
node.param = param;
|
|
1228
|
+
node.body = body;
|
|
1229
|
+
return node;
|
|
1230
|
+
}
|
|
1231
|
+
|
|
1232
|
+
function readJsSwitchStatement(ctx, start, end) {
|
|
1233
|
+
const loc = readTypedLoc(ctx);
|
|
1234
|
+
const discriminant = readNode(ctx);
|
|
1235
|
+
const cases = readChildArray(ctx);
|
|
1236
|
+
const node = { type: 'SwitchStatement', start, end };
|
|
1237
|
+
if (loc !== null) node.loc = loc;
|
|
1238
|
+
node.discriminant = discriminant;
|
|
1239
|
+
node.cases = cases;
|
|
1240
|
+
return node;
|
|
1241
|
+
}
|
|
1242
|
+
|
|
1243
|
+
function readJsSwitchCase(ctx, start, end) {
|
|
1244
|
+
const loc = readTypedLoc(ctx);
|
|
1245
|
+
const test = readOptNode(ctx);
|
|
1246
|
+
const consequent = readChildArray(ctx);
|
|
1247
|
+
const node = { type: 'SwitchCase', start, end };
|
|
1248
|
+
if (loc !== null) node.loc = loc;
|
|
1249
|
+
node.test = test;
|
|
1250
|
+
node.consequent = consequent;
|
|
1251
|
+
return node;
|
|
1252
|
+
}
|
|
1253
|
+
|
|
1254
|
+
function readJsLabeledStatement(ctx, start, end) {
|
|
1255
|
+
const loc = readTypedLoc(ctx);
|
|
1256
|
+
const label = readNode(ctx);
|
|
1257
|
+
const body = readNode(ctx);
|
|
1258
|
+
const node = { type: 'LabeledStatement', start, end };
|
|
1259
|
+
if (loc !== null) node.loc = loc;
|
|
1260
|
+
node.label = label;
|
|
1261
|
+
node.body = body;
|
|
1262
|
+
return node;
|
|
1263
|
+
}
|
|
1264
|
+
|
|
1265
|
+
function readJsBreakStatement(ctx, start, end) {
|
|
1266
|
+
const loc = readTypedLoc(ctx);
|
|
1267
|
+
const label = readOptNode(ctx);
|
|
1268
|
+
const node = { type: 'BreakStatement', start, end };
|
|
1269
|
+
if (loc !== null) node.loc = loc;
|
|
1270
|
+
node.label = label;
|
|
1271
|
+
return node;
|
|
1272
|
+
}
|
|
1273
|
+
|
|
1274
|
+
function readJsContinueStatement(ctx, start, end) {
|
|
1275
|
+
const loc = readTypedLoc(ctx);
|
|
1276
|
+
const label = readOptNode(ctx);
|
|
1277
|
+
const node = { type: 'ContinueStatement', start, end };
|
|
1278
|
+
if (loc !== null) node.loc = loc;
|
|
1279
|
+
node.label = label;
|
|
1280
|
+
return node;
|
|
1281
|
+
}
|
|
1282
|
+
|
|
1283
|
+
function readJsImportDeclaration(ctx, start, end) {
|
|
1284
|
+
const loc = readTypedLoc(ctx);
|
|
1285
|
+
const specifiers = readChildArray(ctx);
|
|
1286
|
+
const source = readNode(ctx);
|
|
1287
|
+
const importKind = readOptStr(ctx);
|
|
1288
|
+
const attributes = readChildArray(ctx);
|
|
1289
|
+
const node = { type: 'ImportDeclaration', start, end };
|
|
1290
|
+
if (loc !== null) node.loc = loc;
|
|
1291
|
+
node.specifiers = specifiers;
|
|
1292
|
+
node.source = source;
|
|
1293
|
+
if (importKind !== null) node.importKind = importKind;
|
|
1294
|
+
node.attributes = attributes;
|
|
1295
|
+
return node;
|
|
1296
|
+
}
|
|
1297
|
+
|
|
1298
|
+
function readJsImportSpecifier(ctx, start, end) {
|
|
1299
|
+
const loc = readTypedLoc(ctx);
|
|
1300
|
+
const imported = readNode(ctx);
|
|
1301
|
+
const local = readNode(ctx);
|
|
1302
|
+
const importKind = readOptStr(ctx);
|
|
1303
|
+
const node = { type: 'ImportSpecifier', start, end };
|
|
1304
|
+
if (loc !== null) node.loc = loc;
|
|
1305
|
+
node.imported = imported;
|
|
1306
|
+
node.local = local;
|
|
1307
|
+
if (importKind !== null) node.importKind = importKind;
|
|
1308
|
+
return node;
|
|
1309
|
+
}
|
|
1310
|
+
|
|
1311
|
+
function readJsImportDefaultSpecifier(ctx, start, end) {
|
|
1312
|
+
const loc = readTypedLoc(ctx);
|
|
1313
|
+
const local = readNode(ctx);
|
|
1314
|
+
const node = { type: 'ImportDefaultSpecifier', start, end };
|
|
1315
|
+
if (loc !== null) node.loc = loc;
|
|
1316
|
+
node.local = local;
|
|
1317
|
+
return node;
|
|
1318
|
+
}
|
|
1319
|
+
|
|
1320
|
+
function readJsImportNamespaceSpecifier(ctx, start, end) {
|
|
1321
|
+
const loc = readTypedLoc(ctx);
|
|
1322
|
+
const local = readNode(ctx);
|
|
1323
|
+
const node = { type: 'ImportNamespaceSpecifier', start, end };
|
|
1324
|
+
if (loc !== null) node.loc = loc;
|
|
1325
|
+
node.local = local;
|
|
1326
|
+
return node;
|
|
1327
|
+
}
|
|
1328
|
+
|
|
1329
|
+
function readJsExportNamedDeclaration(ctx, start, end) {
|
|
1330
|
+
const loc = readTypedLoc(ctx);
|
|
1331
|
+
const declaration = readOptNode(ctx);
|
|
1332
|
+
const specifiers = readChildArray(ctx);
|
|
1333
|
+
const source = readOptNode(ctx);
|
|
1334
|
+
const exportKind = readOptStr(ctx);
|
|
1335
|
+
const attributes = readChildArray(ctx);
|
|
1336
|
+
const node = { type: 'ExportNamedDeclaration', start, end };
|
|
1337
|
+
if (loc !== null) node.loc = loc;
|
|
1338
|
+
node.declaration = declaration;
|
|
1339
|
+
node.specifiers = specifiers;
|
|
1340
|
+
node.source = source;
|
|
1341
|
+
if (exportKind !== null) node.exportKind = exportKind;
|
|
1342
|
+
node.attributes = attributes;
|
|
1343
|
+
return node;
|
|
1344
|
+
}
|
|
1345
|
+
|
|
1346
|
+
function readJsExportDefaultDeclaration(ctx, start, end) {
|
|
1347
|
+
const loc = readTypedLoc(ctx);
|
|
1348
|
+
const declaration = readNode(ctx);
|
|
1349
|
+
const node = { type: 'ExportDefaultDeclaration', start, end };
|
|
1350
|
+
if (loc !== null) node.loc = loc;
|
|
1351
|
+
node.declaration = declaration;
|
|
1352
|
+
return node;
|
|
1353
|
+
}
|
|
1354
|
+
|
|
1355
|
+
function readJsExportSpecifier(ctx, start, end) {
|
|
1356
|
+
const loc = readTypedLoc(ctx);
|
|
1357
|
+
const local = readNode(ctx);
|
|
1358
|
+
const exported = readNode(ctx);
|
|
1359
|
+
const exportKind = readOptStr(ctx);
|
|
1360
|
+
const node = { type: 'ExportSpecifier', start, end };
|
|
1361
|
+
if (loc !== null) node.loc = loc;
|
|
1362
|
+
node.local = local;
|
|
1363
|
+
node.exported = exported;
|
|
1364
|
+
if (exportKind !== null) node.exportKind = exportKind;
|
|
1365
|
+
return node;
|
|
1366
|
+
}
|
|
1367
|
+
|
|
1368
|
+
function readJsClassBody(ctx, start, end) {
|
|
1369
|
+
const loc = readTypedLoc(ctx);
|
|
1370
|
+
const body = readChildArray(ctx);
|
|
1371
|
+
const node = { type: 'ClassBody', start, end };
|
|
1372
|
+
if (loc !== null) node.loc = loc;
|
|
1373
|
+
node.body = body;
|
|
1374
|
+
return node;
|
|
1375
|
+
}
|
|
1376
|
+
|
|
1377
|
+
function readJsMethodDefinition(ctx, start, end) {
|
|
1378
|
+
const loc = readTypedLoc(ctx);
|
|
1379
|
+
const staticFlag = readBool(ctx);
|
|
1380
|
+
const computed = readBool(ctx);
|
|
1381
|
+
const kind = readStr(ctx);
|
|
1382
|
+
const key = readNode(ctx);
|
|
1383
|
+
const value = readNode(ctx);
|
|
1384
|
+
const node = { type: 'MethodDefinition', start, end };
|
|
1385
|
+
if (loc !== null) node.loc = loc;
|
|
1386
|
+
node.static = staticFlag;
|
|
1387
|
+
node.computed = computed;
|
|
1388
|
+
node.kind = kind;
|
|
1389
|
+
node.key = key;
|
|
1390
|
+
node.value = value;
|
|
1391
|
+
return node;
|
|
1392
|
+
}
|
|
1393
|
+
|
|
1394
|
+
function readJsPropertyDefinition(ctx, start, end) {
|
|
1395
|
+
const loc = readTypedLoc(ctx);
|
|
1396
|
+
const staticFlag = readBool(ctx);
|
|
1397
|
+
const computed = readBool(ctx);
|
|
1398
|
+
const key = readNode(ctx);
|
|
1399
|
+
const value = readOptNode(ctx);
|
|
1400
|
+
const node = { type: 'PropertyDefinition', start, end };
|
|
1401
|
+
if (loc !== null) node.loc = loc;
|
|
1402
|
+
node.static = staticFlag;
|
|
1403
|
+
node.computed = computed;
|
|
1404
|
+
node.key = key;
|
|
1405
|
+
node.value = value;
|
|
1406
|
+
return node;
|
|
1407
|
+
}
|
|
1408
|
+
|
|
1409
|
+
function readJsStaticBlock(ctx, start, end) {
|
|
1410
|
+
const loc = readTypedLoc(ctx);
|
|
1411
|
+
const body = readChildArray(ctx);
|
|
1412
|
+
const node = { type: 'StaticBlock', start, end };
|
|
1413
|
+
if (loc !== null) node.loc = loc;
|
|
1414
|
+
node.body = body;
|
|
1415
|
+
return node;
|
|
1416
|
+
}
|
|
1417
|
+
|
|
1418
|
+
function readJsTSTypeAnnotation(ctx, start, end) {
|
|
1419
|
+
const loc = readTypedLoc(ctx);
|
|
1420
|
+
const typeAnnotation = readNode(ctx);
|
|
1421
|
+
const node = { type: 'TSTypeAnnotation', start, end };
|
|
1422
|
+
if (loc !== null) node.loc = loc;
|
|
1423
|
+
node.typeAnnotation = typeAnnotation;
|
|
1424
|
+
return node;
|
|
1425
|
+
}
|
|
1426
|
+
|
|
1427
|
+
function readJsTSModuleDeclaration(ctx, start, end) {
|
|
1428
|
+
const loc = readTypedLoc(ctx);
|
|
1429
|
+
const body = readOptNode(ctx);
|
|
1430
|
+
const node = { type: 'TSModuleDeclaration', start, end };
|
|
1431
|
+
if (loc !== null) node.loc = loc;
|
|
1432
|
+
if (body !== null) node.body = body;
|
|
1433
|
+
return node;
|
|
1434
|
+
}
|
|
1435
|
+
|
|
1436
|
+
function readJsComment_(ctx, start, end) {
|
|
1437
|
+
// JsNode::Comment emits `{type: <"Line"|"Block">, start, end, value}` —
|
|
1438
|
+
// the type tag itself carries the discriminator.
|
|
1439
|
+
const commentType = readStr(ctx);
|
|
1440
|
+
const value = readStr(ctx);
|
|
1441
|
+
return { type: commentType, start, end, value };
|
|
1442
|
+
}
|
|
1443
|
+
|
|
1444
|
+
// ---------------------------------------------------------------------------
|
|
1445
|
+
// Concrete decoders
|
|
1446
|
+
// ---------------------------------------------------------------------------
|
|
1447
|
+
|
|
1448
|
+
function readRoot(ctx, start, end) {
|
|
1449
|
+
const root = { css: null, js: null, start, end, type: 'Root', fragment: null, options: null };
|
|
1450
|
+
if (readU8(ctx) === 0) {
|
|
1451
|
+
root.css = null;
|
|
1452
|
+
} else if (ctx.cssStubOnly) {
|
|
1453
|
+
// `FLAG_CSS_STUB_ONLY` — the encoder wrote a TAG_JSON preamble
|
|
1454
|
+
// with an empty payload; the only information that survives is
|
|
1455
|
+
// the outer `start` / `end`. Rebuild the minimal StyleSheet
|
|
1456
|
+
// shape consumers expect.
|
|
1457
|
+
const tag = readU8(ctx);
|
|
1458
|
+
const cssStart = readU32(ctx);
|
|
1459
|
+
const cssEnd = readU32(ctx);
|
|
1460
|
+
const len = readU32(ctx);
|
|
1461
|
+
ctx.pos += len;
|
|
1462
|
+
void tag;
|
|
1463
|
+
root.css = {
|
|
1464
|
+
type: 'StyleSheet',
|
|
1465
|
+
start: cssStart,
|
|
1466
|
+
end: cssEnd,
|
|
1467
|
+
attributes: [],
|
|
1468
|
+
children: [],
|
|
1469
|
+
content: { start: cssStart, end: cssEnd, styles: '', comment: null },
|
|
1470
|
+
};
|
|
1471
|
+
} else {
|
|
1472
|
+
root.css = readJsonNodeWithPreamble(ctx);
|
|
1473
|
+
}
|
|
1474
|
+
root.js = readJsonNodeWithPreamble(ctx);
|
|
1475
|
+
root.fragment = readFragmentDirect(ctx);
|
|
1476
|
+
root.options = readU8(ctx) === 0 ? null : readJsonNodeWithPreamble(ctx);
|
|
1477
|
+
const commentsCount = readU32(ctx);
|
|
1478
|
+
if (commentsCount > 0) {
|
|
1479
|
+
const comments = new Array(commentsCount);
|
|
1480
|
+
for (let i = 0; i < commentsCount; i++) comments[i] = readNode(ctx);
|
|
1481
|
+
root.comments = comments;
|
|
1482
|
+
}
|
|
1483
|
+
if (readU8(ctx) !== 0) root.instance = readNode(ctx);
|
|
1484
|
+
if (readU8(ctx) !== 0) root.module = readNode(ctx);
|
|
1485
|
+
return root;
|
|
1486
|
+
}
|
|
1487
|
+
|
|
1488
|
+
function readJsonNodeWithPreamble(ctx) {
|
|
1489
|
+
// Read a TAG_JSON node where the preamble lives at the current
|
|
1490
|
+
// cursor (used by Root for child slots — css, js, options).
|
|
1491
|
+
const tag = readU8(ctx);
|
|
1492
|
+
const _start = readU32(ctx);
|
|
1493
|
+
const _end = readU32(ctx);
|
|
1494
|
+
void _start;
|
|
1495
|
+
void _end;
|
|
1496
|
+
if (tag !== TAG_JSON) {
|
|
1497
|
+
throw new EnvelopeError(
|
|
1498
|
+
`parse envelope: expected TAG_JSON child but got 0x${tag.toString(16)}`,
|
|
1499
|
+
);
|
|
1500
|
+
}
|
|
1501
|
+
return readJsonNodePayload(ctx);
|
|
1502
|
+
}
|
|
1503
|
+
|
|
1504
|
+
function readFragment(ctx) {
|
|
1505
|
+
const count = readU32(ctx);
|
|
1506
|
+
const nodes = new Array(count);
|
|
1507
|
+
for (let i = 0; i < count; i++) nodes[i] = readNode(ctx);
|
|
1508
|
+
return { type: 'Fragment', nodes };
|
|
1509
|
+
}
|
|
1510
|
+
|
|
1511
|
+
function readFragmentDirect(ctx) {
|
|
1512
|
+
// Expect the TAG_FRAGMENT preamble at the cursor.
|
|
1513
|
+
const tag = readU8(ctx);
|
|
1514
|
+
const _start = readU32(ctx);
|
|
1515
|
+
const _end = readU32(ctx);
|
|
1516
|
+
void _start;
|
|
1517
|
+
void _end;
|
|
1518
|
+
if (tag !== TAG_FRAGMENT) {
|
|
1519
|
+
throw new EnvelopeError(
|
|
1520
|
+
`parse envelope: expected TAG_FRAGMENT but got 0x${tag.toString(16)}`,
|
|
1521
|
+
);
|
|
1522
|
+
}
|
|
1523
|
+
return readFragment(ctx);
|
|
1524
|
+
}
|
|
1525
|
+
|
|
1526
|
+
function readJsComment(ctx, start, end) {
|
|
1527
|
+
const kindByte = readU8(ctx);
|
|
1528
|
+
const kind = kindByte === 1 ? 'Block' : 'Line';
|
|
1529
|
+
const value = readStr(ctx);
|
|
1530
|
+
const loc = readSourceLocation(ctx);
|
|
1531
|
+
return { type: kind, start, end, value, loc };
|
|
1532
|
+
}
|
|
1533
|
+
|
|
1534
|
+
function readText(ctx, start, end) {
|
|
1535
|
+
const raw = readStr(ctx);
|
|
1536
|
+
const data = readStr(ctx);
|
|
1537
|
+
return { type: 'Text', start, end, raw, data };
|
|
1538
|
+
}
|
|
1539
|
+
|
|
1540
|
+
function readComment(ctx, start, end) {
|
|
1541
|
+
const data = readStr(ctx);
|
|
1542
|
+
return { type: 'Comment', start, end, data };
|
|
1543
|
+
}
|
|
1544
|
+
|
|
1545
|
+
function readExpressionTag(ctx, start, end) {
|
|
1546
|
+
const expression = readNode(ctx);
|
|
1547
|
+
return { type: 'ExpressionTag', start, end, expression };
|
|
1548
|
+
}
|
|
1549
|
+
|
|
1550
|
+
function readSingleExprTag(ctx, typeName, start, end) {
|
|
1551
|
+
const expression = readNode(ctx);
|
|
1552
|
+
return { type: typeName, start, end, expression };
|
|
1553
|
+
}
|
|
1554
|
+
|
|
1555
|
+
function readConstTag(ctx, start, end) {
|
|
1556
|
+
const declaration = readNode(ctx);
|
|
1557
|
+
return { type: 'ConstTag', start, end, declaration };
|
|
1558
|
+
}
|
|
1559
|
+
|
|
1560
|
+
function readDebugTag(ctx, start, end) {
|
|
1561
|
+
const count = readU32(ctx);
|
|
1562
|
+
const identifiers = new Array(count);
|
|
1563
|
+
for (let i = 0; i < count; i++) identifiers[i] = readNode(ctx);
|
|
1564
|
+
return { type: 'DebugTag', start, end, identifiers };
|
|
1565
|
+
}
|
|
1566
|
+
|
|
1567
|
+
// ---- Elements -------------------------------------------------------------
|
|
1568
|
+
|
|
1569
|
+
function readElementCommon(ctx) {
|
|
1570
|
+
const name = readStr(ctx);
|
|
1571
|
+
const name_loc = readOptSourceLocation(ctx);
|
|
1572
|
+
const attrCount = readU32(ctx);
|
|
1573
|
+
const attributes = new Array(attrCount);
|
|
1574
|
+
for (let i = 0; i < attrCount; i++) attributes[i] = readNode(ctx);
|
|
1575
|
+
const fragment = readFragmentDirect(ctx);
|
|
1576
|
+
return { name, name_loc, attributes, fragment };
|
|
1577
|
+
}
|
|
1578
|
+
|
|
1579
|
+
function readElement(ctx, typeName, start, end) {
|
|
1580
|
+
const { name, name_loc, attributes, fragment } = readElementCommon(ctx);
|
|
1581
|
+
const node = { type: typeName, start, end, name, attributes, fragment };
|
|
1582
|
+
if (name_loc !== null) node.name_loc = name_loc;
|
|
1583
|
+
return node;
|
|
1584
|
+
}
|
|
1585
|
+
|
|
1586
|
+
function readSvelteComponentElement(ctx, start, end) {
|
|
1587
|
+
const { name, name_loc, attributes, fragment } = readElementCommon(ctx);
|
|
1588
|
+
const expression = readNode(ctx);
|
|
1589
|
+
const node = {
|
|
1590
|
+
type: 'SvelteComponent',
|
|
1591
|
+
start,
|
|
1592
|
+
end,
|
|
1593
|
+
name,
|
|
1594
|
+
attributes,
|
|
1595
|
+
fragment,
|
|
1596
|
+
expression,
|
|
1597
|
+
};
|
|
1598
|
+
if (name_loc !== null) node.name_loc = name_loc;
|
|
1599
|
+
return node;
|
|
1600
|
+
}
|
|
1601
|
+
|
|
1602
|
+
function readSvelteDynamicElement(ctx, start, end) {
|
|
1603
|
+
const { name, name_loc, attributes, fragment } = readElementCommon(ctx);
|
|
1604
|
+
const tagExpr = readNode(ctx);
|
|
1605
|
+
const node = {
|
|
1606
|
+
type: 'SvelteElement',
|
|
1607
|
+
start,
|
|
1608
|
+
end,
|
|
1609
|
+
name,
|
|
1610
|
+
attributes,
|
|
1611
|
+
fragment,
|
|
1612
|
+
tag: tagExpr,
|
|
1613
|
+
};
|
|
1614
|
+
if (name_loc !== null) node.name_loc = name_loc;
|
|
1615
|
+
return node;
|
|
1616
|
+
}
|
|
1617
|
+
|
|
1618
|
+
// ---- Script ---------------------------------------------------------------
|
|
1619
|
+
|
|
1620
|
+
function readScript(ctx, start, end) {
|
|
1621
|
+
const ctxByte = readU8(ctx);
|
|
1622
|
+
const context = ctxByte === 1 ? 'module' : 'default';
|
|
1623
|
+
const content = readNode(ctx);
|
|
1624
|
+
const attrCount = readU32(ctx);
|
|
1625
|
+
const attributes = new Array(attrCount);
|
|
1626
|
+
for (let i = 0; i < attrCount; i++) attributes[i] = readNode(ctx);
|
|
1627
|
+
return { type: 'Script', start, end, context, content, attributes };
|
|
1628
|
+
}
|
|
1629
|
+
|
|
1630
|
+
// ---- Attributes -----------------------------------------------------------
|
|
1631
|
+
|
|
1632
|
+
function readAttributeValue(ctx) {
|
|
1633
|
+
const tag = readU8(ctx);
|
|
1634
|
+
if (tag === ATTRVAL_TRUE) return true;
|
|
1635
|
+
if (tag === ATTRVAL_EXPRESSION) {
|
|
1636
|
+
// The encoded form is a full ExpressionTag preamble + payload.
|
|
1637
|
+
return readNode(ctx);
|
|
1638
|
+
}
|
|
1639
|
+
if (tag === ATTRVAL_SEQUENCE) {
|
|
1640
|
+
const count = readU32(ctx);
|
|
1641
|
+
const parts = new Array(count);
|
|
1642
|
+
for (let i = 0; i < count; i++) parts[i] = readAttributeValuePart(ctx);
|
|
1643
|
+
return parts;
|
|
1644
|
+
}
|
|
1645
|
+
throw new EnvelopeError(`parse envelope: bad attribute-value tag 0x${tag.toString(16)}`);
|
|
1646
|
+
}
|
|
1647
|
+
|
|
1648
|
+
function readAttributeValuePart(ctx) {
|
|
1649
|
+
// The encoded form is the next node — either TAG_TEXT or TAG_EXPRESSION_TAG.
|
|
1650
|
+
return readNode(ctx);
|
|
1651
|
+
}
|
|
1652
|
+
|
|
1653
|
+
function readAttributeNode(ctx, start, end) {
|
|
1654
|
+
const name = readStr(ctx);
|
|
1655
|
+
const name_loc = readOptSourceLocation(ctx);
|
|
1656
|
+
const value = readAttributeValue(ctx);
|
|
1657
|
+
const node = { type: 'Attribute', start, end, name, value };
|
|
1658
|
+
if (name_loc !== null) node.name_loc = name_loc;
|
|
1659
|
+
return node;
|
|
1660
|
+
}
|
|
1661
|
+
|
|
1662
|
+
function readSpreadAttribute(ctx, start, end) {
|
|
1663
|
+
const expression = readNode(ctx);
|
|
1664
|
+
return { type: 'SpreadAttribute', start, end, expression };
|
|
1665
|
+
}
|
|
1666
|
+
|
|
1667
|
+
function readBindDirective(ctx, start, end) {
|
|
1668
|
+
const name = readStr(ctx);
|
|
1669
|
+
const name_loc = readOptSourceLocation(ctx);
|
|
1670
|
+
const expression = readNode(ctx);
|
|
1671
|
+
const modifiers = readModifiers(ctx);
|
|
1672
|
+
const node = { start, end, type: 'BindDirective', name, expression, modifiers };
|
|
1673
|
+
if (name_loc !== null) node.name_loc = name_loc;
|
|
1674
|
+
return node;
|
|
1675
|
+
}
|
|
1676
|
+
|
|
1677
|
+
function readOnDirective(ctx, start, end) {
|
|
1678
|
+
const name = readStr(ctx);
|
|
1679
|
+
const name_loc = readOptSourceLocation(ctx);
|
|
1680
|
+
const hasExpr = readU8(ctx) !== 0;
|
|
1681
|
+
const expression = hasExpr ? readNode(ctx) : undefined;
|
|
1682
|
+
const modifiers = readModifiers(ctx);
|
|
1683
|
+
const node = { type: 'OnDirective', start, end, name };
|
|
1684
|
+
if (name_loc !== null) node.name_loc = name_loc;
|
|
1685
|
+
if (expression !== undefined) node.expression = expression;
|
|
1686
|
+
node.modifiers = modifiers;
|
|
1687
|
+
return node;
|
|
1688
|
+
}
|
|
1689
|
+
|
|
1690
|
+
function readClassDirective(ctx, start, end) {
|
|
1691
|
+
const name = readStr(ctx);
|
|
1692
|
+
const name_loc = readOptSourceLocation(ctx);
|
|
1693
|
+
const expression = readNode(ctx);
|
|
1694
|
+
const node = { type: 'ClassDirective', start, end, name, expression };
|
|
1695
|
+
if (name_loc !== null) node.name_loc = name_loc;
|
|
1696
|
+
return node;
|
|
1697
|
+
}
|
|
1698
|
+
|
|
1699
|
+
function readStyleDirective(ctx, start, end) {
|
|
1700
|
+
const name = readStr(ctx);
|
|
1701
|
+
const name_loc = readOptSourceLocation(ctx);
|
|
1702
|
+
const value = readAttributeValue(ctx);
|
|
1703
|
+
const modifiers = readModifiers(ctx);
|
|
1704
|
+
const node = { type: 'StyleDirective', start, end, name, value, modifiers };
|
|
1705
|
+
if (name_loc !== null) node.name_loc = name_loc;
|
|
1706
|
+
return node;
|
|
1707
|
+
}
|
|
1708
|
+
|
|
1709
|
+
function readTransitionDirective(ctx, start, end) {
|
|
1710
|
+
const name = readStr(ctx);
|
|
1711
|
+
const name_loc = readOptSourceLocation(ctx);
|
|
1712
|
+
const hasExpr = readU8(ctx) !== 0;
|
|
1713
|
+
const expression = hasExpr ? readNode(ctx) : undefined;
|
|
1714
|
+
const modifiers = readModifiers(ctx);
|
|
1715
|
+
const intro = readBool(ctx);
|
|
1716
|
+
const outro = readBool(ctx);
|
|
1717
|
+
const hasMeta = readU8(ctx) !== 0;
|
|
1718
|
+
const metadata = hasMeta ? readInlineJson(ctx) : undefined;
|
|
1719
|
+
const node = { type: 'TransitionDirective', start, end, name };
|
|
1720
|
+
if (name_loc !== null) node.name_loc = name_loc;
|
|
1721
|
+
if (expression !== undefined) node.expression = expression;
|
|
1722
|
+
node.modifiers = modifiers;
|
|
1723
|
+
node.intro = intro;
|
|
1724
|
+
node.outro = outro;
|
|
1725
|
+
if (metadata !== undefined) node.metadata = metadata;
|
|
1726
|
+
return node;
|
|
1727
|
+
}
|
|
1728
|
+
|
|
1729
|
+
function readAnimateDirective(ctx, start, end) {
|
|
1730
|
+
const name = readStr(ctx);
|
|
1731
|
+
const name_loc = readOptSourceLocation(ctx);
|
|
1732
|
+
const hasExpr = readU8(ctx) !== 0;
|
|
1733
|
+
const expression = hasExpr ? readNode(ctx) : undefined;
|
|
1734
|
+
const hasMeta = readU8(ctx) !== 0;
|
|
1735
|
+
const metadata = hasMeta ? readInlineJson(ctx) : undefined;
|
|
1736
|
+
const node = { type: 'AnimateDirective', start, end, name };
|
|
1737
|
+
if (name_loc !== null) node.name_loc = name_loc;
|
|
1738
|
+
if (expression !== undefined) node.expression = expression;
|
|
1739
|
+
if (metadata !== undefined) node.metadata = metadata;
|
|
1740
|
+
return node;
|
|
1741
|
+
}
|
|
1742
|
+
|
|
1743
|
+
function readUseDirective(ctx, start, end) {
|
|
1744
|
+
const name = readStr(ctx);
|
|
1745
|
+
const name_loc = readOptSourceLocation(ctx);
|
|
1746
|
+
const hasExpr = readU8(ctx) !== 0;
|
|
1747
|
+
const expression = hasExpr ? readNode(ctx) : undefined;
|
|
1748
|
+
const node = { type: 'UseDirective', start, end, name };
|
|
1749
|
+
if (name_loc !== null) node.name_loc = name_loc;
|
|
1750
|
+
if (expression !== undefined) node.expression = expression;
|
|
1751
|
+
return node;
|
|
1752
|
+
}
|
|
1753
|
+
|
|
1754
|
+
function readLetDirective(ctx, start, end) {
|
|
1755
|
+
const name = readStr(ctx);
|
|
1756
|
+
const name_loc = readOptSourceLocation(ctx);
|
|
1757
|
+
const hasExpr = readU8(ctx) !== 0;
|
|
1758
|
+
const expression = hasExpr ? readNode(ctx) : undefined;
|
|
1759
|
+
const node = { type: 'LetDirective', start, end, name };
|
|
1760
|
+
if (name_loc !== null) node.name_loc = name_loc;
|
|
1761
|
+
if (expression !== undefined) node.expression = expression;
|
|
1762
|
+
return node;
|
|
1763
|
+
}
|
|
1764
|
+
|
|
1765
|
+
// ---- Blocks ---------------------------------------------------------------
|
|
1766
|
+
|
|
1767
|
+
function readIfBlock(ctx, start, end) {
|
|
1768
|
+
const elseif = readBool(ctx);
|
|
1769
|
+
const test = readNode(ctx);
|
|
1770
|
+
const consequent = readFragmentDirect(ctx);
|
|
1771
|
+
const hasAlt = readU8(ctx) !== 0;
|
|
1772
|
+
const alternate = hasAlt ? readFragmentDirect(ctx) : null;
|
|
1773
|
+
return { type: 'IfBlock', elseif, start, end, test, consequent, alternate };
|
|
1774
|
+
}
|
|
1775
|
+
|
|
1776
|
+
function readEachBlock(ctx, start, end) {
|
|
1777
|
+
const expression = readNode(ctx);
|
|
1778
|
+
const body = readFragmentDirect(ctx);
|
|
1779
|
+
const hasCtx = readU8(ctx) !== 0;
|
|
1780
|
+
const context = hasCtx ? readNode(ctx) : null;
|
|
1781
|
+
const hasFallback = readU8(ctx) !== 0;
|
|
1782
|
+
const fallback = hasFallback ? readFragmentDirect(ctx) : undefined;
|
|
1783
|
+
const index = readOptStr(ctx);
|
|
1784
|
+
const hasKey = readU8(ctx) !== 0;
|
|
1785
|
+
const key = hasKey ? readNode(ctx) : undefined;
|
|
1786
|
+
const node = { type: 'EachBlock', start, end, expression, body, context };
|
|
1787
|
+
if (fallback !== undefined) node.fallback = fallback;
|
|
1788
|
+
if (index !== null) node.index = index;
|
|
1789
|
+
if (key !== undefined) node.key = key;
|
|
1790
|
+
return node;
|
|
1791
|
+
}
|
|
1792
|
+
|
|
1793
|
+
function readAwaitBlock(ctx, start, end) {
|
|
1794
|
+
const expression = readNode(ctx);
|
|
1795
|
+
const hasValue = readU8(ctx) !== 0;
|
|
1796
|
+
const value = hasValue ? readNode(ctx) : null;
|
|
1797
|
+
const hasError = readU8(ctx) !== 0;
|
|
1798
|
+
const error = hasError ? readNode(ctx) : null;
|
|
1799
|
+
const hasPending = readU8(ctx) !== 0;
|
|
1800
|
+
const pending = hasPending ? readFragmentDirect(ctx) : null;
|
|
1801
|
+
const hasThen = readU8(ctx) !== 0;
|
|
1802
|
+
const thenFrag = hasThen ? readFragmentDirect(ctx) : null;
|
|
1803
|
+
const hasCatch = readU8(ctx) !== 0;
|
|
1804
|
+
const catchFrag = hasCatch ? readFragmentDirect(ctx) : null;
|
|
1805
|
+
return {
|
|
1806
|
+
type: 'AwaitBlock',
|
|
1807
|
+
start,
|
|
1808
|
+
end,
|
|
1809
|
+
expression,
|
|
1810
|
+
value,
|
|
1811
|
+
error,
|
|
1812
|
+
pending,
|
|
1813
|
+
then: thenFrag,
|
|
1814
|
+
catch: catchFrag,
|
|
1815
|
+
};
|
|
1816
|
+
}
|
|
1817
|
+
|
|
1818
|
+
function readKeyBlock(ctx, start, end) {
|
|
1819
|
+
const expression = readNode(ctx);
|
|
1820
|
+
const fragment = readFragmentDirect(ctx);
|
|
1821
|
+
return { type: 'KeyBlock', start, end, expression, fragment };
|
|
1822
|
+
}
|
|
1823
|
+
|
|
1824
|
+
function readSnippetBlock(ctx, start, end) {
|
|
1825
|
+
const expression = readNode(ctx);
|
|
1826
|
+
const typeParams = readOptStr(ctx);
|
|
1827
|
+
const paramCount = readU32(ctx);
|
|
1828
|
+
const parameters = new Array(paramCount);
|
|
1829
|
+
for (let i = 0; i < paramCount; i++) parameters[i] = readNode(ctx);
|
|
1830
|
+
const body = readFragmentDirect(ctx);
|
|
1831
|
+
const node = { type: 'SnippetBlock', start, end, expression, parameters, body };
|
|
1832
|
+
if (typeParams !== null) node.typeParams = typeParams;
|
|
1833
|
+
return node;
|
|
1834
|
+
}
|
|
1835
|
+
|
|
1836
|
+
module.exports = { decodeParseEnvelope, EnvelopeError };
|