From 694d6cceb72f87e53d5afb5af26ed0efc3988577 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=EA=B0=95=EB=8F=99=EC=9C=A4=20=28Donny=29?= Date: Wed, 3 Jul 2024 15:46:37 +0900 Subject: [PATCH 1/3] Scripts --- .../scripts/build_nodejs_release.sh | 4 - .../scripts/build_web_release.sh | 4 - .../__tests__/error.js | 11 + .../__tests__/simple.js | 250 ++++++++++++++++++ .../binding_typescript_wasm/scripts/build.sh | 1 + .../binding_typescript_wasm/scripts/test.sh | 6 + 6 files changed, 268 insertions(+), 8 deletions(-) delete mode 100755 bindings/binding_core_wasm/scripts/build_nodejs_release.sh delete mode 100755 bindings/binding_core_wasm/scripts/build_web_release.sh create mode 100644 bindings/binding_typescript_wasm/__tests__/error.js create mode 100644 bindings/binding_typescript_wasm/__tests__/simple.js create mode 100755 bindings/binding_typescript_wasm/scripts/build.sh create mode 100755 bindings/binding_typescript_wasm/scripts/test.sh diff --git a/bindings/binding_core_wasm/scripts/build_nodejs_release.sh b/bindings/binding_core_wasm/scripts/build_nodejs_release.sh deleted file mode 100755 index 3bb0665c539c..000000000000 --- a/bindings/binding_core_wasm/scripts/build_nodejs_release.sh +++ /dev/null @@ -1,4 +0,0 @@ -#!/bin/bash - -# run this script from the wasm folder ./scripts/build_nodejs_release.sh -npx wasm-pack build --scope swc -t nodejs --features plugin diff --git a/bindings/binding_core_wasm/scripts/build_web_release.sh b/bindings/binding_core_wasm/scripts/build_web_release.sh deleted file mode 100755 index b57746ecb289..000000000000 --- a/bindings/binding_core_wasm/scripts/build_web_release.sh +++ /dev/null @@ -1,4 +0,0 @@ -#!/bin/bash - -# run this script from the wasm folder ./scripts/build_web_release.sh -npx wasm-pack build --scope swc --features plugin diff --git a/bindings/binding_typescript_wasm/__tests__/error.js b/bindings/binding_typescript_wasm/__tests__/error.js new file mode 100644 index 000000000000..f6568a8fa07e --- /dev/null +++ b/bindings/binding_typescript_wasm/__tests__/error.js @@ -0,0 +1,11 @@ +const swc = require("../pkg"); + +it("properly reports error", function () { + expect(() => { + swc.transformSync("Foo {}", {}); + }).toThrow("Syntax Error"); + + expect(() => { + swc.transformSync("Foo {}", {}); + }).toThrow("Expected ';', '}' or "); +}); diff --git a/bindings/binding_typescript_wasm/__tests__/simple.js b/bindings/binding_typescript_wasm/__tests__/simple.js new file mode 100644 index 000000000000..805e79f7bd96 --- /dev/null +++ b/bindings/binding_typescript_wasm/__tests__/simple.js @@ -0,0 +1,250 @@ +const swc = require("../pkg"); + +describe("transform", () => { + it("should work", function () { + const output = swc.transformSync("class Foo {}", {}); + + expect(output).toMatchInlineSnapshot(` + { + "code": "function _class_call_check(instance, Constructor) { + if (!(instance instanceof Constructor)) { + throw new TypeError("Cannot call a class as a function"); + } + } + var Foo = function Foo() { + "use strict"; + _class_call_check(this, Foo); + }; + ", + } + `); + }); + + it("should work with async facade", async () => { + const output = await swc.transform("class Foo {}", {}); + + expect(output).toMatchInlineSnapshot(` + { + "code": "function _class_call_check(instance, Constructor) { + if (!(instance instanceof Constructor)) { + throw new TypeError("Cannot call a class as a function"); + } + } + var Foo = function Foo() { + "use strict"; + _class_call_check(this, Foo); + }; + ", + } + `); + }); + + it("should work with program object", async () => { + const input = swc.parseSync("class Foo {}", { + syntax: "typescript", + target: "es2021", + }); + + const output = await swc.transform(input, {}); + expect(output).toMatchInlineSnapshot(` + { + "code": "function _class_call_check(instance, Constructor) { + if (!(instance instanceof Constructor)) { + throw new TypeError("Cannot call a class as a function"); + } + } + var Foo = function Foo() { + "use strict"; + _class_call_check(this, Foo); + }; + ", + } + `); + }); + + it("should support 'paths' and 'baseUrl'", () => { + const { code } = swc.transformSync( + ` + import foo from '@src/app'; + console.log(foo) + `, + { + filename: "main.js", + jsc: { + parser: { + syntax: "typescript", + }, + target: "es2021", + transform: {}, + baseUrl: __dirname, + paths: { + "@src/*": ["bar/*"], + }, + }, + module: { + type: "commonjs", + }, + } + ); + + expect(code).toContain(`bar/app`); + }); +}); + +describe("parse", () => { + it("should work", () => { + const output = swc.parseSync("class Foo {}", { + syntax: "typescript", + target: "es2021", + }); + + expect(output).toMatchInlineSnapshot(` + { + "body": [ + { + "body": [], + "declare": false, + "decorators": [], + "identifier": { + "optional": false, + "span": { + "ctxt": 2, + "end": 289, + "start": 286, + }, + "type": "Identifier", + "value": "Foo", + }, + "implements": [], + "isAbstract": false, + "span": { + "ctxt": 0, + "end": 292, + "start": 280, + }, + "superClass": null, + "superTypeParams": null, + "type": "ClassDeclaration", + "typeParams": null, + }, + ], + "interpreter": null, + "span": { + "ctxt": 0, + "end": 292, + "start": 280, + }, + "type": "Module", + } + `); + }); + + it("should work with async facade", async () => { + const output = await swc.parse("class Foo {}", { + syntax: "typescript", + target: "es2021", + }); + + expect(output).toMatchInlineSnapshot(` + { + "body": [ + { + "body": [], + "declare": false, + "decorators": [], + "identifier": { + "optional": false, + "span": { + "ctxt": 2, + "end": 302, + "start": 299, + }, + "type": "Identifier", + "value": "Foo", + }, + "implements": [], + "isAbstract": false, + "span": { + "ctxt": 0, + "end": 305, + "start": 293, + }, + "superClass": null, + "superTypeParams": null, + "type": "ClassDeclaration", + "typeParams": null, + }, + ], + "interpreter": null, + "span": { + "ctxt": 0, + "end": 305, + "start": 293, + }, + "type": "Module", + } + `); + }); +}); + +describe("minify", () => { + it("should work", () => { + const output = swc.minifySync( + "const somename = 1; console.log(somename);", + { module: false } + ); + + expect(output).toMatchInlineSnapshot(` + { + "code": "let somename=1;console.log(1);", + } + `); + }); + + it("should work with async facade", async () => { + const output = await swc.minify( + "const somename = 1; console.log(somename);", + { module: false } + ); + + expect(output).toMatchInlineSnapshot(` + { + "code": "let somename=1;console.log(1);", + } + `); + }); +}); + +describe("print", () => { + it("should work", () => { + const input = swc.parseSync("class Foo {}", { + syntax: "typescript", + target: "es2021", + }); + + const output = swc.printSync(input); + expect(output).toMatchInlineSnapshot(` + { + "code": "class Foo { + } + ", + } + `); + }); + + it("should work with async facade", async () => { + const input = swc.parseSync("class Foo {}", { + syntax: "typescript", + target: "es2021", + }); + + const output = await swc.print(input); + expect(output).toMatchInlineSnapshot(` + { + "code": "class Foo { + } + ", + } + `); + }); +}); diff --git a/bindings/binding_typescript_wasm/scripts/build.sh b/bindings/binding_typescript_wasm/scripts/build.sh new file mode 100755 index 000000000000..2e9676dd568d --- /dev/null +++ b/bindings/binding_typescript_wasm/scripts/build.sh @@ -0,0 +1 @@ +wasm-pack build --debug --scope swc -t nodejs --features plugin --features getrandom/js $@ diff --git a/bindings/binding_typescript_wasm/scripts/test.sh b/bindings/binding_typescript_wasm/scripts/test.sh new file mode 100755 index 000000000000..38d3522d0252 --- /dev/null +++ b/bindings/binding_typescript_wasm/scripts/test.sh @@ -0,0 +1,6 @@ +#!/usr/bin/env bash + +set -eu + +./scripts/build.sh +npx jest $@ \ No newline at end of file From 112590455d02f30a0b75421d84d29fd1ac472055 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=EA=B0=95=EB=8F=99=EC=9C=A4=20=28Donny=29?= Date: Wed, 3 Jul 2024 15:49:26 +0900 Subject: [PATCH 2/3] feature flag --- bindings/binding_typescript_wasm/scripts/build.sh | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/bindings/binding_typescript_wasm/scripts/build.sh b/bindings/binding_typescript_wasm/scripts/build.sh index 2e9676dd568d..84e9df918c60 100755 --- a/bindings/binding_typescript_wasm/scripts/build.sh +++ b/bindings/binding_typescript_wasm/scripts/build.sh @@ -1 +1 @@ -wasm-pack build --debug --scope swc -t nodejs --features plugin --features getrandom/js $@ +wasm-pack build --debug --scope swc -t nodejs --features getrandom/js $@ From 377c7ce555b690b04d5e7668ed3f28030feabc24 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=EA=B0=95=EB=8F=99=EC=9C=A4=20=28Donny=29?= Date: Wed, 3 Jul 2024 15:54:55 +0900 Subject: [PATCH 3/3] test --- .../__tests__/error.js | 11 - .../__tests__/simple.js | 250 ------------------ .../__tests__/transform.js | 40 +++ bindings/binding_typescript_wasm/src/lib.rs | 2 +- 4 files changed, 41 insertions(+), 262 deletions(-) delete mode 100644 bindings/binding_typescript_wasm/__tests__/error.js delete mode 100644 bindings/binding_typescript_wasm/__tests__/simple.js create mode 100644 bindings/binding_typescript_wasm/__tests__/transform.js diff --git a/bindings/binding_typescript_wasm/__tests__/error.js b/bindings/binding_typescript_wasm/__tests__/error.js deleted file mode 100644 index f6568a8fa07e..000000000000 --- a/bindings/binding_typescript_wasm/__tests__/error.js +++ /dev/null @@ -1,11 +0,0 @@ -const swc = require("../pkg"); - -it("properly reports error", function () { - expect(() => { - swc.transformSync("Foo {}", {}); - }).toThrow("Syntax Error"); - - expect(() => { - swc.transformSync("Foo {}", {}); - }).toThrow("Expected ';', '}' or "); -}); diff --git a/bindings/binding_typescript_wasm/__tests__/simple.js b/bindings/binding_typescript_wasm/__tests__/simple.js deleted file mode 100644 index 805e79f7bd96..000000000000 --- a/bindings/binding_typescript_wasm/__tests__/simple.js +++ /dev/null @@ -1,250 +0,0 @@ -const swc = require("../pkg"); - -describe("transform", () => { - it("should work", function () { - const output = swc.transformSync("class Foo {}", {}); - - expect(output).toMatchInlineSnapshot(` - { - "code": "function _class_call_check(instance, Constructor) { - if (!(instance instanceof Constructor)) { - throw new TypeError("Cannot call a class as a function"); - } - } - var Foo = function Foo() { - "use strict"; - _class_call_check(this, Foo); - }; - ", - } - `); - }); - - it("should work with async facade", async () => { - const output = await swc.transform("class Foo {}", {}); - - expect(output).toMatchInlineSnapshot(` - { - "code": "function _class_call_check(instance, Constructor) { - if (!(instance instanceof Constructor)) { - throw new TypeError("Cannot call a class as a function"); - } - } - var Foo = function Foo() { - "use strict"; - _class_call_check(this, Foo); - }; - ", - } - `); - }); - - it("should work with program object", async () => { - const input = swc.parseSync("class Foo {}", { - syntax: "typescript", - target: "es2021", - }); - - const output = await swc.transform(input, {}); - expect(output).toMatchInlineSnapshot(` - { - "code": "function _class_call_check(instance, Constructor) { - if (!(instance instanceof Constructor)) { - throw new TypeError("Cannot call a class as a function"); - } - } - var Foo = function Foo() { - "use strict"; - _class_call_check(this, Foo); - }; - ", - } - `); - }); - - it("should support 'paths' and 'baseUrl'", () => { - const { code } = swc.transformSync( - ` - import foo from '@src/app'; - console.log(foo) - `, - { - filename: "main.js", - jsc: { - parser: { - syntax: "typescript", - }, - target: "es2021", - transform: {}, - baseUrl: __dirname, - paths: { - "@src/*": ["bar/*"], - }, - }, - module: { - type: "commonjs", - }, - } - ); - - expect(code).toContain(`bar/app`); - }); -}); - -describe("parse", () => { - it("should work", () => { - const output = swc.parseSync("class Foo {}", { - syntax: "typescript", - target: "es2021", - }); - - expect(output).toMatchInlineSnapshot(` - { - "body": [ - { - "body": [], - "declare": false, - "decorators": [], - "identifier": { - "optional": false, - "span": { - "ctxt": 2, - "end": 289, - "start": 286, - }, - "type": "Identifier", - "value": "Foo", - }, - "implements": [], - "isAbstract": false, - "span": { - "ctxt": 0, - "end": 292, - "start": 280, - }, - "superClass": null, - "superTypeParams": null, - "type": "ClassDeclaration", - "typeParams": null, - }, - ], - "interpreter": null, - "span": { - "ctxt": 0, - "end": 292, - "start": 280, - }, - "type": "Module", - } - `); - }); - - it("should work with async facade", async () => { - const output = await swc.parse("class Foo {}", { - syntax: "typescript", - target: "es2021", - }); - - expect(output).toMatchInlineSnapshot(` - { - "body": [ - { - "body": [], - "declare": false, - "decorators": [], - "identifier": { - "optional": false, - "span": { - "ctxt": 2, - "end": 302, - "start": 299, - }, - "type": "Identifier", - "value": "Foo", - }, - "implements": [], - "isAbstract": false, - "span": { - "ctxt": 0, - "end": 305, - "start": 293, - }, - "superClass": null, - "superTypeParams": null, - "type": "ClassDeclaration", - "typeParams": null, - }, - ], - "interpreter": null, - "span": { - "ctxt": 0, - "end": 305, - "start": 293, - }, - "type": "Module", - } - `); - }); -}); - -describe("minify", () => { - it("should work", () => { - const output = swc.minifySync( - "const somename = 1; console.log(somename);", - { module: false } - ); - - expect(output).toMatchInlineSnapshot(` - { - "code": "let somename=1;console.log(1);", - } - `); - }); - - it("should work with async facade", async () => { - const output = await swc.minify( - "const somename = 1; console.log(somename);", - { module: false } - ); - - expect(output).toMatchInlineSnapshot(` - { - "code": "let somename=1;console.log(1);", - } - `); - }); -}); - -describe("print", () => { - it("should work", () => { - const input = swc.parseSync("class Foo {}", { - syntax: "typescript", - target: "es2021", - }); - - const output = swc.printSync(input); - expect(output).toMatchInlineSnapshot(` - { - "code": "class Foo { - } - ", - } - `); - }); - - it("should work with async facade", async () => { - const input = swc.parseSync("class Foo {}", { - syntax: "typescript", - target: "es2021", - }); - - const output = await swc.print(input); - expect(output).toMatchInlineSnapshot(` - { - "code": "class Foo { - } - ", - } - `); - }); -}); diff --git a/bindings/binding_typescript_wasm/__tests__/transform.js b/bindings/binding_typescript_wasm/__tests__/transform.js new file mode 100644 index 000000000000..a20de8582026 --- /dev/null +++ b/bindings/binding_typescript_wasm/__tests__/transform.js @@ -0,0 +1,40 @@ +const swc = require("../pkg"); + +it("properly reports error", function () { + expect(() => { + swc.transformSync("Foo {}", {}); + }).toThrow(); +}); + +describe("trannsform", () => { + it("should strip types", async () => { + const { code } = await swc.transform( + ` + export const foo: number = 1; + type Foo = number; + `, + {} + ); + expect(code).toMatchInlineSnapshot(` + "export const foo = 1; + " + `); + }); + + it("should preserve enum", async () => { + const { code } = await swc.transform( + ` + enum Foo { + Bar + } + `, + {} + ); + await expect(code).toMatchInlineSnapshot(` + "enum Foo { + Bar + } + " + `); + }); +}); diff --git a/bindings/binding_typescript_wasm/src/lib.rs b/bindings/binding_typescript_wasm/src/lib.rs index 21fb9bcd4115..f580a9e44451 100644 --- a/bindings/binding_typescript_wasm/src/lib.rs +++ b/bindings/binding_typescript_wasm/src/lib.rs @@ -72,7 +72,7 @@ pub fn transform(input: JsString, options: JsValue) -> Promise { future_to_promise(async move { transform_sync(input, options) }) } -#[wasm_bindgen] +#[wasm_bindgen(js_name = "transformSync")] pub fn transform_sync(input: JsString, options: JsValue) -> Result { let options: Options = serde_wasm_bindgen::from_value(options)?;