2021-02-26 12:21:42 +03:00
|
|
|
use std::path::PathBuf;
|
2022-02-09 09:33:32 +03:00
|
|
|
|
2022-04-25 18:31:59 +03:00
|
|
|
use swc_common::{chain, pass::Repeat, Mark};
|
2024-06-21 19:56:37 +03:00
|
|
|
use swc_ecma_parser::{EsSyntax, Syntax};
|
2022-03-15 23:07:04 +03:00
|
|
|
use swc_ecma_transforms_base::fixer::paren_remover;
|
2021-08-13 13:03:04 +03:00
|
|
|
use swc_ecma_transforms_optimization::simplify::{dce::dce, expr_simplifier};
|
2022-03-15 23:07:04 +03:00
|
|
|
use swc_ecma_transforms_testing::{test_fixture, Tester};
|
|
|
|
use swc_ecma_visit::{as_folder, Fold, VisitMut};
|
|
|
|
|
|
|
|
fn remover(t: &Tester) -> impl VisitMut + Fold {
|
|
|
|
as_folder(paren_remover(Some(
|
|
|
|
Box::leak(Box::new(t.comments.clone())) as _
|
|
|
|
)))
|
|
|
|
}
|
2021-02-26 12:21:42 +03:00
|
|
|
|
2021-07-29 08:18:21 +03:00
|
|
|
#[testing::fixture("tests/dce/**/input.js")]
|
2021-02-26 12:21:42 +03:00
|
|
|
fn dce_single_pass(input: PathBuf) {
|
|
|
|
let output = input.with_file_name("output.js");
|
|
|
|
|
|
|
|
test_fixture(
|
2024-06-21 19:56:37 +03:00
|
|
|
Syntax::Es(EsSyntax {
|
2021-05-30 13:52:51 +03:00
|
|
|
decorators: true,
|
2021-02-26 12:21:42 +03:00
|
|
|
..Default::default()
|
|
|
|
}),
|
2022-05-11 07:36:22 +03:00
|
|
|
&|t| {
|
|
|
|
let unresolved_mark = Mark::new();
|
|
|
|
|
|
|
|
chain!(remover(t), dce(Default::default(), unresolved_mark))
|
|
|
|
},
|
2021-02-26 12:21:42 +03:00
|
|
|
&input,
|
|
|
|
&output,
|
2022-10-07 08:52:22 +03:00
|
|
|
Default::default(),
|
2021-02-26 12:21:42 +03:00
|
|
|
);
|
|
|
|
}
|
|
|
|
|
2021-07-29 08:18:21 +03:00
|
|
|
#[testing::fixture("tests/dce/**/input.js")]
|
2021-02-26 12:21:42 +03:00
|
|
|
fn dce_repeated(input: PathBuf) {
|
|
|
|
let output = input.with_file_name("output.full.js");
|
|
|
|
|
|
|
|
test_fixture(
|
2024-06-21 19:56:37 +03:00
|
|
|
Syntax::Es(EsSyntax {
|
2021-05-30 13:52:51 +03:00
|
|
|
decorators: true,
|
2021-02-26 12:21:42 +03:00
|
|
|
..Default::default()
|
|
|
|
}),
|
2022-05-11 07:36:22 +03:00
|
|
|
&|t| {
|
|
|
|
chain!(
|
|
|
|
remover(t),
|
|
|
|
Repeat::new(dce(Default::default(), Mark::new()))
|
|
|
|
)
|
|
|
|
},
|
2021-02-26 12:21:42 +03:00
|
|
|
&input,
|
|
|
|
&output,
|
2022-10-07 08:52:22 +03:00
|
|
|
Default::default(),
|
2021-02-26 12:21:42 +03:00
|
|
|
);
|
|
|
|
}
|
2021-05-13 18:11:27 +03:00
|
|
|
|
2024-07-18 06:50:59 +03:00
|
|
|
#[testing::fixture("tests/dce-jsx/**/input.js")]
|
|
|
|
fn dce_jsx(input: PathBuf) {
|
|
|
|
let output = input.with_file_name("output.js");
|
|
|
|
|
|
|
|
test_fixture(
|
|
|
|
Syntax::Es(EsSyntax {
|
|
|
|
decorators: true,
|
|
|
|
jsx: true,
|
|
|
|
..Default::default()
|
|
|
|
}),
|
|
|
|
&|t| chain!(remover(t), dce(Default::default(), Mark::new())),
|
|
|
|
&input,
|
|
|
|
&output,
|
|
|
|
Default::default(),
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
2021-07-29 08:18:21 +03:00
|
|
|
#[testing::fixture("tests/expr-simplifier/**/input.js")]
|
2021-05-13 18:11:27 +03:00
|
|
|
fn expr(input: PathBuf) {
|
|
|
|
let output = input.with_file_name("output.js");
|
|
|
|
|
|
|
|
test_fixture(
|
2023-03-30 11:06:02 +03:00
|
|
|
Syntax::Es(Default::default()),
|
2022-04-25 18:31:59 +03:00
|
|
|
&|t| {
|
|
|
|
let top_level_mark = Mark::fresh(Mark::root());
|
|
|
|
|
|
|
|
chain!(
|
|
|
|
remover(t),
|
|
|
|
Repeat::new(expr_simplifier(top_level_mark, Default::default()))
|
|
|
|
)
|
|
|
|
},
|
2021-05-13 18:11:27 +03:00
|
|
|
&input,
|
|
|
|
&output,
|
2022-10-07 08:52:22 +03:00
|
|
|
Default::default(),
|
2021-05-13 18:11:27 +03:00
|
|
|
);
|
|
|
|
}
|