From 3fe0090b945fa1ded9eda9ff8722fdeea6b48c5c Mon Sep 17 00:00:00 2001 From: Marco Ippolito Date: Thu, 14 Nov 2024 10:02:15 +0100 Subject: [PATCH] test: check syntax is not polyfilled --- test/index.test.js | 20 ++++++++++++++++ test/snapshots/transform.test.js.snapshot | 16 +++++++++---- test/transform.test.js | 28 ++++++++++++++++++++++- 3 files changed, 59 insertions(+), 5 deletions(-) diff --git a/test/index.test.js b/test/index.test.js index 374245a69..90c9f922f 100644 --- a/test/index.test.js +++ b/test/index.test.js @@ -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); +}); diff --git a/test/snapshots/transform.test.js.snapshot b/test/snapshots/transform.test.js.snapshot index d39644af4..c5f0db8f2 100644 --- a/test/snapshots/transform.test.js.snapshot +++ b/test/snapshots/transform.test.js.snapshot @@ -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" `; @@ -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" `; diff --git a/test/transform.test.js b/test/transform.test.js index 4f8a56923..20fac0650 100644 --- a/test/transform.test.js +++ b/test/transform.test.js @@ -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 @@ -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); +});