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

This commit is contained in:
Donny/강동윤 2024-07-15 19:07:55 +09:00 committed by GitHub
parent b2a22ed771
commit 0e79a5b428
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
10 changed files with 111 additions and 17 deletions

View File

@ -40,6 +40,5 @@ swc_malloc = { version = "0.5.10", path = "../swc_malloc" }
[[bench]]
features = ["scoped"]
harness = false
name = "bench"
harness = false
name = "bench"

View File

@ -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};
@ -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 {

View File

@ -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;
@ -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")]
@ -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::<(), _>(())
})
@ -38,7 +52,7 @@ fn error(input: PathBuf) {
.unwrap();
}
fn opts() -> Options {
fn opts(mode: Mode) -> Options {
Options {
module: None,
filename: None,
@ -46,6 +60,7 @@ fn opts() -> Options {
decorators: true,
..Default::default()
},
mode,
..Default::default()
}
}

View File

@ -0,0 +1,6 @@
const foo = {
foo: 1,
bar: "bar",
baz: foo
};
const bar = "bar";

View File

@ -0,0 +1,2 @@
const foo = 1;
const bar = "bar";

View File

@ -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;

View File

@ -0,0 +1,2 @@
const foo = 1;
const bar = "bar";

View File

@ -0,0 +1,2 @@
const foo = 1;
const bar = "bar";

View File

@ -0,0 +1,2 @@
const foo = 1;
const bar = "bar";

View File

@ -0,0 +1,2 @@
const foo = 1;
const bar = "bar";