Skip to content

Commit

Permalink
feat: add transformValidatedStripTypes method
Browse files Browse the repository at this point in the history
  • Loading branch information
ChALkeR committed Aug 1, 2024
1 parent e533394 commit f7a627a
Show file tree
Hide file tree
Showing 2 changed files with 48 additions and 0 deletions.
31 changes: 31 additions & 0 deletions src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,3 +14,34 @@ export function transformSync(
...options,
});
}

function doesTSStripTypesResultMatchSource(code: string, source: string) {
if (code.length !== source.length) return false;
for (let i = 0; i < code.length; i++) {
// Might return charcodes if surrogate pair started at i-1, which is fine
// All values swc ever inserts are below \u{10000} and are not UTF-16 surrogate pairs, while some are 3-byte UTF-8
// We still need to check codePointAt to ensure that already started surrogate pairs at i-1 are not broken by this insertion
const a = code.codePointAt(i);
if (a === source.codePointAt(i)) continue;
// https://github.com/nodejs/amaro/blob/e533394f576f946add41dd8816816435e8100c3b/deps/swc/crates/swc_fast_ts_strip/src/lib.rs#L400-L414
// https://github.com/nodejs/amaro/blob/e533394f576f946add41dd8816816435e8100c3b/deps/swc/crates/swc_fast_ts_strip/src/lib.rs#L200-L226
if (
a !== 0x20 && // 0020 Space [20]
a !== 0x3b && // 003b Semicolon ; [3b]
a !== 0xa0 && // 00A0 No-Break Space [c2 a0]
a !== 0x2002 && // 2002 En Space [e2 80 82]
a !== 0xfeff // FEFF ZWNBSP [ef bb bf]
) {
return false;
}
}
return true;
}

export function transformValidatedStripTypes(source: string): string {
const { code } = transformSync(source, { mode: "strip-only" });
if (!doesTSStripTypesResultMatchSource(code, source)) {
throw new Error("swc returned unexpected transform result");
}
return code;
}
17 changes: 17 additions & 0 deletions test/validatedStripTypes.test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
const { describe, test } = require("node:test");
const assert = require("node:assert");
const { transformValidatedStripTypes } = require("../dist/index.js");

describe("transformValidatedStripTypes", () => {
test("should perform type stripping", () => {
assert.strictEqual(typeof transformValidatedStripTypes, "function");
const code = transformValidatedStripTypes("const foo: string = 'bar';");
assert.strictEqual(code, "const foo = 'bar';");
});

test("should perform type stripping with multi-byte types", () => {
assert.strictEqual(typeof transformValidatedStripTypes, "function");
const code = transformValidatedStripTypes("const foo: äöü = 'bar';");
assert.strictEqual(code, "const foo     = 'bar';");
});
});

0 comments on commit f7a627a

Please sign in to comment.