mirror of
https://github.com/danbulant/oxc
synced 2026-05-19 04:08:41 +00:00
refactor(minifier): clean up (#8346)
This commit is contained in:
parent
a69d15f299
commit
9a5c66ac6a
11 changed files with 24 additions and 31 deletions
|
|
@ -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);
|
||||
|
|
@ -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)
|
||||
|
|
|
|||
|
|
@ -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.
|
||||
|
|
|
|||
|
|
@ -51,6 +51,8 @@ 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(),
|
||||
|
|
@ -58,7 +60,7 @@ impl PeepholeOptimizations {
|
|||
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,
|
||||
|
|
|
|||
|
|
@ -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
|
||||
///
|
||||
|
|
|
|||
|
|
@ -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
|
||||
///
|
||||
|
|
|
|||
|
|
@ -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,
|
||||
}
|
||||
|
||||
|
|
@ -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)`.
|
||||
|
|
@ -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);
|
||||
}
|
||||
|
||||
|
|
|
|||
|
|
@ -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.
|
||||
///
|
||||
|
|
|
|||
|
|
@ -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>
|
||||
|
|
|
|||
|
|
@ -13,7 +13,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};
|
||||
|
||||
/// A peephole optimization that minimizes code by simplifying conditional
|
||||
/// expressions, replacing IFs with HOOKs, replacing object constructors
|
||||
|
|
@ -21,12 +21,9 @@ use crate::{node_util::Ctx, CompressorPass};
|
|||
/// <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,
|
||||
|
|
|
|||
|
|
@ -2,8 +2,8 @@
|
|||
|
||||
mod ast_passes;
|
||||
mod compressor;
|
||||
mod ctx;
|
||||
mod keep_var;
|
||||
mod node_util;
|
||||
mod options;
|
||||
|
||||
#[cfg(test)]
|
||||
|
|
|
|||
Loading…
Reference in a new issue