diff --git a/crates/oxc_ecmascript/src/constant_evaluation/mod.rs b/crates/oxc_ecmascript/src/constant_evaluation/mod.rs index ce365fc68..101ac9908 100644 --- a/crates/oxc_ecmascript/src/constant_evaluation/mod.rs +++ b/crates/oxc_ecmascript/src/constant_evaluation/mod.rs @@ -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, } } diff --git a/crates/oxc_minifier/src/ast_passes/peephole_fold_constants.rs b/crates/oxc_minifier/src/ast_passes/peephole_fold_constants.rs index 67d94e76f..92dbc6772 100644 --- a/crates/oxc_minifier/src/ast_passes/peephole_fold_constants.rs +++ b/crates/oxc_minifier/src/ast_passes/peephole_fold_constants.rs @@ -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");