diff --git a/.changeset/silly-mugs-heal.md b/.changeset/silly-mugs-heal.md new file mode 100644 index 000000000000..147d312cfaa0 --- /dev/null +++ b/.changeset/silly-mugs-heal.md @@ -0,0 +1,5 @@ +--- +swc_ecma_transforms_typescript: patch +--- + +fix(es/typescript): Preserve more comments diff --git a/crates/swc/tests/fixture/issues-9xxx/9505/input/.swcrc b/crates/swc/tests/fixture/issues-9xxx/9505/input/.swcrc new file mode 100644 index 000000000000..9a2abc4e7b87 --- /dev/null +++ b/crates/swc/tests/fixture/issues-9xxx/9505/input/.swcrc @@ -0,0 +1,15 @@ +{ + "minify": false, + "jsc": { + "parser": { + "syntax": "typescript" + }, + "target": "es2022", + "minify": { + "mangle": false, + "format": { + "comments": "all" + } + } + } +} \ No newline at end of file diff --git a/crates/swc/tests/fixture/issues-9xxx/9505/input/index.ts b/crates/swc/tests/fixture/issues-9xxx/9505/input/index.ts new file mode 100644 index 000000000000..da8acd83df3c --- /dev/null +++ b/crates/swc/tests/fixture/issues-9xxx/9505/input/index.ts @@ -0,0 +1,5 @@ +;// a.ts +const a = 1 + +;// b.ts +const b = 2 \ No newline at end of file diff --git a/crates/swc/tests/fixture/issues-9xxx/9505/output/index.ts b/crates/swc/tests/fixture/issues-9xxx/9505/output/index.ts new file mode 100644 index 000000000000..4d63956e008e --- /dev/null +++ b/crates/swc/tests/fixture/issues-9xxx/9505/output/index.ts @@ -0,0 +1,3 @@ +; // a.ts +const a = 1; // b.ts +const b = 2; diff --git a/crates/swc/tests/tsc-references/generatorTypeCheck59.1.normal.js b/crates/swc/tests/tsc-references/generatorTypeCheck59.1.normal.js index 6f2a7bfd9ac8..8244d04fbe1e 100644 --- a/crates/swc/tests/tsc-references/generatorTypeCheck59.1.normal.js +++ b/crates/swc/tests/tsc-references/generatorTypeCheck59.1.normal.js @@ -7,4 +7,5 @@ function* g() { _ts_decorate([ (yield "") ], C.prototype, "m", null); + ; } diff --git a/crates/swc/tests/tsc-references/generatorTypeCheck60.1.normal.js b/crates/swc/tests/tsc-references/generatorTypeCheck60.1.normal.js index 7610b5c09c47..493e46d5f08a 100644 --- a/crates/swc/tests/tsc-references/generatorTypeCheck60.1.normal.js +++ b/crates/swc/tests/tsc-references/generatorTypeCheck60.1.normal.js @@ -2,4 +2,5 @@ function* g() { class C extends (yield) { } + ; } diff --git a/crates/swc/tests/tsc-references/generatorTypeCheck61.1.normal.js b/crates/swc/tests/tsc-references/generatorTypeCheck61.1.normal.js index ae3d87e2b02e..9da9033b9960 100644 --- a/crates/swc/tests/tsc-references/generatorTypeCheck61.1.normal.js +++ b/crates/swc/tests/tsc-references/generatorTypeCheck61.1.normal.js @@ -6,4 +6,5 @@ function* g() { C = _ts_decorate([ (yield 0) ], C); + ; } diff --git a/crates/swc/tests/tsc-references/parserDoStatement2.1.normal.js b/crates/swc/tests/tsc-references/parserDoStatement2.1.normal.js index 58e1221b4793..21d0951881b8 100644 --- a/crates/swc/tests/tsc-references/parserDoStatement2.1.normal.js +++ b/crates/swc/tests/tsc-references/parserDoStatement2.1.normal.js @@ -1,3 +1,5 @@ //// [parserDoStatement2.ts] -do {}while (false); +do { + ; +}while (false); false; diff --git a/crates/swc/tests/tsc-references/parserSbp_7.9_A9_T3.1.normal.js b/crates/swc/tests/tsc-references/parserSbp_7.9_A9_T3.1.normal.js index 10479aa52f0d..21c282407f18 100644 --- a/crates/swc/tests/tsc-references/parserSbp_7.9_A9_T3.1.normal.js +++ b/crates/swc/tests/tsc-references/parserSbp_7.9_A9_T3.1.normal.js @@ -7,5 +7,7 @@ * @path bestPractice/Sbp_7.9_A9_T3.js * @description Execute do { \n ; \n }while(false) true */ //CHECK#1 -do {}while (false); +do { + ; +}while (false); true; diff --git a/crates/swc/tests/tsc-references/superInStaticMembers1(target=es2015).1.normal.js b/crates/swc/tests/tsc-references/superInStaticMembers1(target=es2015).1.normal.js index d19ac41e6eb9..1cf73ffe9c96 100644 --- a/crates/swc/tests/tsc-references/superInStaticMembers1(target=es2015).1.normal.js +++ b/crates/swc/tests/tsc-references/superInStaticMembers1(target=es2015).1.normal.js @@ -50,6 +50,7 @@ C._ = [ _get(_get_prototype_of(C), "w", C).call(C); })(), (()=>{ + ; // no collision _get(_get_prototype_of(C), "w", C).call(C); })(), (()=>{ diff --git a/crates/swc/tests/tsc-references/superInStaticMembers1(target=es2021).1.normal.js b/crates/swc/tests/tsc-references/superInStaticMembers1(target=es2021).1.normal.js index d19ac41e6eb9..1cf73ffe9c96 100644 --- a/crates/swc/tests/tsc-references/superInStaticMembers1(target=es2021).1.normal.js +++ b/crates/swc/tests/tsc-references/superInStaticMembers1(target=es2021).1.normal.js @@ -50,6 +50,7 @@ C._ = [ _get(_get_prototype_of(C), "w", C).call(C); })(), (()=>{ + ; // no collision _get(_get_prototype_of(C), "w", C).call(C); })(), (()=>{ diff --git a/crates/swc/tests/tsc-references/superInStaticMembers1(target=es2022).1.normal.js b/crates/swc/tests/tsc-references/superInStaticMembers1(target=es2022).1.normal.js index 0303766a7790..e6886753b27c 100644 --- a/crates/swc/tests/tsc-references/superInStaticMembers1(target=es2022).1.normal.js +++ b/crates/swc/tests/tsc-references/superInStaticMembers1(target=es2022).1.normal.js @@ -48,6 +48,7 @@ class C extends B { super.w(); })(), (()=>{ + ; // no collision super.w(); })(), (()=>{ diff --git a/crates/swc/tests/tsc-references/superInStaticMembers1(target=es5).1.normal.js b/crates/swc/tests/tsc-references/superInStaticMembers1(target=es5).1.normal.js index cd1db73fc953..b76a08eb0cc4 100644 --- a/crates/swc/tests/tsc-references/superInStaticMembers1(target=es5).1.normal.js +++ b/crates/swc/tests/tsc-references/superInStaticMembers1(target=es5).1.normal.js @@ -69,6 +69,7 @@ C._ = [ _get(_get_prototype_of(C), "w", C).call(C); }(), function() { + ; // no collision _get(_get_prototype_of(C), "w", C).call(C); }(), function() { diff --git a/crates/swc/tests/tsc-references/superInStaticMembers1(target=esnext).1.normal.js b/crates/swc/tests/tsc-references/superInStaticMembers1(target=esnext).1.normal.js index 0303766a7790..e6886753b27c 100644 --- a/crates/swc/tests/tsc-references/superInStaticMembers1(target=esnext).1.normal.js +++ b/crates/swc/tests/tsc-references/superInStaticMembers1(target=esnext).1.normal.js @@ -48,6 +48,7 @@ class C extends B { super.w(); })(), (()=>{ + ; // no collision super.w(); })(), (()=>{ diff --git a/crates/swc_ecma_transforms_typescript/src/strip_type.rs b/crates/swc_ecma_transforms_typescript/src/strip_type.rs index 41b9fa550774..2c7a5a65506b 100644 --- a/crates/swc_ecma_transforms_typescript/src/strip_type.rs +++ b/crates/swc_ecma_transforms_typescript/src/strip_type.rs @@ -206,7 +206,7 @@ impl VisitMut for StripType { fn visit_mut_stmts(&mut self, n: &mut Vec) { n.visit_mut_children_with(self); - n.retain(|s| !s.is_empty()); + n.retain(|s| !matches!(s, Stmt::Empty(e) if e.span.is_dummy())); } fn visit_mut_stmt(&mut self, n: &mut Stmt) {