Repository navigation
fix: accept amaro 1.1 and 1.2 stripping of non-ASCII types in --typescript - #70
Open
exo-nikita wants to merge 4 commits into
Open
exo-nikita wants to merge 4 commits into
exo-nikita wants to merge 4 commits into
Conversation
…cript 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.
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.
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.
This branch has not been deployed
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Port of PreventiveMeasures/libraries#472.
With
--typescript(Node.js 22.13+ without native type stripping, and theelectron-as-nodeengines),.tsfiles load throughloader/typescript.loader.js, which double-checks Node'sstripTypeScriptTypes: the same UTF-8 length as the source, and only spaces or semicolons where it differs. That is what swc used to write, a space for each erased byte, but no Node.js that hasstripTypeScriptTypeswrites that:'Grüße' | '你好', failed withresult mismatch.length mismatch.Changes
loader/typescript.loader.js: compare in UTF-16 units, whose length both forms keep, and accept where the source differs the blanks either writes, or a semicolon as before. A stripping that writes other blanks, like tabs, is still rejected. The source bytes must be valid UTF-8 (isUtf8): the byte comparison rejected invalid UTF-8 as a side effect, since decoding replaces it, but a comparison of the decoded strings would not. A leading byte order mark is dropped before stripping, as Node does when it reads a module, and as stripping does: a.tsfile starting with one failed onmainwithlength mismatch. Both as in PreventiveMeasures/triage'sstrip-types-loader.js.tests/typescript.test.ts: a type with 2-, 3- and 4-byte characters. On Node.js 22.13.0, which CI'stest:typescriptruns through the loader, it fails without the change (result mismatch).Evidence
let a: "é一😀" = 1: 22.13.0 (0.2.0), 22.22.0 (1.1.4), 22.23.5 (1.1.8), 24.0.0 (0.5.2), 24.22.3 (1.1.11), 25.9.0 (1.1.8) and 26.13.1 (1.1.11) all write blanks of the bytes. amaro 1.2.0 and 1.3.0 from npm write spaces.load()with the stripping swapped for Node 24.18's, amaro 1.1.11, 1.2.0 and 1.3.0, and Node's with each unit it changed made a space (amaro 1.2's form), over the test files and non-ASCII samples (literal types, JSDoc, type-only imports, generics): before, every non-ASCII file fails (result mismatchwith 1.1,length mismatchwith 1.2 on); after, all pass. With each changed unit made a tab instead, all are still rejected.invalid utf-8with each stripping above;mainrejected it too, except with amaro 1.2 on, which let one through in a type..tstest file starting with a byte order mark, which failed onmainwithlength mismatch, passes on Node.js 22.13.0 through the loader, with the same stack positions as Node.js 24.18.0's own stripping (line 1, column 23, the mark not counted).test:typescriptpasses on Node.js 22.13.0 (through the loader), 22.22.0, 24.0.0, 25.9.0, 26.13.1 and 24.18.0.CI=true node --run test:allandnode --run lintpass on 24.18.0, andquickjs:bundlepasses the new test.The package version isn't bumped.