Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

refactor(minifier): clean up #8346

Merged
merged 1 commit into from
Jan 8, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
22 changes: 10 additions & 12 deletions crates/oxc_ecmascript/src/constant_evaluation/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -298,17 +298,15 @@ pub trait ConstantEvaluation<'a> {
}
BinaryOperator::BitwiseAnd | BinaryOperator::BitwiseOR | BinaryOperator::BitwiseXOR => {
if left.is_big_int_literal() && right.is_big_int_literal() {
let left_bigint = self.get_side_free_bigint_value(left);
let right_bigint = self.get_side_free_bigint_value(right);
if let (Some(left_val), Some(right_val)) = (left_bigint, right_bigint) {
let result_val: BigInt = match operator {
BinaryOperator::BitwiseAnd => left_val & right_val,
BinaryOperator::BitwiseOR => left_val | right_val,
BinaryOperator::BitwiseXOR => left_val ^ right_val,
_ => unreachable!(),
};
return Some(ConstantValue::BigInt(result_val));
}
let left_val = self.get_side_free_bigint_value(left)?;
let right_val = self.get_side_free_bigint_value(right)?;
let result_val: BigInt = match operator {
BinaryOperator::BitwiseAnd => left_val & right_val,
BinaryOperator::BitwiseOR => left_val | right_val,
BinaryOperator::BitwiseXOR => left_val ^ right_val,
_ => unreachable!(),
};
return Some(ConstantValue::BigInt(result_val));
}
let left_num = self.get_side_free_number_value(left);
let right_num = self.get_side_free_number_value(right);
Expand All @@ -330,7 +328,7 @@ pub trait ConstantEvaluation<'a> {
if left.may_have_side_effects() {
return None;
}
if let Some(right_ident) = right.get_identifier_reference() {
if let Expression::Identifier(right_ident) = right {
let name = right_ident.name.as_str();
if matches!(name, "Object" | "Number" | "Boolean" | "String")
&& self.is_global_reference(right_ident)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ use oxc_ast::ast::*;
use oxc_syntax::identifier::is_identifier_name;
use oxc_traverse::{traverse_mut_with_ctx, ReusableTraverseCtx, Traverse, TraverseCtx};

use crate::{node_util::Ctx, CompressorPass};
use crate::{ctx::Ctx, CompressorPass};

/// Converts property accesses from quoted string or bracket access syntax to dot or unquoted string
/// syntax, where possible. Dot syntax is more compact.
Expand Down
4 changes: 3 additions & 1 deletion crates/oxc_minifier/src/ast_passes/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -51,14 +51,16 @@ pub struct PeepholeOptimizations {
}

impl PeepholeOptimizations {
/// `in_fixed_loop`: Do not compress syntaxes that are hard to analyze inside the fixed loop.
/// Opposite of `late` in Closure Compiler.
pub fn new(in_fixed_loop: bool, options: CompressOptions) -> Self {
Self {
x0_statement_fusion: StatementFusion::new(),
x1_minimize_exit_points: MinimizeExitPoints::new(),
x2_exploit_assigns: ExploitAssigns::new(),
x3_collapse_variable_declarations: CollapseVariableDeclarations::new(),
x4_peephole_remove_dead_code: PeepholeRemoveDeadCode::new(),
x5_peephole_minimize_conditions: PeepholeMinimizeConditions::new(in_fixed_loop),
x5_peephole_minimize_conditions: PeepholeMinimizeConditions::new(),
x6_peephole_substitute_alternate_syntax: PeepholeSubstituteAlternateSyntax::new(
options.target,
in_fixed_loop,
Expand Down
2 changes: 1 addition & 1 deletion crates/oxc_minifier/src/ast_passes/normalize.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ use oxc_ast::ast::*;
use oxc_syntax::scope::ScopeFlags;
use oxc_traverse::{traverse_mut_with_ctx, ReusableTraverseCtx, Traverse, TraverseCtx};

use crate::{node_util::Ctx, CompressorPass};
use crate::{ctx::Ctx, CompressorPass};

/// Normalize AST
///
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ use oxc_syntax::{
};
use oxc_traverse::{traverse_mut_with_ctx, Ancestor, ReusableTraverseCtx, Traverse, TraverseCtx};

use crate::{node_util::Ctx, CompressorPass};
use crate::{ctx::Ctx, CompressorPass};

/// Constant Folding
///
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,10 +14,6 @@ use crate::CompressorPass;
///
/// <https://github.com/google/closure-compiler/blob/v20240609/src/com/google/javascript/jscomp/PeepholeMinimizeConditions.java>
pub struct PeepholeMinimizeConditions {
/// Do not compress syntaxes that are hard to analyze inside the fixed loop.
#[allow(unused)]
in_fixed_loop: bool,

pub(crate) changed: bool,
}

Expand Down Expand Up @@ -70,8 +66,8 @@ impl<'a> Traverse<'a> for PeepholeMinimizeConditions {
}

impl<'a> PeepholeMinimizeConditions {
pub fn new(in_fixed_loop: bool) -> Self {
Self { in_fixed_loop, changed: false }
pub fn new() -> Self {
Self { changed: false }
}

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

fn test(source_text: &str, positive: &str) {
let allocator = Allocator::default();
let mut pass = super::PeepholeMinimizeConditions::new(true);
let mut pass = super::PeepholeMinimizeConditions::new();
tester::test(&allocator, source_text, positive, &mut pass);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ use oxc_ecmascript::{
use oxc_span::SPAN;
use oxc_traverse::{traverse_mut_with_ctx, Ancestor, ReusableTraverseCtx, Traverse, TraverseCtx};

use crate::{keep_var::KeepVar, node_util::Ctx, CompressorPass};
use crate::{ctx::Ctx, keep_var::KeepVar, CompressorPass};

/// Remove Dead Code from the AST.
///
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ use oxc_ecmascript::{
};
use oxc_traverse::{traverse_mut_with_ctx, ReusableTraverseCtx, Traverse, TraverseCtx};

use crate::{node_util::Ctx, CompressorPass};
use crate::{ctx::Ctx, CompressorPass};

/// Minimize With Known Methods
/// <https://github.com/google/closure-compiler/blob/v20240609/src/com/google/javascript/jscomp/PeepholeReplaceKnownMethods.java>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,20 +13,17 @@ use oxc_syntax::{
};
use oxc_traverse::{traverse_mut_with_ctx, Ancestor, ReusableTraverseCtx, Traverse, TraverseCtx};

use crate::{node_util::Ctx, CompressorPass};
use crate::{ctx::Ctx, CompressorPass};

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

in_fixed_loop: bool,

// states
in_define_export: bool,

pub(crate) changed: bool,
Expand Down
2 changes: 1 addition & 1 deletion crates/oxc_minifier/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,8 @@

mod ast_passes;
mod compressor;
mod ctx;
mod keep_var;
mod node_util;
mod options;

#[cfg(test)]
Expand Down
Loading