feat(es/typescript): Improve fast TS stripper (#9152)

**Related issue:**

- https://github.com/swc-project/swc/pull/9143#pullrequestreview-2160591522
This commit is contained in:
Donny/강동윤 2024-07-05 21:34:03 +09:00 committed by GitHub
parent a26e134bf9
commit 9fca4ab555
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
9 changed files with 28 additions and 24 deletions

View File

@ -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`] = `" "`;
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 {}
: ^^^^^^^^^^^^^
\`----
"
`;
@ -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;
"
`;

View File

@ -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();

View File

@ -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,
@ -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;
}

View File

@ -1,5 +1,5 @@
x TypeScript namespace declaration is not supported in strip-only mode
,----
1 | module 'foo' { }
: ^^^^^^^^^^^^^^^^
1 | module aModuleKeywordNamespace { }
: ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
`----

View File

@ -1 +1 @@
module 'foo' { }
module aModuleKeywordNamespace { }

View File

@ -0,0 +1 @@

View File

@ -0,0 +1 @@
module 'myAmbientModuleDeclaration' { }

View File

@ -0,0 +1 @@

View File

@ -0,0 +1 @@
declare namespace Foo { }