From 55b1d09682715c965d3dabc6b395b52094c70c58 Mon Sep 17 00:00:00 2001 From: Nikita S <259122746+exo-nikita@users.noreply.github.com> Date: Wed, 18 Nov 2026 18:33:23 +0000 Subject: [PATCH 1/4] fix: accept amaro 1.1 and 1.2 stripping of non-ASCII types in --typescript The --typescript loader checked Node's stripping byte by byte: the same UTF-8 length, and only spaces or semicolons where the source differs, as swc used to write a space for each erased byte. Node's stripping, amaro 0.2.0 (Node.js 22.13.0) to 1.1.11 (26.13.1), writes a blank of as many UTF-8 bytes for each erased UTF-16 unit instead, so a type with non-ASCII in it failed with 'result mismatch'. amaro 1.2 writes a space for each unit, so with it the same types fail with 'length mismatch'. Compare in UTF-16 units, which both keep, accepting where the source differs the blanks either writes, or a semicolon as before. A stripping that writes other blanks is still rejected. Port of PreventiveMeasures/libraries#472. --- loader/typescript.loader.js | 20 +++++++++++--------- tests/typescript.test.ts | 5 +++++ 2 files changed, 16 insertions(+), 9 deletions(-) diff --git a/loader/typescript.loader.js b/loader/typescript.loader.js index fda26e7b..b7eb3845 100644 --- a/loader/typescript.loader.js +++ b/loader/typescript.loader.js @@ -13,20 +13,22 @@ function shouldProcessUrl(s) { } } -const { URL, Buffer } = globalThis +// What stripping writes for each UTF-16 unit it erases is a blank: up to amaro 1.1, one of as many UTF-8 bytes +// (a space, a no-break space, an en space; a space and a byte order mark for four), and from amaro 1.2 a space +const blanks = new Set([' ', '\u00A0', '\u2002', '\uFEFF']) + +const { URL } = globalThis export async function load(url, context, nextLoad) { if (shouldProcessUrl(url)) { - const sourceBuf = await readFile(new URL(url)) - const source = sourceBuf.toString('utf8') + const source = await readFile(new URL(url), 'utf8') const transformed = stripTypeScriptTypes(source, { mode: 'strip' }) - const transformedBuf = Buffer.from(transformed) - if (sourceBuf.length !== transformedBuf.length) throw new Error('length mismatch') + if (source.length !== transformed.length) throw new Error('length mismatch') // in units, kept either way // eslint-disable-next-line unicorn/no-for-loop - for (let i = 0; i < transformedBuf.length; i++) { - // should match either the source buffer or spaces or semicolon: https://github.com/swc-project/swc/issues/9331 - const val = transformedBuf[i] - if (val !== sourceBuf[i] && val !== 0x20 && val !== 0x3b) throw new Error('result mismatch') + for (let i = 0; i < transformed.length; i++) { + // should match either the source or a blank or semicolon: https://github.com/swc-project/swc/issues/9331 + const val = transformed[i] + if (val !== source[i] && !blanks.has(val) && val !== ';') throw new Error('result mismatch') } return { format: 'module', source: transformed, shortCircuit: true } diff --git a/tests/typescript.test.ts b/tests/typescript.test.ts index 55a97b89..7667afca 100644 --- a/tests/typescript.test.ts +++ b/tests/typescript.test.ts @@ -21,4 +21,9 @@ describe('typescript', () => { test('sum works', () => { expect(sum(20, 22)).toBe(42) }) + + test('non-ASCII in stripped types', () => { + const greet = (name: 'Grüße' | '你好' | '😀'): string => name + expect(greet('你好')).toBe('你好') + }) }) From 78c29a65cb6e9f085f8897230d54b2897225ebf5 Mon Sep 17 00:00:00 2001 From: Nikita S <259122746+exo-nikita@users.noreply.github.com> Date: Wed, 18 Nov 2026 18:38:19 +0000 Subject: [PATCH 2/4] chore: trim typescript loader comments --- loader/typescript.loader.js | 7 +++---- 1 file changed, 3 insertions(+), 4 deletions(-) diff --git a/loader/typescript.loader.js b/loader/typescript.loader.js index b7eb3845..9e6e8a6e 100644 --- a/loader/typescript.loader.js +++ b/loader/typescript.loader.js @@ -13,8 +13,7 @@ function shouldProcessUrl(s) { } } -// What stripping writes for each UTF-16 unit it erases is a blank: up to amaro 1.1, one of as many UTF-8 bytes -// (a space, a no-break space, an en space; a space and a byte order mark for four), and from amaro 1.2 a space +// amaro < 1.2 writes blanks of the same UTF-8 length, 1.2+ writes spaces const blanks = new Set([' ', '\u00A0', '\u2002', '\uFEFF']) const { URL } = globalThis @@ -23,10 +22,10 @@ export async function load(url, context, nextLoad) { if (shouldProcessUrl(url)) { const source = await readFile(new URL(url), 'utf8') const transformed = stripTypeScriptTypes(source, { mode: 'strip' }) - if (source.length !== transformed.length) throw new Error('length mismatch') // in units, kept either way + if (source.length !== transformed.length) throw new Error('length mismatch') // eslint-disable-next-line unicorn/no-for-loop for (let i = 0; i < transformed.length; i++) { - // should match either the source or a blank or semicolon: https://github.com/swc-project/swc/issues/9331 + // should match either the source or blanks or semicolon: https://github.com/swc-project/swc/issues/9331 const val = transformed[i] if (val !== source[i] && !blanks.has(val) && val !== ';') throw new Error('result mismatch') } From b56a7b5d938def1eee9181e447976b9bccb92932 Mon Sep 17 00:00:00 2001 From: Nikita S <259122746+exo-nikita@users.noreply.github.com> Date: Wed, 18 Nov 2026 18:43:09 +0000 Subject: [PATCH 3/4] fix: require valid UTF-8 before comparing in units Comparing the source to its stripping in bytes rejected invalid UTF-8 as a side effect: decoding replaces it, so the re-encoded output did not match the file. Comparing the decoded strings in units does not, and a file with invalid UTF-8 loaded with replacement characters. Check the source bytes with isUtf8 first. --- loader/typescript.loader.js | 5 ++++- 1 file changed, 4 insertions(+), 1 deletion(-) diff --git a/loader/typescript.loader.js b/loader/typescript.loader.js index 9e6e8a6e..242e73d9 100644 --- a/loader/typescript.loader.js +++ b/loader/typescript.loader.js @@ -1,3 +1,4 @@ +import { isUtf8 } from 'node:buffer' import { readFile } from 'node:fs/promises' import { stripTypeScriptTypes } from 'node:module' // 22.13.0+ import { fileURLToPath } from 'node:url' @@ -20,7 +21,9 @@ const { URL } = globalThis export async function load(url, context, nextLoad) { if (shouldProcessUrl(url)) { - const source = await readFile(new URL(url), 'utf8') + const sourceBuf = await readFile(new URL(url)) + if (!isUtf8(sourceBuf)) throw new Error('invalid utf-8') + const source = sourceBuf.toString('utf8') const transformed = stripTypeScriptTypes(source, { mode: 'strip' }) if (source.length !== transformed.length) throw new Error('length mismatch') // eslint-disable-next-line unicorn/no-for-loop From d05523abe9cc13bab93e8b20579a8fdb7fc005ac Mon Sep 17 00:00:00 2001 From: Nikita S <259122746+exo-nikita@users.noreply.github.com> Date: Wed, 18 Nov 2026 19:33:45 +0000 Subject: [PATCH 4/4] fix: drop a byte order mark before stripping, as Node does Node drops a leading byte order mark when it reads a module, and so does stripTypeScriptTypes, so a .ts file starting with one failed the length check with 'length mismatch'. Drop it before stripping and comparing. --- loader/typescript.loader.js | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/loader/typescript.loader.js b/loader/typescript.loader.js index 242e73d9..d46fce17 100644 --- a/loader/typescript.loader.js +++ b/loader/typescript.loader.js @@ -23,7 +23,9 @@ export async function load(url, context, nextLoad) { if (shouldProcessUrl(url)) { const sourceBuf = await readFile(new URL(url)) if (!isUtf8(sourceBuf)) throw new Error('invalid utf-8') - const source = sourceBuf.toString('utf8') + const decoded = sourceBuf.toString('utf8') + // Node drops a byte order mark when reading a module, and so does stripping + const source = decoded.startsWith('\uFEFF') ? decoded.slice(1) : decoded const transformed = stripTypeScriptTypes(source, { mode: 'strip' }) if (source.length !== transformed.length) throw new Error('length mismatch') // eslint-disable-next-line unicorn/no-for-loop