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(es/typescript): Add swc_fast_ts_strip #9143

Merged
merged 63 commits into from
Jul 5, 2024
Merged
Show file tree
Hide file tree
Changes from 10 commits
Commits
Show all changes
63 commits
Select commit Hold shift + click to select a range
7486ed9
CI
kdy1 Jul 5, 2024
fffa744
commit
kdy1 Jul 5, 2024
0868c9c
tesh.sh
kdy1 Jul 5, 2024
22d922e
test
kdy1 Jul 5, 2024
2c3633a
module/namespace
kdy1 Jul 5, 2024
8f309e0
swap error message
kdy1 Jul 5, 2024
5f9506f
Update jest snapshot
kdy1 Jul 5, 2024
30b6172
Merge branch 'main' into ts-wasm-lint
swc-bot Jul 5, 2024
2218f86
Merge branch 'main' into ts-wasm-lint
kdy1 Jul 5, 2024
05c1076
Update jest snapshot
kdy1 Jul 5, 2024
759f57a
Drop deps
kdy1 Jul 5, 2024
d8f2f78
cargo lockfile
kdy1 Jul 5, 2024
d47a38f
simple codegen
kdy1 Jul 5, 2024
339b698
simplify
kdy1 Jul 5, 2024
19836e4
replace
kdy1 Jul 5, 2024
638a924
test
kdy1 Jul 5, 2024
430011e
test
kdy1 Jul 5, 2024
739e319
Handle no replacement
kdy1 Jul 5, 2024
61dced9
replacement at end
kdy1 Jul 5, 2024
cb427c4
type ann
kdy1 Jul 5, 2024
a6b070f
snapshot
kdy1 Jul 5, 2024
130ed75
Update jest snapshot
kdy1 Jul 5, 2024
acd6dab
strip more
kdy1 Jul 5, 2024
0c643ef
strip more
kdy1 Jul 5, 2024
8fa21f0
Update jest snapshot
kdy1 Jul 5, 2024
aaf51f4
Update test
kdy1 Jul 5, 2024
4f96e71
Add a test
kdy1 Jul 5, 2024
aac52fc
Add a crate
kdy1 Jul 5, 2024
41baf46
cargo lockfile
kdy1 Jul 5, 2024
88e9b82
lib
kdy1 Jul 5, 2024
8493c02
Deps
kdy1 Jul 5, 2024
27c29b2
cargo lockfile
kdy1 Jul 5, 2024
83127f9
Prepare testing
kdy1 Jul 5, 2024
b067b69
cargo lockfiel
kdy1 Jul 5, 2024
04d8439
test
kdy1 Jul 5, 2024
f457375
Rename
kdy1 Jul 5, 2024
d28c82c
Rename
kdy1 Jul 5, 2024
a80e77f
syntax
kdy1 Jul 5, 2024
bb5d434
Update test refs
kdy1 Jul 5, 2024
90670f1
sort
kdy1 Jul 5, 2024
e13685b
UPdate test refs
kdy1 Jul 5, 2024
f4a3653
CI
kdy1 Jul 5, 2024
72565f3
bump
kdy1 Jul 5, 2024
e4e69f8
Depend
kdy1 Jul 5, 2024
b43e19c
cargo lockfile
kdy1 Jul 5, 2024
71c89c5
test
kdy1 Jul 5, 2024
3a2046d
Update jest snapsho
kdy1 Jul 5, 2024
39f24e4
Add tests
kdy1 Jul 5, 2024
364bc54
more syntax
kdy1 Jul 5, 2024
1d86e60
Update test refs
kdy1 Jul 5, 2024
d72e7a6
More syntax
kdy1 Jul 5, 2024
d8620c0
swc_core
kdy1 Jul 5, 2024
0ae794c
cargo lockfile
kdy1 Jul 5, 2024
99805c6
Patch imports
kdy1 Jul 5, 2024
6e3ff48
Patch dep
kdy1 Jul 5, 2024
9fae924
cargo lockfile
kdy1 Jul 5, 2024
fff9efa
lib.rs
kdy1 Jul 5, 2024
60ec5ce
chore: Remove the generic type marker.
magic-akari Jul 5, 2024
f2c5d06
ts this param
magic-akari Jul 5, 2024
4ea8b34
fix blank string
magic-akari Jul 5, 2024
7a1f4f0
cargo lockfile
kdy1 Jul 5, 2024
3a11f8e
Merge branch 'main' into ts-wasm-lint
kdy1 Jul 5, 2024
9fd59b0
make clippy happy
magic-akari Jul 5, 2024
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
10 changes: 9 additions & 1 deletion .github/workflows/CI.yml
Original file line number Diff line number Diff line change
Expand Up @@ -168,6 +168,14 @@ jobs:
if: >-
${{ !contains(github.event.head_commit.message, 'chore: ') }}
runs-on: ubuntu-latest
strategy:
fail-fast: false
matrix:
pkg:
- binding_core_wasm
- binding_minifier_wasm
- binding_typescript_wasm

steps:
- uses: actions/checkout@v4

Expand Down Expand Up @@ -202,7 +210,7 @@ jobs:

- name: Test
run: |
(cd bindings/binding_core_wasm && ./scripts/test.sh)
(cd bindings/${{ matrix.pkg }} && ./scripts/test.sh)

cargo-test:
name: Test - ${{ matrix.settings.crate }} - ${{ matrix.settings.os }}
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
// Jest Snapshot v1, https://goo.gl/fbAQLP

exports[`minify should work 1`] = `
Object {
"code": "console.log(1);",
}
`;
15 changes: 15 additions & 0 deletions bindings/binding_minifier_wasm/__tests__/simple.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
const swc = require("../pkg");

describe("minify", () => {
it("should work", async () => {
const output = await swc.minify(
`{
const a = 1;
console.log(a);
}`,
{}
);

expect(output).toMatchSnapshot();
});
});
6 changes: 6 additions & 0 deletions bindings/binding_minifier_wasm/scripts/test.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
#!/usr/bin/env bash

set -eu

wasm-pack build --out-name wasm --release --scope=swc --target nodejs
npx jest $@
Original file line number Diff line number Diff line change
@@ -1,8 +1,25 @@
// Jest Snapshot v1, https://goo.gl/fbAQLP

exports[`transform in strip-only mode should throw an error when it encounters an enum 1`] = `
exports[`transform in strip-only mode should throw an error when it encounters a module 1`] = `
" x TypeScript namespace declaration is not supported in strip-only mode
,----
1 | module 'foo' {}
: ^^^^^^^^^^^^^^^
\`----
"
`;

exports[`transform in strip-only mode should throw an error when it encounters a namespace 1`] = `
" x TypeScript namespace declaration is not supported in strip-only mode
,----
1 | namespace Foo {}
: ^^^^^^^^^^^^^^^^
\`----
"
x TypeScript enum is not supported in strip-only mode
`;

exports[`transform in strip-only mode should throw an error when it encounters an enum 1`] = `
" x TypeScript enum is not supported in strip-only mode
,----
1 | enum Foo {}
: ^^^^^^^^^^^
Expand All @@ -11,8 +28,7 @@ exports[`transform in strip-only mode should throw an error when it encounters a
`;

exports[`transform in strip-only mode should throw an error with a descriptive message when it encounters a decorator 1`] = `
"
x Decorators are not supported
" x Decorators are not supported
,----
1 | class Foo { @decorator foo() {} }
: ^^^^^^^^^^
Expand Down
22 changes: 18 additions & 4 deletions bindings/binding_typescript_wasm/__tests__/transform.js
Original file line number Diff line number Diff line change
Expand Up @@ -27,13 +27,29 @@ describe("transform", () => {
).rejects.toMatchSnapshot();
});

it('should throw an error with a descriptive message when it encounters a decorator', async () => {
it("should throw an error with a descriptive message when it encounters a decorator", async () => {
await expect(
swc.transform("class Foo { @decorator foo() {} }", {
mode: "strip-only",
})
).rejects.toMatchSnapshot();
})
});

it("should throw an error when it encounters a namespace", async () => {
await expect(
swc.transform("namespace Foo {}", {
mode: "strip-only",
})
).rejects.toMatchSnapshot();
});

it("should throw an error when it encounters a module", async () => {
await expect(
swc.transform("module 'foo' {}", {
mode: "strip-only",
})
).rejects.toMatchSnapshot();
});
});

describe("in transform mode", () => {
Expand All @@ -44,7 +60,5 @@ describe("transform", () => {

expect(code).toMatchSnapshot();
});


});
});
45 changes: 44 additions & 1 deletion bindings/binding_typescript_wasm/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,10 @@ use swc_core::{
FileName, Mark, SourceMap, Spanned, GLOBALS,
},
ecma::{
ast::{Decorator, EsVersion, Program, TsEnumDecl, TsParamPropParam},
ast::{
Decorator, EsVersion, Program, TsEnumDecl, TsModuleDecl, TsNamespaceDecl,
TsParamPropParam,
},
codegen::text_writer::JsWriter,
parser::{
parse_file_as_module, parse_file_as_program, parse_file_as_script, Syntax, TsSyntax,
Expand Down Expand Up @@ -263,6 +266,10 @@ impl Visit for Validator {
}

fn visit_ts_enum_decl(&mut self, e: &TsEnumDecl) {
if e.declare {
return;
}

if matches!(self.mode, Mode::StripOnly) {
HANDLER.with(|handler| {
handler.span_err(
Expand All @@ -276,6 +283,42 @@ impl Visit for Validator {
e.visit_children_with(self);
}

fn visit_ts_module_decl(&mut self, n: &TsModuleDecl) {
if n.declare {
return;
}

if matches!(self.mode, Mode::StripOnly) {
HANDLER.with(|handler| {
handler.span_err(
n.span(),
"TypeScript namespace declaration is not supported in strip-only mode",
);
});
return;
}

n.visit_children_with(self);
}

fn visit_ts_namespace_decl(&mut self, n: &TsNamespaceDecl) {
if n.declare {
return;
}

if matches!(self.mode, Mode::StripOnly) {
HANDLER.with(|handler| {
handler.span_err(
n.span(),
"TypeScript module declaration is not supported in strip-only mode",
);
});
return;
}

n.visit_children_with(self);
}

fn visit_ts_param_prop_param(&mut self, n: &TsParamPropParam) {
if matches!(self.mode, Mode::StripOnly) {
HANDLER.with(|handler| {
Expand Down
Loading