yuku-codegen 0.5.17 → 0.5.18
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +1 -1
- package/encode.js +35 -32
- package/index.d.ts +9 -3
- package/package.json +13 -13
package/README.md
CHANGED
|
@@ -155,7 +155,7 @@ print(program, { comments: true }).code;
|
|
|
155
155
|
// "// hello\nconst x = 1;"
|
|
156
156
|
```
|
|
157
157
|
|
|
158
|
-
Because comments are attached to nodes, they survive AST transforms: move or replace a node and its comments come with it. See the [yuku-parser comments docs](https://www.npmjs.com/package/yuku-parser#comments) for the
|
|
158
|
+
Because comments are attached to nodes, they survive AST transforms: move or replace a node and its comments come with it. See the [yuku-parser comments docs](https://www.npmjs.com/package/yuku-parser#comments) for the comment options.
|
|
159
159
|
|
|
160
160
|
## Minification
|
|
161
161
|
|
package/encode.js
CHANGED
|
@@ -4,17 +4,17 @@
|
|
|
4
4
|
const NULL = 0xFFFFFFFF;
|
|
5
5
|
const _enc = new TextEncoder();
|
|
6
6
|
const NODE_SIZE = 48;
|
|
7
|
-
const HEADER_SIZE =
|
|
7
|
+
const HEADER_SIZE = 44;
|
|
8
8
|
const NODE_FLAGS_OFFSET = 2;
|
|
9
9
|
const NODE_FIELD0_OFFSET = 4;
|
|
10
10
|
const NODE_HEADER_U32S = 2;
|
|
11
11
|
const NODE_SPAN_START_U32 = 10;
|
|
12
12
|
const NODE_SPAN_END_U32 = 11;
|
|
13
|
-
const
|
|
14
|
-
const
|
|
15
|
-
const
|
|
16
|
-
const
|
|
17
|
-
const
|
|
13
|
+
const ATTACHED_COMMENT_SIZE = 12;
|
|
14
|
+
const ATTACHED_COMMENT_FLAGS_OFFSET = 0;
|
|
15
|
+
const ATTACHED_COMMENT_VALUE_START_OFFSET = 4;
|
|
16
|
+
const ATTACHED_COMMENT_VALUE_END_OFFSET = 8;
|
|
17
|
+
const FLAG_ATTACHED_COMMENTS = 2;
|
|
18
18
|
const BINARY_OPS_INV = {"==": 0, "!=": 1, "===": 2, "!==": 3, "<": 4, "<=": 5, ">": 6, ">=": 7, "+": 8, "-": 9, "*": 10, "/": 11, "%": 12, "**": 13, "|": 14, "^": 15, "&": 16, "<<": 17, ">>": 18, ">>>": 19, "in": 20, "instanceof": 21};
|
|
19
19
|
const LOGICAL_OPS_INV = {"&&": 0, "||": 1, "??": 2};
|
|
20
20
|
const UNARY_OPS_INV = {"-": 0, "+": 1, "!": 2, "~": 3, "typeof": 4, "void": 5, "delete": 6};
|
|
@@ -591,9 +591,11 @@ function encode(program, lineStarts) {
|
|
|
591
591
|
}
|
|
592
592
|
function enc_string_literal(n) {
|
|
593
593
|
const s = encStr(typeof n.value === "string" ? n.value : "");
|
|
594
|
+
const r = encStr(typeof n.raw === "string" ? n.raw : "");
|
|
594
595
|
const idx = alloc();
|
|
595
596
|
tagAt(idx, 33);
|
|
596
597
|
slotAt(idx, 0, s.start); slotAt(idx, 1, s.end);
|
|
598
|
+
slotAt(idx, 2, r.start); slotAt(idx, 3, r.end);
|
|
597
599
|
spanAt(idx, asStart(n), asEnd(n));
|
|
598
600
|
recordComments(n, idx);
|
|
599
601
|
return idx;
|
|
@@ -2292,14 +2294,14 @@ function encode(program, lineStarts) {
|
|
|
2292
2294
|
}
|
|
2293
2295
|
const progIdx = encNode(root);
|
|
2294
2296
|
const POSITION_INV = { "before": 0, "after": 1, "inside": 2 };
|
|
2295
|
-
// counting-sort comments by host into a prefix-sum offsets
|
|
2296
|
-
// then write each
|
|
2297
|
-
const
|
|
2298
|
-
let
|
|
2297
|
+
// counting-sort attached comments by host into a prefix-sum offsets
|
|
2298
|
+
// table, then write each at its slot in `attachedBytes`.
|
|
2299
|
+
const attached = commentsByIdx.size > 0;
|
|
2300
|
+
let attachedCount = 0;
|
|
2299
2301
|
let offsetsBytes = null;
|
|
2300
|
-
let
|
|
2301
|
-
if (
|
|
2302
|
-
for (const arr of commentsByIdx.values())
|
|
2302
|
+
let attachedBytes = null;
|
|
2303
|
+
if (attached) {
|
|
2304
|
+
for (const arr of commentsByIdx.values()) attachedCount += arr.length;
|
|
2303
2305
|
const offsets = new Uint32Array(nodeCount + 1);
|
|
2304
2306
|
for (const [idx, arr] of commentsByIdx) offsets[idx] = arr.length;
|
|
2305
2307
|
let sum = 0;
|
|
@@ -2310,9 +2312,9 @@ function encode(program, lineStarts) {
|
|
|
2310
2312
|
}
|
|
2311
2313
|
offsets[nodeCount] = sum;
|
|
2312
2314
|
offsetsBytes = offsets.buffer;
|
|
2313
|
-
|
|
2314
|
-
const cU8 = new Uint8Array(
|
|
2315
|
-
const cDV = new DataView(
|
|
2315
|
+
attachedBytes = new ArrayBuffer(attachedCount * ATTACHED_COMMENT_SIZE);
|
|
2316
|
+
const cU8 = new Uint8Array(attachedBytes);
|
|
2317
|
+
const cDV = new DataView(attachedBytes);
|
|
2316
2318
|
const cursor = new Uint32Array(nodeCount);
|
|
2317
2319
|
for (const [idx, arr] of commentsByIdx) {
|
|
2318
2320
|
for (let j = 0; j < arr.length; j++) {
|
|
@@ -2323,10 +2325,10 @@ function encode(program, lineStarts) {
|
|
|
2323
2325
|
f |= ((POSITION_INV[c.position] ?? 0) & 3) << 1;
|
|
2324
2326
|
if (c.sameLine) f |= 8;
|
|
2325
2327
|
const v = encStr(typeof c.value === "string" ? c.value : "");
|
|
2326
|
-
const o = slot *
|
|
2327
|
-
cU8[o +
|
|
2328
|
-
cDV.setUint32(o +
|
|
2329
|
-
cDV.setUint32(o +
|
|
2328
|
+
const o = slot * ATTACHED_COMMENT_SIZE;
|
|
2329
|
+
cU8[o + ATTACHED_COMMENT_FLAGS_OFFSET] = f;
|
|
2330
|
+
cDV.setUint32(o + ATTACHED_COMMENT_VALUE_START_OFFSET, v.start, true);
|
|
2331
|
+
cDV.setUint32(o + ATTACHED_COMMENT_VALUE_END_OFFSET, v.end, true);
|
|
2330
2332
|
}
|
|
2331
2333
|
}
|
|
2332
2334
|
}
|
|
@@ -2341,12 +2343,12 @@ function encode(program, lineStarts) {
|
|
|
2341
2343
|
}
|
|
2342
2344
|
const totalNodeBytes = nodeCount * NODE_SIZE;
|
|
2343
2345
|
const totalExtraBytes = extraCount * 4;
|
|
2344
|
-
const totalOffsetsBytes =
|
|
2345
|
-
const
|
|
2346
|
+
const totalOffsetsBytes = attached ? (nodeCount + 1) * 4 : 0;
|
|
2347
|
+
const totalAttachedBytes = attachedCount * ATTACHED_COMMENT_SIZE;
|
|
2346
2348
|
const totalLineStartsBytes = lineStartsCount * 4;
|
|
2347
2349
|
const finalSize =
|
|
2348
2350
|
HEADER_SIZE + totalNodeBytes + totalExtraBytes + poolLen +
|
|
2349
|
-
totalOffsetsBytes +
|
|
2351
|
+
totalOffsetsBytes + totalAttachedBytes + totalLineStartsBytes;
|
|
2350
2352
|
const out = new ArrayBuffer(finalSize);
|
|
2351
2353
|
const outU8 = new Uint8Array(out);
|
|
2352
2354
|
const outU32 = new Uint32Array(out, 0, (finalSize >>> 2));
|
|
@@ -2354,23 +2356,24 @@ function encode(program, lineStarts) {
|
|
|
2354
2356
|
outU32[1] = extraCount;
|
|
2355
2357
|
outU32[2] = poolLen;
|
|
2356
2358
|
outU32[3] = 0;
|
|
2357
|
-
outU32[4] =
|
|
2358
|
-
outU32[5] =
|
|
2359
|
-
outU32[6] =
|
|
2360
|
-
outU32[7] =
|
|
2361
|
-
outU32[8] =
|
|
2362
|
-
outU32[9] = 0;
|
|
2359
|
+
outU32[4] = 0;
|
|
2360
|
+
outU32[5] = attachedCount;
|
|
2361
|
+
outU32[6] = lineStartsCount;
|
|
2362
|
+
outU32[7] = 0;
|
|
2363
|
+
outU32[8] = progIdx;
|
|
2364
|
+
outU32[9] = attached ? FLAG_ATTACHED_COMMENTS : 0;
|
|
2365
|
+
outU32[10] = 0;
|
|
2363
2366
|
const nodesOff = HEADER_SIZE;
|
|
2364
2367
|
const extrasOff = nodesOff + totalNodeBytes;
|
|
2365
2368
|
const poolOff = extrasOff + totalExtraBytes;
|
|
2366
2369
|
const offsetsOff = poolOff + poolLen;
|
|
2367
|
-
const
|
|
2368
|
-
const lineStartsOff =
|
|
2370
|
+
const attachedOff = offsetsOff + totalOffsetsBytes;
|
|
2371
|
+
const lineStartsOff = attachedOff + totalAttachedBytes;
|
|
2369
2372
|
outU8.set(new Uint8Array(nodeAB, 0, totalNodeBytes), nodesOff);
|
|
2370
2373
|
outU8.set(new Uint8Array(extras.buffer, 0, totalExtraBytes), extrasOff);
|
|
2371
2374
|
outU8.set(pool.subarray(0, poolLen), poolOff);
|
|
2372
2375
|
if (offsetsBytes !== null) outU8.set(new Uint8Array(offsetsBytes), offsetsOff);
|
|
2373
|
-
if (
|
|
2376
|
+
if (attachedBytes !== null) outU8.set(new Uint8Array(attachedBytes), attachedOff);
|
|
2374
2377
|
if (lineStartsBytes !== null) outU8.set(new Uint8Array(lineStartsBytes), lineStartsOff);
|
|
2375
2378
|
return out;
|
|
2376
2379
|
}
|
package/index.d.ts
CHANGED
|
@@ -3,8 +3,14 @@ import type { Comment, Program } from "yuku-parser";
|
|
|
3
3
|
/** Whitespace mode for the generated output. */
|
|
4
4
|
export type Format = "pretty" | "compact";
|
|
5
5
|
|
|
6
|
-
/**
|
|
7
|
-
|
|
6
|
+
/**
|
|
7
|
+
* Quote style for emitted string literals.
|
|
8
|
+
*
|
|
9
|
+
* - `"preserve"`: reuse each literal's raw source text verbatim (quotes and
|
|
10
|
+
* escapes exactly as written).
|
|
11
|
+
* - `"double"` / `"single"`: re-escape from the decoded value using that quote.
|
|
12
|
+
*/
|
|
13
|
+
export type Quotes = "preserve" | "double" | "single";
|
|
8
14
|
|
|
9
15
|
/**
|
|
10
16
|
* Comment passthrough filter.
|
|
@@ -46,7 +52,7 @@ export interface CodegenOptions {
|
|
|
46
52
|
* @default 2
|
|
47
53
|
*/
|
|
48
54
|
indent?: number;
|
|
49
|
-
/** @default "
|
|
55
|
+
/** @default "preserve" */
|
|
50
56
|
quotes?: Quotes;
|
|
51
57
|
/**
|
|
52
58
|
* Enable Source Map V3 output. Pass a {@link SourceMapOptions} object. Its
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "yuku-codegen",
|
|
3
|
-
"version": "0.5.
|
|
3
|
+
"version": "0.5.18",
|
|
4
4
|
"description": "High-performance JavaScript/TypeScript code generator written in Zig",
|
|
5
5
|
"license": "MIT",
|
|
6
6
|
"repository": {
|
|
@@ -17,17 +17,17 @@
|
|
|
17
17
|
"encode.js"
|
|
18
18
|
],
|
|
19
19
|
"optionalDependencies": {
|
|
20
|
-
"@yuku-codegen/binding-linux-x64-gnu": "0.5.
|
|
21
|
-
"@yuku-codegen/binding-linux-arm64-gnu": "0.5.
|
|
22
|
-
"@yuku-codegen/binding-linux-arm-gnu": "0.5.
|
|
23
|
-
"@yuku-codegen/binding-linux-x64-musl": "0.5.
|
|
24
|
-
"@yuku-codegen/binding-linux-arm64-musl": "0.5.
|
|
25
|
-
"@yuku-codegen/binding-linux-arm-musl": "0.5.
|
|
26
|
-
"@yuku-codegen/binding-darwin-x64": "0.5.
|
|
27
|
-
"@yuku-codegen/binding-darwin-arm64": "0.5.
|
|
28
|
-
"@yuku-codegen/binding-win32-x64": "0.5.
|
|
29
|
-
"@yuku-codegen/binding-win32-arm64": "0.5.
|
|
30
|
-
"@yuku-codegen/binding-freebsd-x64": "0.5.
|
|
20
|
+
"@yuku-codegen/binding-linux-x64-gnu": "0.5.18",
|
|
21
|
+
"@yuku-codegen/binding-linux-arm64-gnu": "0.5.18",
|
|
22
|
+
"@yuku-codegen/binding-linux-arm-gnu": "0.5.18",
|
|
23
|
+
"@yuku-codegen/binding-linux-x64-musl": "0.5.18",
|
|
24
|
+
"@yuku-codegen/binding-linux-arm64-musl": "0.5.18",
|
|
25
|
+
"@yuku-codegen/binding-linux-arm-musl": "0.5.18",
|
|
26
|
+
"@yuku-codegen/binding-darwin-x64": "0.5.18",
|
|
27
|
+
"@yuku-codegen/binding-darwin-arm64": "0.5.18",
|
|
28
|
+
"@yuku-codegen/binding-win32-x64": "0.5.18",
|
|
29
|
+
"@yuku-codegen/binding-win32-arm64": "0.5.18",
|
|
30
|
+
"@yuku-codegen/binding-freebsd-x64": "0.5.18"
|
|
31
31
|
},
|
|
32
32
|
"keywords": [
|
|
33
33
|
"ast",
|
|
@@ -40,6 +40,6 @@
|
|
|
40
40
|
"typescript"
|
|
41
41
|
],
|
|
42
42
|
"peerDependencies": {
|
|
43
|
-
"yuku-parser": "0.5.
|
|
43
|
+
"yuku-parser": "0.5.17"
|
|
44
44
|
}
|
|
45
45
|
}
|