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): Improve fast TS stripper #9152

Merged
merged 5 commits into from
Jul 5, 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
Original file line number Diff line number Diff line change
@@ -1,51 +1,51 @@
// Jest Snapshot v1, https://goo.gl/fbAQLP

exports[`transform in strip-only mode should remove declare enum 1`] = `""`;
exports[`transform in strip-only mode should remove declare enum 1`] = `" "`;
kdy1 marked this conversation as resolved.
Show resolved Hide resolved

exports[`transform in strip-only mode should remove declare enum 2`] = `""`;
exports[`transform in strip-only mode should remove declare enum 2`] = `" "`;

exports[`transform in strip-only mode should remove declare enum 3`] = `""`;
exports[`transform in strip-only mode should remove declare enum 3`] = `" "`;

exports[`transform in strip-only mode should strip complex expressions 1`] = `
"const foo = {
foo: 1,
bar: "bar",
};
foo: 1 ,
bar: "bar" ,
} ;
const bar = "bar";"
`;

exports[`transform in strip-only mode should strip nonnull assertions 1`] = `
"const foo = 1;
"const foo = 1 ;
const bar = "bar";"
`;

exports[`transform in strip-only mode should strip satisfies 1`] = `
"const foo = 1;
"const foo = 1 ;
const bar = "bar";"
`;

exports[`transform in strip-only mode should strip type annotations 1`] = `
"const foo = 1;
const bar = "bar";"
const bar = "bar";"
`;

exports[`transform in strip-only mode should strip type assertions 1`] = `
"const foo = 1;
"const foo = 1 ;
const bar = "bar";"
`;

exports[`transform in strip-only mode should strip type declarations 1`] = `
"const foo = 1;


const bar = "bar";"
const bar = "bar";"
`;

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' {}
: ^^^^^^^^^^^^^^^
1 | module foo {}
: ^^^^^^^^^^^^^
\`----
"
`;
Expand Down Expand Up @@ -79,7 +79,7 @@ exports[`transform in strip-only mode should throw an error with a descriptive m

exports[`transform should strip types 1`] = `
"
export const foo = 1;

export const foo = 1;
"
`;
2 changes: 1 addition & 1 deletion bindings/binding_typescript_wasm/__tests__/transform.js
Original file line number Diff line number Diff line change
Expand Up @@ -133,7 +133,7 @@ describe("transform", () => {

it("should throw an error when it encounters a module", async () => {
await expect(
swc.transform("module 'foo' {}", {
swc.transform("module foo {}", {
mode: "strip-only",
})
).rejects.toMatchSnapshot();
Expand Down
6 changes: 3 additions & 3 deletions crates/swc_fast_ts_strip/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,8 @@ use swc_common::{
};
use swc_ecma_ast::{
BindingIdent, Decorator, EsVersion, Ident, Param, Pat, Program, TsAsExpr, TsConstAssertion,
TsEnumDecl, TsInstantiation, TsModuleDecl, TsNamespaceDecl, TsNonNullExpr, TsParamPropParam,
TsSatisfiesExpr, TsTypeAliasDecl, TsTypeAnn,
TsEnumDecl, TsInstantiation, TsModuleDecl, TsModuleName, TsNamespaceDecl, TsNonNullExpr,
TsParamPropParam, TsSatisfiesExpr, TsTypeAliasDecl, TsTypeAnn,
};
use swc_ecma_parser::{
parse_file_as_module, parse_file_as_program, parse_file_as_script, Syntax, TsSyntax,
Expand Down Expand Up @@ -154,7 +154,7 @@ impl Visit for TsStrip {
}

fn visit_ts_module_decl(&mut self, n: &TsModuleDecl) {
if n.declare {
if n.declare || matches!(n.id, TsModuleName::Str(..)) {
self.add_replacement(n.span);
return;
}
Expand Down
4 changes: 2 additions & 2 deletions crates/swc_fast_ts_strip/tests/errors/modules.swc-stderr
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
x TypeScript namespace declaration is not supported in strip-only mode
,----
1 | module 'foo' { }
: ^^^^^^^^^^^^^^^^
1 | module aModuleKeywordNamespace { }
: ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
`----
2 changes: 1 addition & 1 deletion crates/swc_fast_ts_strip/tests/errors/modules.ts
Original file line number Diff line number Diff line change
@@ -1 +1 @@
module 'foo' { }
module aModuleKeywordNamespace { }
1 change: 1 addition & 0 deletions crates/swc_fast_ts_strip/tests/fixture/modules.js
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@

1 change: 1 addition & 0 deletions crates/swc_fast_ts_strip/tests/fixture/modules.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
module 'myAmbientModuleDeclaration' { }
1 change: 1 addition & 0 deletions crates/swc_fast_ts_strip/tests/fixture/namespaces.js
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@

1 change: 1 addition & 0 deletions crates/swc_fast_ts_strip/tests/fixture/namespaces.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
declare namespace Foo { }
Loading