Skip to content

Commit

Permalink
fix(es/typescript): Fix transform mode (#9243)
Browse files Browse the repository at this point in the history
  • Loading branch information
kdy1 committed Jul 15, 2024
1 parent b2a22ed commit 0e79a5b
Show file tree
Hide file tree
Showing 10 changed files with 111 additions and 17 deletions.
5 changes: 2 additions & 3 deletions crates/swc_allocator/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,5 @@ swc_malloc = { version = "0.5.10", path = "../swc_malloc" }


[[bench]]
features = ["scoped"]
harness = false
name = "bench"
harness = false
name = "bench"
27 changes: 17 additions & 10 deletions crates/swc_fast_ts_strip/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,12 @@ use swc_ecma_parser::{
token::{IdentLike, KnownIdent, Token, TokenAndSpan, Word},
Capturing, Parser, StringInput, Syntax, TsSyntax,
};
use swc_ecma_transforms_base::{fixer::fixer, helpers::inject_helpers, hygiene::hygiene, resolver};
use swc_ecma_transforms_base::{
fixer::fixer,
helpers::{inject_helpers, Helpers, HELPERS},
hygiene::hygiene,
resolver,
};
use swc_ecma_transforms_typescript::typescript;
use swc_ecma_visit::{Visit, VisitMutWith, VisitWith};

Expand Down Expand Up @@ -218,19 +223,21 @@ pub fn operate(
let unresolved_mark = Mark::new();
let top_level_mark = Mark::new();

program.visit_mut_with(&mut resolver(unresolved_mark, top_level_mark, true));
HELPERS.set(&Helpers::new(false), || {
program.visit_mut_with(&mut resolver(unresolved_mark, top_level_mark, true));

program.visit_mut_with(&mut typescript::typescript(
options.transform.unwrap_or_default(),
unresolved_mark,
top_level_mark,
));
program.visit_mut_with(&mut typescript::typescript(
options.transform.unwrap_or_default(),
unresolved_mark,
top_level_mark,
));

program.visit_mut_with(&mut inject_helpers(unresolved_mark));
program.visit_mut_with(&mut inject_helpers(unresolved_mark));

program.visit_mut_with(&mut hygiene());
program.visit_mut_with(&mut hygiene());

program.visit_mut_with(&mut fixer(Some(&comments)));
program.visit_mut_with(&mut fixer(Some(&comments)));
});

let mut src = vec![];
let mut src_map_buf = if options.source_map {
Expand Down
23 changes: 19 additions & 4 deletions crates/swc_fast_ts_strip/tests/fixture.rs
Original file line number Diff line number Diff line change
@@ -1,16 +1,17 @@
use std::path::PathBuf;

use swc_ecma_parser::TsSyntax;
use swc_fast_ts_strip::{operate, Options};
use swc_fast_ts_strip::{operate, Mode, Options};
use testing::NormalizedOutput;

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

testing::run_test(false, |cm, handler| {
let code = operate(&cm, handler, input_code, opts())
let code = operate(&cm, handler, input_code.clone(), opts(Mode::StripOnly))
.expect("should not return Err()")
.code;

Expand All @@ -21,6 +22,19 @@ fn test(input: PathBuf) {
Ok(())
})
.expect("should not fail");

testing::run_test(false, |cm, handler| {
let code = operate(&cm, handler, input_code, opts(Mode::Transform))
.expect("should not return Err()")
.code;

NormalizedOutput::new_raw(code)
.compare_to_file(transform_output_file)
.unwrap();

Ok(())
})
.expect("should not fail");
}

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

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

Err::<(), _>(())
})
Expand All @@ -38,14 +52,15 @@ fn error(input: PathBuf) {
.unwrap();
}

fn opts() -> Options {
fn opts(mode: Mode) -> Options {
Options {
module: None,
filename: None,
parser: TsSyntax {
decorators: true,
..Default::default()
},
mode,
..Default::default()
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
const foo = {
foo: 1,
bar: "bar",
baz: foo
};
const bar = "bar";
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
const foo = 1;
const bar = "bar";
57 changes: 57 additions & 0 deletions crates/swc_fast_ts_strip/tests/fixture/output.transform.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
let x = 1;
[];
"test";
class C extends Array {
field = "";
static accessor f1;
f2;
method(a) {}
get g() {
return 1;
}
set g(v) {}
}
class D extends C {
method(...args) {}
}
class A {
b;
}
{
let m = new Map([]);
}{
let a = foo;
}{
let a = foo([]);
}{
let f = function(p) {};
}{
function overload() {}
}void 0;
export { C };
function foo(p = ()=>1) {
return p;
}
void 0;
void 0;
void 0;
void 0;
void 0;
void 0;
{
()=>1;
}{
()=>1;
}{
()=>1;
}{
()=>1;
}{
()=>1;
}{
(a, b, c = [])=>1;
}()=>1;
{
(a, b, c = [])=>1;
}()=>1;
()=>1;
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
const foo = 1;
const bar = "bar";
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
const foo = 1;
const bar = "bar";
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
const foo = 1;
const bar = "bar";
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
const foo = 1;
const bar = "bar";

0 comments on commit 0e79a5b

Please sign in to comment.