Skip to content
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
Original file line number Diff line number Diff line change
Expand Up @@ -2,15 +2,13 @@ use oxc_allocator::Vec;
use oxc_ast::ast::*;
use oxc_traverse::{Traverse, TraverseCtx};

use crate::{CompressOptions, CompressorPass};
use crate::CompressorPass;

/// Collapse variable declarations.
///
/// `var a; var b = 1; var c = 2` => `var a, b = 1; c = 2`
/// <https://github.com/google/closure-compiler/blob/master/src/com/google/javascript/jscomp/CollapseVariableDeclarations.java>
pub struct CollapseVariableDeclarations {
options: CompressOptions,

changed: bool,
}

Expand All @@ -32,8 +30,8 @@ impl<'a> Traverse<'a> for CollapseVariableDeclarations {
}

impl<'a> CollapseVariableDeclarations {
pub fn new(options: CompressOptions) -> Self {
Self { options, changed: false }
pub fn new() -> Self {
Self { changed: false }
}

fn is_require_call(var_decl: &VariableDeclaration) -> bool {
Expand All @@ -58,7 +56,7 @@ impl<'a> CollapseVariableDeclarations {
}

fn join_vars(&mut self, stmts: &mut Vec<'a, Statement<'a>>, ctx: &mut TraverseCtx<'a>) {
if !self.options.join_vars || stmts.len() < 2 {
if stmts.len() < 2 {
return;
}

Expand Down Expand Up @@ -115,11 +113,11 @@ impl<'a> CollapseVariableDeclarations {
mod test {
use oxc_allocator::Allocator;

use crate::{tester, CompressOptions};
use crate::tester;

fn test(source_text: &str, expected: &str) {
let allocator = Allocator::default();
let mut pass = super::CollapseVariableDeclarations::new(CompressOptions::default());
let mut pass = super::CollapseVariableDeclarations::new();
tester::test(&allocator, source_text, expected, &mut pass);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,15 +9,13 @@ use oxc_syntax::{
};
use oxc_traverse::{Ancestor, Traverse, TraverseCtx};

use crate::{node_util::Ctx, CompressOptions, CompressorPass};
use crate::{node_util::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/master/src/com/google/javascript/jscomp/PeepholeSubstituteAlternateSyntax.java>
pub struct PeepholeSubstituteAlternateSyntax {
options: CompressOptions,

/// 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 Compier.
Expand Down Expand Up @@ -162,8 +160,8 @@ impl<'a> Traverse<'a> for PeepholeSubstituteAlternateSyntax {
}

impl<'a, 'b> PeepholeSubstituteAlternateSyntax {
pub fn new(in_fixed_loop: bool, options: CompressOptions) -> Self {
Self { options, in_fixed_loop, in_define_export: false, changed: false }
pub fn new(in_fixed_loop: bool) -> Self {
Self { in_fixed_loop, in_define_export: false, changed: false }
}

/* Utilities */
Expand Down Expand Up @@ -214,14 +212,13 @@ impl<'a, 'b> PeepholeSubstituteAlternateSyntax {
/* Expressions */

/// Transforms boolean expression `true` => `!0` `false` => `!1`.
/// Enabled by `compress.booleans`.
/// Do not compress `true` in `Object.defineProperty(exports, 'Foo', {enumerable: true, ...})`.
fn try_compress_boolean(&mut self, expr: &mut Expression<'a>, ctx: Ctx<'a, 'b>) {
if self.in_fixed_loop {
return;
}
let Expression::BooleanLiteral(lit) = expr else { return };
if self.options.booleans && !self.in_define_export {
if !self.in_define_export {
let parent = ctx.ancestry.parent();
let no_unary = {
if let Ancestor::BinaryExpressionRight(u) = parent {
Expand Down Expand Up @@ -255,10 +252,7 @@ impl<'a, 'b> PeepholeSubstituteAlternateSyntax {

/// Compress `typeof foo == "undefined"` into `typeof foo > "u"`
/// Enabled by `compress.typeofs`
fn compress_typeof_undefined(&self, expr: &mut BinaryExpression<'a>, ctx: Ctx<'a, 'b>) {
if !self.options.typeofs {
return;
}
fn compress_typeof_undefined(&mut self, expr: &mut BinaryExpression<'a>, ctx: Ctx<'a, 'b>) {
if !matches!(expr.operator, BinaryOperator::Equality | BinaryOperator::StrictEquality) {
return;
}
Expand Down Expand Up @@ -289,6 +283,7 @@ impl<'a, 'b> PeepholeSubstituteAlternateSyntax {
ctx.ast.expression_from_string_literal(right),
);
*expr = binary_expr;
self.changed = true;
}

fn commutative_pair<A, F, G, RetF: 'a, RetG: 'a>(
Expand Down Expand Up @@ -588,12 +583,11 @@ impl<'a, 'b> PeepholeSubstituteAlternateSyntax {
mod test {
use oxc_allocator::Allocator;

use crate::{tester, CompressOptions};
use crate::tester;

fn test(source_text: &str, expected: &str) {
let allocator = Allocator::default();
let mut pass =
super::PeepholeSubstituteAlternateSyntax::new(false, CompressOptions::default());
let mut pass = super::PeepholeSubstituteAlternateSyntax::new(false);
tester::test(&allocator, source_text, expected, &mut pass);
}

Expand Down
10 changes: 3 additions & 7 deletions crates/oxc_minifier/src/compressor.rs
Original file line number Diff line number Diff line change
Expand Up @@ -48,10 +48,7 @@ impl<'a> Compressor<'a> {
&mut PeepholeRemoveDeadCode::new(),
// TODO: MinimizeExitPoints
&mut PeepholeMinimizeConditions::new(),
&mut PeepholeSubstituteAlternateSyntax::new(
/* in_fixed_loop */ true,
self.options,
),
&mut PeepholeSubstituteAlternateSyntax::new(/* in_fixed_loop */ true),
&mut PeepholeReplaceKnownMethods::new(),
&mut PeepholeFoldConstants::new(),
];
Expand All @@ -77,11 +74,10 @@ impl<'a> Compressor<'a> {

// Passes listed in `getFinalization` in `DefaultPassConfig`
ExploitAssigns::new().build(program, &mut ctx);
CollapseVariableDeclarations::new(self.options).build(program, &mut ctx);
CollapseVariableDeclarations::new().build(program, &mut ctx);

// Late latePeepholeOptimizations
PeepholeSubstituteAlternateSyntax::new(/* in_fixed_loop */ false, self.options)
.build(program, &mut ctx);
PeepholeSubstituteAlternateSyntax::new(/* in_fixed_loop */ false).build(program, &mut ctx);
}

fn dead_code_elimination(program: &mut Program<'a>, ctx: &mut TraverseCtx<'a>) {
Expand Down
47 changes: 2 additions & 45 deletions crates/oxc_minifier/src/options.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,6 @@
pub struct CompressOptions {
pub dead_code_elimination: bool,

/// Various optimizations for boolean context, for example `!!a ? b : c` → `a ? b : c`.
///
/// Default `true`
pub booleans: bool,

/// Remove `debugger;` statements.
///
/// Default `true`
Expand All @@ -16,26 +11,6 @@ pub struct CompressOptions {
///
/// Default `false`
pub drop_console: bool,

/// Attempt to evaluate constant expressions
///
/// Default `true`
pub evaluate: bool,

/// Join consecutive var statements.
///
/// Default `true`
pub join_vars: bool,

/// Optimizations for do, while and for loops when we can statically determine the condition
///
/// Default `true`
pub loops: bool,

/// Transforms `typeof foo == "undefined" into `foo === void 0`
///
/// Default `true`
pub typeofs: bool,
}

#[allow(clippy::derivable_impls)]
Expand All @@ -47,29 +22,11 @@ impl Default for CompressOptions {

impl CompressOptions {
pub fn all_true() -> Self {
Self {
dead_code_elimination: false,
booleans: true,
drop_debugger: true,
drop_console: true,
evaluate: true,
join_vars: true,
loops: true,
typeofs: true,
}
Self { dead_code_elimination: false, drop_debugger: true, drop_console: true }
}

pub fn all_false() -> Self {
Self {
dead_code_elimination: false,
booleans: false,
drop_debugger: false,
drop_console: false,
evaluate: false,
join_vars: false,
loops: false,
typeofs: false,
}
Self { dead_code_elimination: false, drop_debugger: false, drop_console: false }
}

pub fn dead_code_elimination() -> Self {
Expand Down
5 changes: 0 additions & 5 deletions crates/oxc_wasm/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -264,13 +264,8 @@ impl Oxc {
mangle: minifier_options.mangle.unwrap_or_default(),
compress: if minifier_options.compress.unwrap_or_default() {
CompressOptions {
booleans: compress_options.booleans,
drop_console: compress_options.drop_console,
drop_debugger: compress_options.drop_debugger,
evaluate: compress_options.evaluate,
join_vars: compress_options.join_vars,
loops: compress_options.loops,
typeofs: compress_options.typeofs,
..CompressOptions::default()
}
} else {
Expand Down
5 changes: 1 addition & 4 deletions tasks/minsize/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -95,10 +95,7 @@ pub fn run() -> Result<(), io::Error> {

fn minify_twice(file: &TestFile) -> String {
let source_type = SourceType::from_path(&file.file_name).unwrap();
let options = MinifierOptions {
mangle: true,
compress: CompressOptions { evaluate: false, ..CompressOptions::default() },
};
let options = MinifierOptions { mangle: true, compress: CompressOptions::default() };
// let source_text1 = minify(&file.source_text, source_type, options);
// let source_text2 = minify(&source_text1, source_type, options);
// assert!(source_text1 == source_text2, "Minification failed for {}", &file.file_name);
Expand Down