Skip to content

Commit

Permalink
feat(minifier): minimize ~undefined, ~null, ~true, ~false (#8247
Browse files Browse the repository at this point in the history
)
  • Loading branch information
Boshen committed Jan 4, 2025
1 parent f73dc9e commit bd8d677
Show file tree
Hide file tree
Showing 2 changed files with 12 additions and 0 deletions.
4 changes: 4 additions & 0 deletions crates/oxc_ecmascript/src/constant_evaluation/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -440,6 +440,10 @@ pub trait ConstantEvaluation<'a> {
.map(|v| !v.to_int_32())
.map(|v| v as f64)
.map(ConstantValue::Number),
ValueType::Undefined | ValueType::Null => Some(ConstantValue::Number(-1.0)),
ValueType::Boolean => self
.get_side_free_boolean_value(&expr.argument)
.map(|v| ConstantValue::Number(if v { -2.0 } else { -1.0 })),
_ => None,
}
}
Expand Down
8 changes: 8 additions & 0 deletions crates/oxc_minifier/src/ast_passes/peephole_fold_constants.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1447,6 +1447,14 @@ mod test {
test("y | 3 & 7", "y | 3");
}

#[test]
fn test_fold_bitwise_not() {
test("~undefined", "-1");
test("~null", "-1");
test("~false", "-1");
test("~true", "-2");
}

#[test]
fn test_fold_bit_shifts() {
test("x = 1 << 0", "x=1");
Expand Down

0 comments on commit bd8d677

Please sign in to comment.