Skip to content

Commit f82b704

Browse files
committed
refactor(minifier): clean up
1 parent e085d66 commit f82b704

File tree

11 files changed

+24
-31
lines changed

11 files changed

+24
-31
lines changed

crates/oxc_ecmascript/src/constant_evaluation/mod.rs

Lines changed: 10 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -298,17 +298,15 @@ pub trait ConstantEvaluation<'a> {
298298
}
299299
BinaryOperator::BitwiseAnd | BinaryOperator::BitwiseOR | BinaryOperator::BitwiseXOR => {
300300
if left.is_big_int_literal() && right.is_big_int_literal() {
301-
let left_bigint = self.get_side_free_bigint_value(left);
302-
let right_bigint = self.get_side_free_bigint_value(right);
303-
if let (Some(left_val), Some(right_val)) = (left_bigint, right_bigint) {
304-
let result_val: BigInt = match operator {
305-
BinaryOperator::BitwiseAnd => left_val & right_val,
306-
BinaryOperator::BitwiseOR => left_val | right_val,
307-
BinaryOperator::BitwiseXOR => left_val ^ right_val,
308-
_ => unreachable!(),
309-
};
310-
return Some(ConstantValue::BigInt(result_val));
311-
}
301+
let left_val = self.get_side_free_bigint_value(left)?;
302+
let right_val = self.get_side_free_bigint_value(right)?;
303+
let result_val: BigInt = match operator {
304+
BinaryOperator::BitwiseAnd => left_val & right_val,
305+
BinaryOperator::BitwiseOR => left_val | right_val,
306+
BinaryOperator::BitwiseXOR => left_val ^ right_val,
307+
_ => unreachable!(),
308+
};
309+
return Some(ConstantValue::BigInt(result_val));
312310
}
313311
let left_num = self.get_side_free_number_value(left);
314312
let right_num = self.get_side_free_number_value(right);
@@ -330,7 +328,7 @@ pub trait ConstantEvaluation<'a> {
330328
if left.may_have_side_effects() {
331329
return None;
332330
}
333-
if let Some(right_ident) = right.get_identifier_reference() {
331+
if let Expression::Identifier(right_ident) = right {
334332
let name = right_ident.name.as_str();
335333
if matches!(name, "Object" | "Number" | "Boolean" | "String")
336334
&& self.is_global_reference(right_ident)

crates/oxc_minifier/src/ast_passes/convert_to_dotted_properties.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ use oxc_ast::ast::*;
22
use oxc_syntax::identifier::is_identifier_name;
33
use oxc_traverse::{traverse_mut_with_ctx, ReusableTraverseCtx, Traverse, TraverseCtx};
44

5-
use crate::{node_util::Ctx, CompressorPass};
5+
use crate::{ctx::Ctx, CompressorPass};
66

77
/// Converts property accesses from quoted string or bracket access syntax to dot or unquoted string
88
/// syntax, where possible. Dot syntax is more compact.

crates/oxc_minifier/src/ast_passes/mod.rs

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -51,14 +51,16 @@ pub struct PeepholeOptimizations {
5151
}
5252

5353
impl PeepholeOptimizations {
54+
/// `in_fixed_loop`: Do not compress syntaxes that are hard to analyze inside the fixed loop.
55+
/// Opposite of `late` in Closure Compiler.
5456
pub fn new(in_fixed_loop: bool, options: CompressOptions) -> Self {
5557
Self {
5658
x0_statement_fusion: StatementFusion::new(),
5759
x1_minimize_exit_points: MinimizeExitPoints::new(),
5860
x2_exploit_assigns: ExploitAssigns::new(),
5961
x3_collapse_variable_declarations: CollapseVariableDeclarations::new(),
6062
x4_peephole_remove_dead_code: PeepholeRemoveDeadCode::new(),
61-
x5_peephole_minimize_conditions: PeepholeMinimizeConditions::new(in_fixed_loop),
63+
x5_peephole_minimize_conditions: PeepholeMinimizeConditions::new(),
6264
x6_peephole_substitute_alternate_syntax: PeepholeSubstituteAlternateSyntax::new(
6365
options.target,
6466
in_fixed_loop,

crates/oxc_minifier/src/ast_passes/normalize.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ use oxc_ast::ast::*;
22
use oxc_syntax::scope::ScopeFlags;
33
use oxc_traverse::{traverse_mut_with_ctx, ReusableTraverseCtx, Traverse, TraverseCtx};
44

5-
use crate::{node_util::Ctx, CompressorPass};
5+
use crate::{ctx::Ctx, CompressorPass};
66

77
/// Normalize AST
88
///

crates/oxc_minifier/src/ast_passes/peephole_fold_constants.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ use oxc_syntax::{
1010
};
1111
use oxc_traverse::{traverse_mut_with_ctx, Ancestor, ReusableTraverseCtx, Traverse, TraverseCtx};
1212

13-
use crate::{node_util::Ctx, CompressorPass};
13+
use crate::{ctx::Ctx, CompressorPass};
1414

1515
/// Constant Folding
1616
///

crates/oxc_minifier/src/ast_passes/peephole_minimize_conditions.rs

Lines changed: 3 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -14,10 +14,6 @@ use crate::CompressorPass;
1414
///
1515
/// <https://github.com/google/closure-compiler/blob/v20240609/src/com/google/javascript/jscomp/PeepholeMinimizeConditions.java>
1616
pub struct PeepholeMinimizeConditions {
17-
/// Do not compress syntaxes that are hard to analyze inside the fixed loop.
18-
#[allow(unused)]
19-
in_fixed_loop: bool,
20-
2117
pub(crate) changed: bool,
2218
}
2319

@@ -70,8 +66,8 @@ impl<'a> Traverse<'a> for PeepholeMinimizeConditions {
7066
}
7167

7268
impl<'a> PeepholeMinimizeConditions {
73-
pub fn new(in_fixed_loop: bool) -> Self {
74-
Self { in_fixed_loop, changed: false }
69+
pub fn new() -> Self {
70+
Self { changed: false }
7571
}
7672

7773
/// Try to minimize NOT nodes such as `!(x==y)`.
@@ -613,7 +609,7 @@ mod test {
613609

614610
fn test(source_text: &str, positive: &str) {
615611
let allocator = Allocator::default();
616-
let mut pass = super::PeepholeMinimizeConditions::new(true);
612+
let mut pass = super::PeepholeMinimizeConditions::new();
617613
tester::test(&allocator, source_text, positive, &mut pass);
618614
}
619615

crates/oxc_minifier/src/ast_passes/peephole_remove_dead_code.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ use oxc_ecmascript::{
77
use oxc_span::SPAN;
88
use oxc_traverse::{traverse_mut_with_ctx, Ancestor, ReusableTraverseCtx, Traverse, TraverseCtx};
99

10-
use crate::{keep_var::KeepVar, node_util::Ctx, CompressorPass};
10+
use crate::{ctx::Ctx, keep_var::KeepVar, CompressorPass};
1111

1212
/// Remove Dead Code from the AST.
1313
///

crates/oxc_minifier/src/ast_passes/peephole_replace_known_methods.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ use oxc_ecmascript::{
99
};
1010
use oxc_traverse::{traverse_mut_with_ctx, ReusableTraverseCtx, Traverse, TraverseCtx};
1111

12-
use crate::{node_util::Ctx, CompressorPass};
12+
use crate::{ctx::Ctx, CompressorPass};
1313

1414
/// Minimize With Known Methods
1515
/// <https://github.com/google/closure-compiler/blob/v20240609/src/com/google/javascript/jscomp/PeepholeReplaceKnownMethods.java>

crates/oxc_minifier/src/ast_passes/peephole_substitute_alternate_syntax.rs

Lines changed: 2 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -13,20 +13,17 @@ use oxc_syntax::{
1313
};
1414
use oxc_traverse::{traverse_mut_with_ctx, Ancestor, ReusableTraverseCtx, Traverse, TraverseCtx};
1515

16-
use crate::{node_util::Ctx, CompressorPass};
16+
use crate::{ctx::Ctx, CompressorPass};
1717

1818
/// A peephole optimization that minimizes code by simplifying conditional
1919
/// expressions, replacing IFs with HOOKs, replacing object constructors
2020
/// with literals, and simplifying returns.
2121
/// <https://github.com/google/closure-compiler/blob/v20240609/src/com/google/javascript/jscomp/PeepholeSubstituteAlternateSyntax.java>
2222
pub struct PeepholeSubstituteAlternateSyntax {
2323
target: ESTarget,
24-
/// Do not compress syntaxes that are hard to analyze inside the fixed loop.
25-
/// e.g. Do not compress `undefined -> void 0`, `true` -> `!0`.
26-
/// Opposite of `late` in Closure Compiler.
24+
2725
in_fixed_loop: bool,
2826

29-
// states
3027
in_define_export: bool,
3128

3229
pub(crate) changed: bool,

0 commit comments

Comments
 (0)