mirror of
https://github.com/swc-project/swc.git
synced 2024-12-18 11:11:30 +03:00
6a97c7bef8
# API Changes
## `swc_ecma_utils`
### Renames
- `find_ids` => `find_pat_ids`
- `prepend` => `prepend_stmt`
- `as_bool` => `cast_to_bool`
- `as_number` => `as_pure_number`
- `as_string` => `cast_to_string`
- (`as_pure_bool`, `cast_to_number` is not renamed)
- `UsageFinder` => `IdentUsageFinder`.
- `IdentFinder` => `IdentRefFinder`.
## Changes
- Re-export of `HANDLER` from `swc_common::errors` is removed. Use `swc_common::error::HANDLER` instead.
- Re-export of `Id` from `swc_ecma_ast` is removed. Use `swc_ecma_ast::Id` instead.
- `ExprCtx` is used for apis related to `Expr`.
- `preserve_effects` is now a method of `ExprCtx`. It was a top-level function.
- `extract_side_effects_to` is now a method of `ExprCtx`. It was a top-level function.
- `IdentUsageFinder` (previously `UsageFinder`) now takes `Id` instead of `Ident`.
- `IdentRefFinder` (previously `IdentFinder`) now takes `Id` instead of `Ident`.
- `ExprExt::is_ident_ref_to` is removed.
- `Expr::is_global_ref_to` is added.
- `:🆔:id` is removed. Use `Ident::to_id()` instead.
91 lines
2.2 KiB
Rust
91 lines
2.2 KiB
Rust
use std::path::PathBuf;
|
|
|
|
use swc_common::{chain, pass::Repeat, Mark};
|
|
use swc_ecma_parser::{EsConfig, Syntax};
|
|
use swc_ecma_transforms_base::fixer::paren_remover;
|
|
use swc_ecma_transforms_optimization::simplify::{dce::dce, expr_simplifier};
|
|
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 _
|
|
)))
|
|
}
|
|
|
|
#[testing::fixture("tests/dce/**/input.js")]
|
|
fn dce_single_pass(input: PathBuf) {
|
|
let output = input.with_file_name("output.js");
|
|
|
|
test_fixture(
|
|
Syntax::Es(EsConfig {
|
|
decorators: true,
|
|
..Default::default()
|
|
}),
|
|
&|t| {
|
|
let unresolved_mark = Mark::new();
|
|
|
|
chain!(remover(t), dce(Default::default(), unresolved_mark))
|
|
},
|
|
&input,
|
|
&output,
|
|
);
|
|
}
|
|
|
|
#[testing::fixture("tests/dce/**/input.js")]
|
|
fn dce_repeated(input: PathBuf) {
|
|
let output = input.with_file_name("output.full.js");
|
|
|
|
test_fixture(
|
|
Syntax::Es(EsConfig {
|
|
decorators: true,
|
|
..Default::default()
|
|
}),
|
|
&|t| {
|
|
chain!(
|
|
remover(t),
|
|
Repeat::new(dce(Default::default(), Mark::new()))
|
|
)
|
|
},
|
|
&input,
|
|
&output,
|
|
);
|
|
}
|
|
|
|
#[testing::fixture("tests/dce-jsx/**/input.js")]
|
|
fn dce_jsx(input: PathBuf) {
|
|
let output = input.with_file_name("output.js");
|
|
|
|
test_fixture(
|
|
Syntax::Es(EsConfig {
|
|
decorators: true,
|
|
jsx: true,
|
|
..Default::default()
|
|
}),
|
|
&|t| chain!(remover(t), dce(Default::default(), Mark::new())),
|
|
&input,
|
|
&output,
|
|
);
|
|
}
|
|
|
|
#[testing::fixture("tests/expr-simplifier/**/input.js")]
|
|
fn expr(input: PathBuf) {
|
|
let output = input.with_file_name("output.js");
|
|
|
|
test_fixture(
|
|
Syntax::Es(EsConfig {
|
|
..Default::default()
|
|
}),
|
|
&|t| {
|
|
let top_level_mark = Mark::fresh(Mark::root());
|
|
|
|
chain!(
|
|
remover(t),
|
|
Repeat::new(expr_simplifier(top_level_mark, Default::default()))
|
|
)
|
|
},
|
|
&input,
|
|
&output,
|
|
);
|
|
}
|