Skip to content

Commit b0e99fa

Browse files
committed
refactor(minifier): remove all the unnecessary fake ast passes
This also removes handling of making cjs-module-lexer to work.
1 parent d9f5e7f commit b0e99fa

13 files changed

+208
-516
lines changed

crates/oxc_minifier/src/ast_passes/collapse_variable_declarations.rs

+20-46
Original file line numberDiff line numberDiff line change
@@ -1,41 +1,27 @@
11
use oxc_allocator::Vec;
22
use oxc_ast::ast::*;
3-
use oxc_traverse::{traverse_mut_with_ctx, ReusableTraverseCtx, Traverse, TraverseCtx};
4-
5-
use crate::CompressorPass;
6-
7-
/// Collapse variable declarations.
8-
///
9-
/// Join Vars:
10-
/// `var a; var b = 1; var c = 2` => `var a, b = 1; c = 2`
11-
/// <https://github.com/google/closure-compiler/blob/v20240609/src/com/google/javascript/jscomp/CollapseVariableDeclarations.java>
12-
///
13-
/// Collapse into for statements:
14-
/// `var a = 0; for(;a<0;a++) {}` => `for(var a = 0;a<0;a++) {}`
15-
/// <https://github.com/google/closure-compiler/blob/v20240609/src/com/google/javascript/jscomp/Denormalize.java>
16-
pub struct CollapseVariableDeclarations {
17-
pub(crate) changed: bool,
18-
}
19-
20-
impl<'a> CompressorPass<'a> for CollapseVariableDeclarations {
21-
fn build(&mut self, program: &mut Program<'a>, ctx: &mut ReusableTraverseCtx<'a>) {
22-
self.changed = false;
23-
traverse_mut_with_ctx(self, program, ctx);
24-
}
25-
}
26-
27-
impl<'a> Traverse<'a> for CollapseVariableDeclarations {
28-
fn exit_statements(&mut self, stmts: &mut Vec<'a, Statement<'a>>, ctx: &mut TraverseCtx<'a>) {
3+
use oxc_traverse::TraverseCtx;
4+
5+
use super::PeepholeOptimizations;
6+
7+
impl<'a> PeepholeOptimizations {
8+
/// Collapse variable declarations.
9+
///
10+
/// Join Vars:
11+
/// `var a; var b = 1; var c = 2` => `var a, b = 1; c = 2`
12+
/// <https://github.com/google/closure-compiler/blob/v20240609/src/com/google/javascript/jscomp/CollapseVariableDeclarations.java>
13+
///
14+
/// Collapse into for statements:
15+
/// `var a = 0; for(;a<0;a++) {}` => `for(var a = 0;a<0;a++) {}`
16+
/// <https://github.com/google/closure-compiler/blob/v20240609/src/com/google/javascript/jscomp/Denormalize.java>
17+
pub fn collapse_variable_declarations(
18+
&mut self,
19+
stmts: &mut Vec<'a, Statement<'a>>,
20+
ctx: &mut TraverseCtx<'a>,
21+
) {
2922
self.join_vars(stmts, ctx);
3023
self.maybe_collapse_into_for_statements(stmts, ctx);
3124
}
32-
}
33-
34-
// Join Vars
35-
impl<'a> CollapseVariableDeclarations {
36-
pub fn new() -> Self {
37-
Self { changed: false }
38-
}
3925

4026
fn is_require_call(var_decl: &VariableDeclaration) -> bool {
4127
var_decl
@@ -112,7 +98,7 @@ impl<'a> CollapseVariableDeclarations {
11298
}
11399

114100
// Collapse into for statements
115-
impl<'a> CollapseVariableDeclarations {
101+
impl<'a> PeepholeOptimizations {
116102
fn maybe_collapse_into_for_statements(
117103
&mut self,
118104
stmts: &mut Vec<'a, Statement<'a>>,
@@ -240,18 +226,6 @@ mod test {
240226
mod join_vars {
241227
use super::{test, test_same};
242228

243-
#[test]
244-
fn cjs() {
245-
// Do not join `require` calls for cjs-module-lexer.
246-
test_same(
247-
" Object.defineProperty(exports, '__esModule', { value: true });
248-
var compilerDom = require('@vue/compiler-dom');
249-
var runtimeDom = require('@vue/runtime-dom');
250-
var shared = require('@vue/shared');
251-
",
252-
);
253-
}
254-
255229
#[test]
256230
fn test_collapsing() {
257231
// Basic collapsing

crates/oxc_minifier/src/ast_passes/convert_to_dotted_properties.rs

+9-26
Original file line numberDiff line numberDiff line change
@@ -1,27 +1,16 @@
11
use oxc_ast::ast::*;
22
use oxc_syntax::identifier::is_identifier_name;
3-
use oxc_traverse::{traverse_mut_with_ctx, ReusableTraverseCtx, Traverse, TraverseCtx};
3+
use oxc_traverse::TraverseCtx;
44

5-
use crate::{ctx::Ctx, CompressorPass};
5+
use super::PeepholeOptimizations;
6+
use crate::ctx::Ctx;
67

7-
/// Converts property accesses from quoted string or bracket access syntax to dot or unquoted string
8-
/// syntax, where possible. Dot syntax is more compact.
9-
///
10-
/// <https://github.com/google/closure-compiler/blob/v20240609/src/com/google/javascript/jscomp/ConvertToDottedProperties.java>
11-
pub struct ConvertToDottedProperties {
12-
pub(crate) changed: bool,
13-
in_fixed_loop: bool,
14-
}
15-
16-
impl<'a> CompressorPass<'a> for ConvertToDottedProperties {
17-
fn build(&mut self, program: &mut Program<'a>, ctx: &mut ReusableTraverseCtx<'a>) {
18-
self.changed = true;
19-
traverse_mut_with_ctx(self, program, ctx);
20-
}
21-
}
22-
23-
impl<'a> Traverse<'a> for ConvertToDottedProperties {
24-
fn exit_member_expression(
8+
impl<'a> PeepholeOptimizations {
9+
/// Converts property accesses from quoted string or bracket access syntax to dot or unquoted string
10+
/// syntax, where possible. Dot syntax is more compact.
11+
///
12+
/// <https://github.com/google/closure-compiler/blob/v20240609/src/com/google/javascript/jscomp/ConvertToDottedProperties.java>
13+
pub fn convert_to_dotted_properties(
2514
&mut self,
2615
expr: &mut MemberExpression<'a>,
2716
ctx: &mut TraverseCtx<'a>,
@@ -30,12 +19,6 @@ impl<'a> Traverse<'a> for ConvertToDottedProperties {
3019
self.try_compress_computed_member_expression(expr, Ctx(ctx));
3120
}
3221
}
33-
}
34-
35-
impl<'a> ConvertToDottedProperties {
36-
pub fn new(in_fixed_loop: bool) -> Self {
37-
Self { changed: false, in_fixed_loop }
38-
}
3922

4023
/// `foo['bar']` -> `foo.bar`
4124
/// `foo?.['bar']` -> `foo?.bar`

crates/oxc_minifier/src/ast_passes/exploit_assigns.rs

+14-26
Original file line numberDiff line numberDiff line change
@@ -1,31 +1,19 @@
11
use oxc_allocator::Vec;
22
use oxc_ast::ast::*;
3-
use oxc_traverse::{traverse_mut_with_ctx, ReusableTraverseCtx, Traverse, TraverseCtx};
4-
5-
use crate::CompressorPass;
6-
7-
/// Tries to chain assignments together.
8-
///
9-
/// <https://github.com/google/closure-compiler/blob/v20240609/src/com/google/javascript/jscomp/ExploitAssigns.java>
10-
pub struct ExploitAssigns {
11-
pub(crate) changed: bool,
12-
}
13-
14-
impl<'a> CompressorPass<'a> for ExploitAssigns {
15-
fn build(&mut self, program: &mut Program<'a>, ctx: &mut ReusableTraverseCtx<'a>) {
16-
self.changed = false;
17-
traverse_mut_with_ctx(self, program, ctx);
18-
}
19-
}
20-
21-
impl<'a> Traverse<'a> for ExploitAssigns {
22-
fn exit_statements(&mut self, _stmts: &mut Vec<'a, Statement<'a>>, _ctx: &mut TraverseCtx<'a>) {
23-
}
24-
}
25-
26-
impl ExploitAssigns {
27-
pub fn new() -> Self {
28-
Self { changed: false }
3+
use oxc_traverse::TraverseCtx;
4+
5+
use super::PeepholeOptimizations;
6+
7+
impl<'a> PeepholeOptimizations {
8+
/// Tries to chain assignments together.
9+
///
10+
/// <https://github.com/google/closure-compiler/blob/v20240609/src/com/google/javascript/jscomp/ExploitAssigns.java>
11+
#[expect(clippy::unused_self)]
12+
pub fn exploit_assigns(
13+
&mut self,
14+
_stmts: &mut Vec<'a, Statement<'a>>,
15+
_ctx: &mut TraverseCtx<'a>,
16+
) {
2917
}
3018
}
3119

crates/oxc_minifier/src/ast_passes/minimize_exit_points.rs

+14-27
Original file line numberDiff line numberDiff line change
@@ -1,34 +1,21 @@
11
use oxc_allocator::Vec;
22
use oxc_ast::ast::*;
3-
use oxc_traverse::{traverse_mut_with_ctx, ReusableTraverseCtx, Traverse, TraverseCtx};
4-
5-
use crate::CompressorPass;
6-
7-
/// Transform the structure of the AST so that the number of explicit exits
8-
/// are minimized and instead flows to implicit exits conditions.
9-
///
10-
/// <https://github.com/google/closure-compiler/blob/v20240609/src/com/google/javascript/jscomp/MinimizeExitPoints.java>
11-
pub struct MinimizeExitPoints {
12-
pub(crate) changed: bool,
13-
}
14-
15-
impl<'a> CompressorPass<'a> for MinimizeExitPoints {
16-
fn build(&mut self, program: &mut Program<'a>, ctx: &mut ReusableTraverseCtx<'a>) {
17-
self.changed = false;
18-
traverse_mut_with_ctx(self, program, ctx);
19-
}
20-
}
21-
22-
impl Traverse<'_> for MinimizeExitPoints {
23-
fn exit_function_body(&mut self, body: &mut FunctionBody<'_>, _ctx: &mut TraverseCtx<'_>) {
3+
use oxc_traverse::TraverseCtx;
4+
5+
use super::PeepholeOptimizations;
6+
7+
impl<'a> PeepholeOptimizations {
8+
/// Transform the structure of the AST so that the number of explicit exits
9+
/// are minimized and instead flows to implicit exits conditions.
10+
///
11+
/// <https://github.com/google/closure-compiler/blob/v20240609/src/com/google/javascript/jscomp/MinimizeExitPoints.java>
12+
pub fn minimize_exit_points(
13+
&mut self,
14+
body: &mut FunctionBody<'_>,
15+
_ctx: &mut TraverseCtx<'_>,
16+
) {
2417
self.remove_last_return(&mut body.statements);
2518
}
26-
}
27-
28-
impl<'a> MinimizeExitPoints {
29-
pub fn new() -> Self {
30-
Self { changed: false }
31-
}
3219

3320
// `function foo() { return }` -> `function foo() {}`
3421
fn remove_last_return(&mut self, stmts: &mut Vec<'a, Statement<'a>>) {

0 commit comments

Comments
 (0)