From 73e916e044933f5a5eeebd950bc8114deade199b Mon Sep 17 00:00:00 2001 From: Nate Moore Date: Tue, 17 Nov 2026 22:49:28 -0500 Subject: [PATCH] Match the source map guide to the engine's records, and update the engine sizes --- README.md | 6 ++--- docs/architecture.md | 4 +-- docs/sourcemaps.md | 59 ++++++++++++++++++-------------------------- 3 files changed, 29 insertions(+), 40 deletions(-) diff --git a/README.md b/README.md index 53a40ab..ebbeb3d 100644 --- a/README.md +++ b/README.md @@ -2,7 +2,7 @@ A TypeScript type stripper in a small, dependency-free WebAssembly module. -typestrip erases types the way [ts-blank-space](https://github.com/bloomberg/ts-blank-space) and Node's type stripping do. Every JavaScript character stays where it was, so the output needs no source map. It streams, so memory stays flat at any input size. The engine is written in [Zena](https://github.com/elematic/zena) and compiled to WasmGC with zero imports. It is 13.6 kB gzipped. +typestrip erases types the way [ts-blank-space](https://github.com/bloomberg/ts-blank-space) and Node's type stripping do. Every JavaScript character stays where it was, so the output needs no source map. It streams, so memory stays flat at any input size. The engine is written in [Zena](https://github.com/elematic/zena) and compiled to WasmGC with zero imports. It is 13.7 kB gzipped. ```js import { transform, transformSync } from "@nibling/typestrip"; @@ -35,9 +35,9 @@ There is one engine per feature set, and a call loads only the one its options n | What you ship | Raw | gzip -9 | brotli -11 | |---|---|---|---| -| Erase mode, with or without JSX: `erase.wasm` + wrapper | 33.0 + 9.8 kB | **13.8 + 4.1 kB** | **12.5 + 3.7 kB** | +| Erase mode, with or without JSX: `erase.wasm` + wrapper | 32.9 + 9.8 kB | **13.7 + 4.1 kB** | **12.5 + 3.7 kB** | | Downlevel mode: `downlevel.wasm` + wrapper | 47.3 + 9.8 kB | **20.1 + 4.1 kB** | **18.2 + 3.7 kB** | -| Downlevel mode writing JSX: `downlevel-jsx.wasm` + wrapper | 57.6 + 9.8 kB | **24.9 + 4.1 kB** | **22.2 + 3.7 kB** | +| Downlevel mode writing JSX: `downlevel-jsx.wasm` + wrapper | 57.7 + 9.8 kB | **25.0 + 4.1 kB** | **22.3 + 3.7 kB** | The wrapper is `src/index.js` bundled with `esbuild --bundle --minify --format=esm`. The browser entry is the same size. Every engine needs WasmGC and wasm SIMD. diff --git a/docs/architecture.md b/docs/architecture.md index a15b312..10a8087 100644 --- a/docs/architecture.md +++ b/docs/architecture.md @@ -444,8 +444,8 @@ regression check; it does not assert a budget. ## Size and speed decisions -Current sizes: `erase.wasm` 32,969 B raw / 13,776 B gzip -9; `downlevel.wasm` 47,309 B / -20,136 B; `downlevel-jsx.wasm` 57,612 B / 24,880 B. JSX output (`jsx.zena`, its text, and the +Current sizes: `erase.wasm` 32,850 B raw / 13,697 B gzip -9; `downlevel.wasm` 47,309 B / +20,136 B; `downlevel-jsx.wasm` 57,715 B / 25,015 B. JSX output (`jsx.zena`, its text, and the entity table) is about 4.7 kB gzip of `downlevel-jsx.wasm`, which is why downlevel mode without it is its own engine. Source map records cost about 1.15 kB gzip in each downlevel engine and 60 B in `erase.wasm` (the exports and the region's bookkeeping; its record paths diff --git a/docs/sourcemaps.md b/docs/sourcemaps.md index 49d3f5a..c98772d 100644 --- a/docs/sourcemaps.md +++ b/docs/sourcemaps.md @@ -4,8 +4,6 @@ How typestrip keeps positions right, when it needs a source map, and how it make Most of what typestrip does needs no map at all. Erase mode keeps every offset. Downlevel mode keeps every line, and shifts columns only inside the constructs it rewrites. A map exists to repair those columns, and nothing else. The design follows from that: map every line to itself, and add a column mapping wherever a rewrite moved text. -> The `sourceMap` option and the engine's record region described here are being built. Sections marked **proposed** describe the shape they are expected to take. - ## Erasable syntax needs no map Erase mode is the common case. It replaces each erased character with a space and keeps every line terminator, so the output has the same UTF-16 length as the input and every kept character sits at its original offset. [modes.md](modes.md#erase-mode) states the guarantee in full. @@ -37,7 +35,7 @@ Three places in the automatic JSX runtime's output broke "every line maps to its - **A namespaced name split across lines.** `svg:\nrect` is written as one string, `"svg:rect"`, on the first line. The map expresses it the same way, with the record's source line. - **The runtime import after a trailing line comment.** The automatic runtime's import is written at the end of the file. When the file ends in a `//` comment, the import needed a new line. The cleanup work moves the import before the file's trailing trivia instead, so no line is ever added. -Two other mechanics in the engine write into output that was already emitted, which would shift the generated columns of records logged before them: the parameter-property field list at the class body's `{`, and the JSX callee (`_jsx(`, `_jsxs(`, or `_createElement(`), which is chosen only when the element closes. The cleanup work turns both into forward writes: fields go after the constructor's closing `}`, and the callee gets a fixed-width slot at the element's open (`_jsx (` and `_jsxs(` are both six characters) that is filled in place at the close. `_createElement(` is longer and rare, and keeps the insert-and-adjust path. +Two other mechanics in the engine write into output that was already emitted, which would shift the generated columns of records logged before them: the parameter-property field list at the class body's `{`, and the JSX callee (`_jsx(`, `_jsxs(`, or `_createElement(`), which is chosen only when the element closes. The callee now gets a fixed-width slot at the element's open (`_jsx (` and `_jsxs(` are both six characters) that is filled in place at the close. `_createElement(` is longer and rare, and keeps the insert-and-adjust path. So does the field list: moving it after the constructor would change the order of the object's own keys, which the downlevel corpus pins, so the records logged before it are adjusted instead. ### What the map leaves out @@ -68,36 +66,27 @@ enum E { A } foo(1); var E;(function(E) { A=0;var A;this.A=A;this[A]="A"; }).call(E||(E={}),E); foo(1); ``` -The generated line falls into copied spans and inserted spans: +A consumer maps every position in a segment to the segment's one source position. It does not add the distance from the segment's start. So once a line's columns have moved, each copied token needs a segment of its own to land on itself, and each inserted span needs one that names the source point it stands for. Before the first rewrite on a line, the identity segment covers everything. -| Generated | Text | Source | Kind | +| Generated column | Text | Source column | Why | |---|---|---|---| -| 0–5 | `var E` | 0–5 `enum E` | Rewritten in place, same width | -| 6–18 | `;(function(E)` | after 5 | Inserted | -| 19–22 | ` { A` | 6–9 | Copied | -| 23–52 | `=0;var A;this.A=A;this[A]="A";` | after 9 | Inserted | -| 53–54 | ` }` | 10–11 | Copied | -| 55–74 | `).call(E\|\|(E={}),E);` | after 11 | Inserted | -| 75–82 | ` foo(1);` | 12–19 | Copied | - -A segment is needed wherever the distance between generated and source columns changes. Inserted text maps to the source point it was inserted at. A copied span that follows an insert maps to that same point, so it needs no segment of its own. - -| Segment | Generated column | Source column | Fields (deltas) | VLQ | -|---|---|---|---|---| -| 1 | 0 | 0 | 0, 0, 0, 0 | `AAAA` | -| 2 | 6 | 6 | +6, 0, 0, +6 | `MAAM` | -| 3 | 23 | 10 | +17, 0, 0, +4 | `iBAAI` | -| 4 | 55 | 12 | +32, 0, 0, +2 | `gCAAE` | - -The four fields are the generated column, the source index, the source line, and the source column. Each is a delta from the previous segment, except the generated column, which restarts at each line. The source index is always 0, since there is one source. The line delta is 0 on every segment of a line, and 1 on the first segment of the next line. - -The input ends in a newline, so there is a second, empty generated line. Its identity segment is the generated column 0, source 0, line +1, and source column 0 − 12 = −12: +| 0 | `var` | 0 | Identity segment | +| 3 | ` ` | 3 | `enum` became `var` and a blank; the first copied range after a rewrite | +| 6 | `;(function(E)` | 6 | Inserted; stands for the input after `E` | +| 19 | ` ` | 6 | Copy resumes | +| 20, 21, 22 | `{`, ` `, `A` | 7, 8, 9 | One segment per copied range | +| 23 | `=0;var A;this.A=A;this[A]="A";` | 10 | Inserted | +| 53, 54 | ` `, `}` | 10, 11 | Copy resumes | +| 55 | `).call(E\|\|(E={}),E);` | 12 | Inserted | +| 75 … 82 | ` `, `foo`, `(`, `1`, `)`, `;` | 12 … 19 | Copy resumes, one segment per range | + +Each segment is four fields: the generated column, the source index, the source line, and the source column. Each is a delta from the previous segment, except the generated column, which restarts at each line. The source index is always 0, since there is one source. The line delta is 0 on every segment of a line, and 1 on the first segment of the next line. The input ends in a newline, so there is a second, empty generated line, whose identity segment is generated column 0, source 0, line +1, and source column 0 − 20 = −20: ``` -AAAA,MAAM,iBAAI,gCAAE;AACZ +AAAA,GAAG,GAAG,aAAA,CAAC,CAAC,CAAC,CAAC,8BAAA,CAAC,CAAC,oBAAA,CAAC,GAAG,CAAC,CAAC,CAAC,CAAC;AACpB ``` -That last segment shows why the identity segment is not a constant string. The source column field is relative to the previous segment's source column, so it is `A` only when that column was 0. Across a run of unchanged lines it is `AACA` every time, which gzip reduces to almost nothing. +`CAAC` is a one-column step on both sides. `aAAA` is a 13-column step in the output with no step in the source: the copy resuming after inserted text. That last segment, `AACpB`, shows why the identity segment is not a constant string. The source column field is relative to the previous segment's source column, so it is `A` only when that column was 0. Across a run of unchanged lines it is `AACA` every time, which gzip reduces to almost nothing. A thousand-line file with no rewrites produces about 5 kB of `mappings` before compression. Each rewritten line adds a few bytes per segment. @@ -113,11 +102,11 @@ A generated column is a source column plus the shift accumulated on the current ### Records -Each **edit boundary**, the start of an inserted span or of a dropped span, logs one **record**: generated line, generated column, source line, source column. Records go into a new memory region with `mapCount` and `mapPtr` exports, read incrementally by the wrapper after each `push`, the same way it reads the error region. (**Proposed** export names; the integrator reconciles them with the plumbing branch.) The last record, written at the end of input, carries the final line count so the wrapper knows how many identity segments to fill. +Each **edit boundary**, where inserted text starts or where copying resumes after inserted or dropped input, logs one **record**, and so does each copied range on a line whose columns have already moved: generated line, generated column, source line, source column, plus the output offset and a kind the engine uses to adjust records under an anchor. The three output primitives write them, so no emitter knows about the map. Records go into a MAP memory region with `mapCount` and `mapPtr` exports, read incrementally by the wrapper after each `push`, the same way it reads the error region. `mapLine` and `mapSrcLine` give the final line counts, so the wrapper knows how many identity segments to fill. Erase mode's engine has no record region and no exports for it, so `build/erase.wasm` pays nothing. -Output under an open anchor may still be rewritten, truncated, or inserted into before it is flushed. Records logged under an anchor are held with it. A rewrite in place changes no column and touches no record. A truncation drops the records past the truncation point; the automatic runtime's `key` re-logs its record at its new generated position with its original source position. An insert into earlier output adds its length to the generated column of every held record on that line after the insertion point. The cleanup work leaves only `_createElement(` on that path. +Output under an open anchor may still be rewritten, truncated, or inserted into before it is flushed. Records logged under an anchor are held with it. A rewrite in place changes no column and touches no record. A truncation drops the records past the truncation point; the automatic runtime's `key` saves its value's source position when it saves the value, and logs a record with it where the value is written again. An insert into earlier output adds its length to the generated column of every held record on that line after the insertion point. Only `_createElement(` and the parameter-property field list take that path. ### Encoding in the wrapper @@ -142,7 +131,7 @@ That function is the whole encoder. The libraries that do this are larger becaus `sources`, `sourcesContent`, and `file` are the caller's. typestrip does not know the file's name, and holding the whole input to fill `sourcesContent` would break the flat memory profile of streaming. The map typestrip returns has `version: 3`, `sources: [""]`, `names: []`, and `mappings`. A caller sets `sources[0]`, and `sourcesContent` or `file` if it wants them. -## The API (proposed) +## The API ```js const { code, map } = await transform(src, { mode: "downlevel", sourceMap: true }); @@ -159,11 +148,11 @@ while (!(next = await gen.next()).done) out += next.value; const map = next.value; ``` -The plumbing branch may adjust names and shapes. [api.md](api.md) is authoritative once it lands. +[api.md](api.md) has the option's full shape, including `source`, `file`, and `includeContent`. -## Testing (planned) +## Testing -- **Round trip.** `@jridgewell/trace-mapping`, as a devDependency, decodes each map in `test/sourcemap.mjs`. For every rewritten construct in the corpus, the generated position of each copied token must resolve to that token's source position, and every line must resolve to the line with the same number, except the automatic runtime's moved `key`. -- **Format.** The decoding suite of [`@tc39/source-map-tests`](https://github.com/tc39/source-map-tests) checks the VLQ encoder and the JSON shape against ECMA-426. -- **Line count.** The existing downlevel judge already requires the output to have the input's line count and every un-rewritten line to equal erase mode's output. It gains a check that the map's `;` count matches. +- **Round trip.** `test/sourcemap.mjs` decodes each map with `@jridgewell/trace-mapping`, a devDependency, for every case in `corpus/downlevel.jsonl` and `corpus/jsx.jsonl`. Every line must have a segment at column 0, segments must be in order and within their lines, every segment must map to the line with the same number except where JSX moves a `key`, and every identifier that appears once in a source line and once in its output line must map back onto itself. A key's value is written from a saved copy, so everything inside it maps to the value's start. +- **Streaming.** The same file, chunked at random and at every five bytes, must produce a byte-identical map. +- **Line count.** The downlevel and JSX judges already require the output to have the input's line count. - **By eye.** Drop an output and its map into [source-map-visualization](https://evanw.github.io/source-map-visualization/) to inspect a case. -- knotmirror 0.0.1