feat(minifier): minimize ~undefined, ~null, ~true, ~false (#8247)

This commit is contained in:
Boshen 2025-01-04 13:34:53 +00:00
parent f73dc9ea9b
commit bd8d677352
2 changed files with 12 additions and 0 deletions

View file

@ -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,
}
}

View file

@ -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");