Skip to content

Commit

Permalink
test: check syntax is not polyfilled
Browse files Browse the repository at this point in the history
  • Loading branch information
marco-ippolito committed Nov 14, 2024
1 parent 818ed8e commit 3fe0090
Show file tree
Hide file tree
Showing 3 changed files with 59 additions and 5 deletions.
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();`;
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 using Symbol.Dispose 1`] = `
"class Resource {\\n [Symbol.dispose]() {\\n console.log(\\"Disposed\\");\\n }\\n}\\nusing r = new Resource()\\n;\\n"
`;

exports[`should not polyfill using using Symbol.asyncDispose 1`] = `
"class AsyncResource {\\n async [Symbol.asyncDispose]() {\\n console.log(\\"Async disposed\\");\\n }\\n}\\nawait using r = new AsyncResource()\\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
28 changes: 27 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,29 @@ test("test noEmptyExport", (t) => {
});
t.assert.snapshot(code);
});

test("should not polyfill using Symbol.Dispose", (t) => {
const inputCode = `
class Resource {
[Symbol.dispose]() { console.log("Disposed"); }
}
using r = new Resource();`;
const { code } = transformSync(inputCode, {
mode: "transform",
sourceMap: true,
});
t.assert.snapshot(code);
});

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, {
mode: "transform",
sourceMap: true,
});
t.assert.snapshot(code);
});

0 comments on commit 3fe0090

Please sign in to comment.