Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix(es/typescript): Fix transform mode #9243

Merged
merged 4 commits into from
Jul 15, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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";
Loading