Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: add transformValidatedStripTypes method #26

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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 {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I would keep just one function transformSync, add a function called validateOptions that checks that options are valid (mode needs to be strip-only when validateResut is true)

And ad the check if validateResult do the assertion
Test the flags and add documentation

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
Copy link

@bcheidemann bcheidemann Aug 7, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Imo, ideally there would be a test which asserts a throw when the new check fails. Appreciate this is not straight-forward since doesTSStripTypesResultMatchSource should never fail, but perhaps it can be exported and tested independently of transformValidatedStripTypes?

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';");
});
});