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

test: check syntax is not polyfilled #114

Merged
merged 1 commit into from
Nov 14, 2024
Merged
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
20 changes: 20 additions & 0 deletions test/index.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -126,3 +126,23 @@ test("should handle empty source code", (t) => {
assert.strictEqual(transformSync(null).code, "");
assert.strictEqual(transformSync("").code, "");
});

test("should not polyfill using Symbol.Dispose", (t) => {
const inputCode = `
class Resource {
[Symbol.dispose]() { console.log("Disposed"); }
}
using r = new Resource();`;
marco-ippolito marked this conversation as resolved.
Show resolved Hide resolved
const { code } = transformSync(inputCode);
assert.strictEqual(code, inputCode);
});

test("should not polyfill using Symbol.asyncDispose", (t) => {
const inputCode = `
class AsyncResource {
async [Symbol.asyncDispose]() { console.log("Async disposed"); }
}
await using r = new AsyncResource();`;
const { code } = transformSync(inputCode);
assert.strictEqual(code, inputCode);
});
16 changes: 12 additions & 4 deletions test/snapshots/transform.test.js.snapshot
Original file line number Diff line number Diff line change
@@ -1,3 +1,15 @@
exports[`should not polyfill using Symbol.Dispose 1`] = `
"using r = 0\\n;\\n"
`;

exports[`should not polyfill using Symbol.asyncDispose 1`] = `
"await using r = 0\\n"
`;

exports[`should not transform TypeScript class decorators with multiple decorators 1`] = `
"@sealed\\n@log\\nclass BugReport {\\n type = \\"report\\";\\n title;\\n constructor(t){\\n this.title = t;\\n }\\n}\\nfunction sealed(constructor) {\\n Object.seal(constructor);\\n Object.seal(constructor.prototype);\\n}\\nfunction log(constructor) {\\n console.log(\\"Creating instance of\\", constructor.name);\\n}\\noutput = new BugReport(\\"Test\\");\\n"
`;

exports[`should perform transformation with error 1`] = `
"var Foo = /*#__PURE__*/ function(Foo) {\\n Foo[Foo[\\"Bar\\"] = 7] = \\"Bar\\";\\n Foo[Foo[\\"Baz\\"] = 2] = \\"Baz\\";\\n return Foo;\\n}(Foo || {});\\noutput = 7;\\nthrow new Error(\\"foo\\");\\n"
`;
Expand Down Expand Up @@ -30,10 +42,6 @@ exports[`should perform transformation without source maps and filename 1`] = `
"var Foo = /*#__PURE__*/ function(Foo) {\\n Foo[Foo[\\"Bar\\"] = 7] = \\"Bar\\";\\n Foo[Foo[\\"Baz\\"] = 2] = \\"Baz\\";\\n return Foo;\\n}(Foo || {});\\noutput = 7;\\n"
`;

exports[`should transform TypeScript class decorators with multiple decorators 1`] = `
"@sealed\\n@log\\nclass BugReport {\\n type = \\"report\\";\\n title;\\n constructor(t){\\n this.title = t;\\n }\\n}\\nfunction sealed(constructor) {\\n Object.seal(constructor);\\n Object.seal(constructor.prototype);\\n}\\nfunction log(constructor) {\\n console.log(\\"Creating instance of\\", constructor.name);\\n}\\noutput = new BugReport(\\"Test\\");\\n"
`;

exports[`should transform TypeScript class fields 1`] = `
"class Counter {\\n count = 0;\\n increment() {\\n this.count++;\\n }\\n}\\nconst counter = new Counter();\\ncounter.increment();\\noutput = counter.count;\\n"
`;
Expand Down
22 changes: 21 additions & 1 deletion test/transform.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -182,7 +182,7 @@ test("should transform TypeScript type annotations and type guards", (t) => {
assert.strictEqual(result, true);
});

test("should transform TypeScript class decorators with multiple decorators", (t) => {
test("should not transform TypeScript class decorators with multiple decorators", (t) => {
const inputCode = `
@sealed
@log
Expand Down Expand Up @@ -278,3 +278,23 @@ test("test noEmptyExport", (t) => {
});
t.assert.snapshot(code);
});

test("should not polyfill using Symbol.Dispose", (t) => {
const inputCode = `
using r = 0;`;
const { code } = transformSync(inputCode, {
mode: "transform",
sourceMap: true,
});
t.assert.snapshot(code);
});

test("should not polyfill using Symbol.asyncDispose", (t) => {
const inputCode = `
await using r = 0;`;
const { code } = transformSync(inputCode, {
mode: "transform",
sourceMap: true,
});
t.assert.snapshot(code);
});