From 66eb11a63df489bcad394221d04e5416d4d7259f Mon Sep 17 00:00:00 2001 From: Nate Moore Date: Tue, 17 Nov 2026 22:49:28 -0500 Subject: [PATCH] Map a moved JSX key to its value's source position, and drop the map exports from the erase engine --- engine/jsx.zena | 41 ++++++++++++++++++++++------------------- engine/main-erase.zena | 17 ----------------- engine/strip.zena | 21 +++++++++++++++++++++ test/sourcemap.mjs | 12 +++++++----- 4 files changed, 50 insertions(+), 41 deletions(-) diff --git a/engine/jsx.zena b/engine/jsx.zena index 96e335b..1837d9e 100644 --- a/engine/jsx.zena +++ b/engine/jsx.zena @@ -6,7 +6,10 @@ // lines in the source closes before each line break and continues after it (`"a"+" b"`). import { Memory } from 'zena:memory'; import { delta } from './lex.zena'; -import { outReserve, outCommit, outInsert, mapInserted, mapCut, mapAt, outEnd, outAt, outTruncate } from './strip.zena'; +import { + outReserve, outCommit, outInsert, mapInserted, mapCut, mapAt, mapSrcAt, mapSrcLine, mapSrcCol, mapKey, outEnd, outAt, + outTruncate, +} from './strip.zena'; import { JSX_ENT, JT_JSX, JT_JSXS, JT_CREATE, JT_FRAGMENT, JT_CHILDREN, JT_EMPTY, JT_IMPORT, JT_IJSX, JT_IJSXS, JT_IFRAGMENT, JT_ICREATE, JT_FROM, JT_RUNTIME, JT_END, JT_P_JSX, JT_P_JSXFRAG, JT_P_RUNTIME, JT_P_SOURCE, JT_CLASSIC, JT_AUTOMATIC, @@ -39,14 +42,14 @@ let U_JSX: i32 = 1; let U_JSXS: i32 = 2; let U_FRAGMENT: i32 = 4; let U_CREATE: i32 = 8; -// The saved keys, innermost last: their bytes, each one's start in them, and each one's value's input offset -// (keyFrom), which keyBegin stacks (keyPend) until keyEnd saves the key. +// The saved keys, innermost last: their bytes, each one's start in them, and each one's value's source line and +// column (keyFrom, two per key), which keyBegin stacks (keyPend) until keyEnd saves the key. var keys: FixedArray = new FixedArray(64, 0); var keysLen: i32 = 0; var keyStarts: FixedArray = new FixedArray(8, 0); -var keyFrom: FixedArray = new FixedArray(8, 0); +var keyFrom: FixedArray = new FixedArray(16, 0); var keyN: i32 = 0; -var keyPend: FixedArray = new FixedArray(8, 0); +var keyPend: FixedArray = new FixedArray(16, 0); var keyPendN: i32 = 0; // Kept text segments of the JSX text being written: [start, end) pairs. @@ -440,11 +443,12 @@ function outByte(o: i32): i32 { export function keyEnd(keyAt: i32): void { if (keyN == keyStarts.length) { let b = new FixedArray(keyN * 2, 0); - let f = new FixedArray(keyN * 2, 0); + let f = new FixedArray(keyN * 4, 0); var i = 0; while (i < keyN) { b[i] = keyStarts[i]; - f[i] = keyFrom[i]; + f[i * 2] = keyFrom[i * 2]; + f[i * 2 + 1] = keyFrom[i * 2 + 1]; i = i + 1; } keyStarts = b; @@ -452,7 +456,8 @@ export function keyEnd(keyAt: i32): void { } keyStarts[keyN] = keysLen; keyPendN = keyPendN - 1; - keyFrom[keyN] = keyPend[keyPendN]; + keyFrom[keyN * 2] = keyPend[keyPendN * 2]; + keyFrom[keyN * 2 + 1] = keyPend[keyPendN * 2 + 1]; keyN = keyN + 1; mapCut(keyAt); let end = outEnd(); @@ -520,32 +525,30 @@ export function keyEnd(keyAt: i32): void { } // A `key` attribute whose value starts at input src, before its value is written; keyEnd pairs them. Keys nest -// (an element in a key value may have its own), and so do these calls. +// (an element in a key value may have its own), and so do these calls. The value's source position is saved for +// the record key() writes, since the key is written on the element's last line, not the value's own. export function keyBegin(src: i32): void { - if (keyPendN == keyPend.length) { - let b = new FixedArray(keyPendN * 2, 0); + if (keyPendN * 2 == keyPend.length) { + let b = new FixedArray(keyPendN * 4, 0); var i = 0; - while (i < keyPendN) { + while (i < keyPendN * 2) { b[i] = keyPend[i]; i = i + 1; } keyPend = b; } - keyPend[keyPendN] = src; + mapSrcAt(src); + keyPend[keyPendN * 2] = mapSrcLine(); + keyPend[keyPendN * 2 + 1] = mapSrcCol(); keyPendN = keyPendN + 1; } -// The input offset of the innermost saved key's value: where the text key() writes next comes from. For a -// source map record, since the key is written on the element's last line, not the value's own. -export function keySource(): i32 { - return keyFrom[keyN - 1]; -} - // `,` and the innermost saved key, which it forgets. export function key(): void { keyN = keyN - 1; let a = keyStarts[keyN]; put(44); + mapKey(keyFrom[keyN * 2], keyFrom[keyN * 2 + 1]); var i = a; while (i < keysLen) { put(keys[i]); diff --git a/engine/main-erase.zena b/engine/main-erase.zena index 7604326..0a86671 100644 --- a/engine/main-erase.zena +++ b/engine/main-erase.zena @@ -2,7 +2,6 @@ import { init as initImpl, inbuf as inbufImpl, push as pushImpl, outbuf as outbufImpl, errorCount as errorCountImpl, errorsPtr as errorsPtrImpl, pragma as pragmaImpl, - mapCount as mapCountImpl, mapPtr as mapPtrImpl, mapLine as mapLineImpl, mapSrcLine as mapSrcLineImpl, } from './strip.zena'; export function init(m: i32): void { @@ -32,19 +31,3 @@ export function errorsPtr(): i32 { export function pragma(a: i32, b: i32, c: i32): void { pragmaImpl(a, b, c); } -export function mapCount(): i32 { - return mapCountImpl(); -} - -export function mapPtr(): i32 { - return mapPtrImpl(); -} - -export function mapLine(): i32 { - return mapLineImpl(); -} - -export function mapSrcLine(): i32 { - return mapSrcLineImpl(); -} - diff --git a/engine/strip.zena b/engine/strip.zena index d29499c..681f73d 100644 --- a/engine/strip.zena +++ b/engine/strip.zena @@ -4821,6 +4821,27 @@ export function mapAt(p: i32): void { mRun = 0; } +// The source position at input p, at or past the input accounted so far: jsx.zena's keyBegin saves it with the +// key, since key() writes the key on the element's last line and its record needs the value's own position. +export function mapSrcAt(p: i32): void { + if (mapOn == 0) return; + mapSync(); + if (p > mEmitted) srcScan(mEmitted, p); +} + +export function mapSrcCol(): i32 { + return mCol; +} + +// A record for the text written next, which stands for the input at source line sl, column sc, already passed. +export function mapKey(sl: i32, sc: i32): void { + if (mapOn == 0) return; + mapSync(); + mapRecord(1); + mapSet(mapLen - 1, gLine, gCol, sl, sc, outLen, 1); + mRun = 1; +} + // Drops the records at output offsets past o (from == 1) or at and past it (from == 0). function mapDropFrom(o: i32, from: i32): void { while (mapLen > 0 && mapGet(mapLen - 1, 4) + from > o) mapLen = mapLen - 1; diff --git a/test/sourcemap.mjs b/test/sourcemap.mjs index 335ce16..06f339a 100644 --- a/test/sourcemap.mjs +++ b/test/sourcemap.mjs @@ -13,7 +13,7 @@ const jsonl = (f) => fs.readFileSync(path.join(root, f), "utf8").split("\n").fil // Identifiers downlevel mode writes that the source may also hold on the same line. const INSERTED = new Set(["var", "function", "call", "this", "typeof", "string", "void", "children", "null", "true", "import", "from", "module", "exports", "const", "require", "u", "Object", "defineProperty", "get", "set", "enumerable", - "jsx", "jsxs", "Fragment", "createElement", "React", "_jsx", "_jsxs", "_Fragment", "_createElement", "key"]); + "jsx", "jsxs", "Fragment", "createElement", "React", "_jsx", "_jsxs", "_Fragment", "_createElement", "key", "as"]); const ID = /[A-Za-z_$][\w$]*/g; const LT = /\r\n|[\r\n\u2028\u2029]/; @@ -80,18 +80,20 @@ function check(name, src, opts) { const count = (text, id) => { let n = 0; for (const m of text.matchAll(ID)) if (m[0] === id) n++; return n; }; outLines.forEach((text, line) => { const srcText = srcLines[line] ?? ""; - // TODO(sm-jsx-cleanup): the automatic runtime writes a key's value from a saved copy, with no source position yet. - const key = /\bkey=(\{[^}]*\}|"[^"]*")/.exec(srcText); for (const m of text.matchAll(ID)) { if (INSERTED.has(m[0]) || count(srcText, m[0]) !== 1 || count(text, m[0]) !== 1) continue; - const where = srcText.search(new RegExp(`(? key.index && where < key.index + key[0].length) continue; const pos = originalPositionFor(tracer, { line: line + 1, column: m.index }); if (pos.line === null) { bad.push(`line ${line} col ${m.index} ${m[0]}: unmapped`); continue; } const at = (srcLines[pos.line - 1] ?? "").slice(pos.column); + // A key's value is written from a saved copy, so everything in it maps to the value's start. + if (pos.line === line + 1 && at.startsWith("{") && /^\{<\w+ key=/.test(at)) { + const v = srcText.slice(pos.column); + const inner = v.indexOf(m[0]); + if (inner > 0 && inner < v.indexOf("}")) continue; + } if (!at.startsWith(m[0])) { // A segment covers the output from its column on; the identifier may sit later in a copied run. const segs = decoded[line]; -- knotmirror 0.0.1