feat(minifier): compress typeof addition string (#8301)

This commit is contained in:
camc314 2025-01-06 23:26:18 +00:00
parent 4d2888d6fc
commit 6afc590db8

View file

@ -334,6 +334,18 @@ impl<'a, 'b> PeepholeFoldConstants {
}
}
}
// typeof foo + ""
if let Expression::UnaryExpression(left) = &e.left {
if left.operator.is_typeof() {
if let Expression::StringLiteral(right) = &e.right {
if right.value.is_empty() {
return Some(ctx.ast.move_expression(&mut e.left));
}
}
}
}
None
}
@ -1777,4 +1789,12 @@ mod test {
test("true.toString()", "'true'");
test("false.toString()", "'false'");
}
#[test]
fn test_fold_typeof_addition_string() {
test_same("typeof foo");
test_same("typeof foo + '123'");
test("typeof foo + ''", "typeof foo");
test_same("typeof foo - ''");
}
}