Skip to content

Commit 0e79a5b

Browse files
authoredJul 15, 2024··
fix(es/typescript): Fix transform mode (#9243)
1 parent b2a22ed commit 0e79a5b

File tree

10 files changed

+111
-17
lines changed

10 files changed

+111
-17
lines changed
 

‎crates/swc_allocator/Cargo.toml

+2-3
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,5 @@ swc_malloc = { version = "0.5.10", path = "../swc_malloc" }
4040

4141

4242
[[bench]]
43-
features = ["scoped"]
44-
harness = false
45-
name = "bench"
43+
harness = false
44+
name = "bench"

‎crates/swc_fast_ts_strip/src/lib.rs

+17-10
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,12 @@ use swc_ecma_parser::{
2222
token::{IdentLike, KnownIdent, Token, TokenAndSpan, Word},
2323
Capturing, Parser, StringInput, Syntax, TsSyntax,
2424
};
25-
use swc_ecma_transforms_base::{fixer::fixer, helpers::inject_helpers, hygiene::hygiene, resolver};
25+
use swc_ecma_transforms_base::{
26+
fixer::fixer,
27+
helpers::{inject_helpers, Helpers, HELPERS},
28+
hygiene::hygiene,
29+
resolver,
30+
};
2631
use swc_ecma_transforms_typescript::typescript;
2732
use swc_ecma_visit::{Visit, VisitMutWith, VisitWith};
2833

@@ -218,19 +223,21 @@ pub fn operate(
218223
let unresolved_mark = Mark::new();
219224
let top_level_mark = Mark::new();
220225

221-
program.visit_mut_with(&mut resolver(unresolved_mark, top_level_mark, true));
226+
HELPERS.set(&Helpers::new(false), || {
227+
program.visit_mut_with(&mut resolver(unresolved_mark, top_level_mark, true));
222228

223-
program.visit_mut_with(&mut typescript::typescript(
224-
options.transform.unwrap_or_default(),
225-
unresolved_mark,
226-
top_level_mark,
227-
));
229+
program.visit_mut_with(&mut typescript::typescript(
230+
options.transform.unwrap_or_default(),
231+
unresolved_mark,
232+
top_level_mark,
233+
));
228234

229-
program.visit_mut_with(&mut inject_helpers(unresolved_mark));
235+
program.visit_mut_with(&mut inject_helpers(unresolved_mark));
230236

231-
program.visit_mut_with(&mut hygiene());
237+
program.visit_mut_with(&mut hygiene());
232238

233-
program.visit_mut_with(&mut fixer(Some(&comments)));
239+
program.visit_mut_with(&mut fixer(Some(&comments)));
240+
});
234241

235242
let mut src = vec![];
236243
let mut src_map_buf = if options.source_map {

‎crates/swc_fast_ts_strip/tests/fixture.rs

+19-4
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,17 @@
11
use std::path::PathBuf;
22

33
use swc_ecma_parser::TsSyntax;
4-
use swc_fast_ts_strip::{operate, Options};
4+
use swc_fast_ts_strip::{operate, Mode, Options};
55
use testing::NormalizedOutput;
66

77
#[testing::fixture("tests/fixture/**/*.ts")]
88
fn test(input: PathBuf) {
99
let input_code = std::fs::read_to_string(&input).unwrap();
1010
let output_file = input.with_extension("js");
11+
let transform_output_file = input.with_file_name("output.transform.js");
1112

1213
testing::run_test(false, |cm, handler| {
13-
let code = operate(&cm, handler, input_code, opts())
14+
let code = operate(&cm, handler, input_code.clone(), opts(Mode::StripOnly))
1415
.expect("should not return Err()")
1516
.code;
1617

@@ -21,6 +22,19 @@ fn test(input: PathBuf) {
2122
Ok(())
2223
})
2324
.expect("should not fail");
25+
26+
testing::run_test(false, |cm, handler| {
27+
let code = operate(&cm, handler, input_code, opts(Mode::Transform))
28+
.expect("should not return Err()")
29+
.code;
30+
31+
NormalizedOutput::new_raw(code)
32+
.compare_to_file(transform_output_file)
33+
.unwrap();
34+
35+
Ok(())
36+
})
37+
.expect("should not fail");
2438
}
2539

2640
#[testing::fixture("tests/errors/**/*.ts")]
@@ -29,7 +43,7 @@ fn error(input: PathBuf) {
2943
let output_file = input.with_extension("swc-stderr");
3044

3145
testing::run_test(false, |cm, handler| {
32-
operate(&cm, handler, input_code, opts()).expect("should not return Err()");
46+
operate(&cm, handler, input_code, opts(Mode::StripOnly)).expect("should not return Err()");
3347

3448
Err::<(), _>(())
3549
})
@@ -38,14 +52,15 @@ fn error(input: PathBuf) {
3852
.unwrap();
3953
}
4054

41-
fn opts() -> Options {
55+
fn opts(mode: Mode) -> Options {
4256
Options {
4357
module: None,
4458
filename: None,
4559
parser: TsSyntax {
4660
decorators: true,
4761
..Default::default()
4862
},
63+
mode,
4964
..Default::default()
5065
}
5166
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
const foo = {
2+
foo: 1,
3+
bar: "bar",
4+
baz: foo
5+
};
6+
const bar = "bar";
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
const foo = 1;
2+
const bar = "bar";
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,57 @@
1+
let x = 1;
2+
[];
3+
"test";
4+
class C extends Array {
5+
field = "";
6+
static accessor f1;
7+
f2;
8+
method(a) {}
9+
get g() {
10+
return 1;
11+
}
12+
set g(v) {}
13+
}
14+
class D extends C {
15+
method(...args) {}
16+
}
17+
class A {
18+
b;
19+
}
20+
{
21+
let m = new Map([]);
22+
}{
23+
let a = foo;
24+
}{
25+
let a = foo([]);
26+
}{
27+
let f = function(p) {};
28+
}{
29+
function overload() {}
30+
}void 0;
31+
export { C };
32+
function foo(p = ()=>1) {
33+
return p;
34+
}
35+
void 0;
36+
void 0;
37+
void 0;
38+
void 0;
39+
void 0;
40+
void 0;
41+
{
42+
()=>1;
43+
}{
44+
()=>1;
45+
}{
46+
()=>1;
47+
}{
48+
()=>1;
49+
}{
50+
()=>1;
51+
}{
52+
(a, b, c = [])=>1;
53+
}()=>1;
54+
{
55+
(a, b, c = [])=>1;
56+
}()=>1;
57+
()=>1;
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
const foo = 1;
2+
const bar = "bar";
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
const foo = 1;
2+
const bar = "bar";
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
const foo = 1;
2+
const bar = "bar";
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
const foo = 1;
2+
const bar = "bar";

0 commit comments

Comments
 (0)
Please sign in to comment.