Skip to content

Commit

Permalink
feat(minifier): compress typeof addition string (#8301)
Browse files Browse the repository at this point in the history
  • Loading branch information
camc314 committed Jan 6, 2025
1 parent 4d2888d commit 6afc590
Showing 1 changed file with 20 additions and 0 deletions.
20 changes: 20 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 @@ -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
}

Expand Down Expand Up @@ -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 - ''");
}
}

0 comments on commit 6afc590

Please sign in to comment.