mirror of
https://github.com/swc-project/swc.git
synced 2024-11-26 09:54:22 +03:00
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:
parent
a26e134bf9
commit
9fca4ab555
@ -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;
|
||||
|
||||
"
|
||||
`;
|
||||
|
@ -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();
|
||||
|
@ -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;
|
||||
}
|
||||
|
@ -1,5 +1,5 @@
|
||||
x TypeScript namespace declaration is not supported in strip-only mode
|
||||
,----
|
||||
1 | module 'foo' { }
|
||||
: ^^^^^^^^^^^^^^^^
|
||||
1 | module aModuleKeywordNamespace { }
|
||||
: ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
|
||||
`----
|
||||
|
@ -1 +1 @@
|
||||
module 'foo' { }
|
||||
module aModuleKeywordNamespace { }
|
1
crates/swc_fast_ts_strip/tests/fixture/modules.js
Normal file
1
crates/swc_fast_ts_strip/tests/fixture/modules.js
Normal file
@ -0,0 +1 @@
|
||||
|
1
crates/swc_fast_ts_strip/tests/fixture/modules.ts
Normal file
1
crates/swc_fast_ts_strip/tests/fixture/modules.ts
Normal file
@ -0,0 +1 @@
|
||||
module 'myAmbientModuleDeclaration' { }
|
1
crates/swc_fast_ts_strip/tests/fixture/namespaces.js
Normal file
1
crates/swc_fast_ts_strip/tests/fixture/namespaces.js
Normal file
@ -0,0 +1 @@
|
||||
|
1
crates/swc_fast_ts_strip/tests/fixture/namespaces.ts
Normal file
1
crates/swc_fast_ts_strip/tests/fixture/namespaces.ts
Normal file
@ -0,0 +1 @@
|
||||
declare namespace Foo { }
|
Loading…
Reference in New Issue
Block a user