Skip to content

Commit f1e9a08

Browse files
committed
feat(minifier): fold typeof { foo } when foo is declared (#8947)
An access to declared `foo` is returns false for `is_literal_value`, but it's fine remove it as it doesn't have a sideeffect.
1 parent cdc62a0 commit f1e9a08

2 files changed

Lines changed: 6 additions & 3 deletions

File tree

crates/oxc_ecmascript/src/constant_evaluation/mod.rs

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -395,9 +395,10 @@ pub trait ConstantEvaluation<'a>: MayHaveSideEffects {
395395
match expr.operator {
396396
UnaryOperator::Typeof => {
397397
let s = match &expr.argument {
398-
Expression::ObjectExpression(_) | Expression::ArrayExpression(_)
399-
if expr.argument.is_literal_value(true) =>
400-
{
398+
Expression::ObjectExpression(_) | Expression::ArrayExpression(_) => {
399+
if self.expression_may_have_side_effects(&expr.argument) {
400+
return None;
401+
}
401402
"object"
402403
}
403404
Expression::FunctionExpression(_) => "function",

crates/oxc_minifier/src/peephole/fold_constants.rs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1158,7 +1158,9 @@ mod test {
11581158
fold("x = typeof [1]", "x = \"object\"");
11591159
fold("x = typeof [1,[]]", "x = \"object\"");
11601160
fold("x = typeof {}", "x = \"object\"");
1161+
test("var foo; NOOP(x = typeof { foo })", "var foo; NOOP(x = \"object\")");
11611162
fold("x = typeof function() {}", "x = 'function'");
1163+
fold_same("x = typeof foo"); // no sideeffect, but we don't know the result
11621164

11631165
fold_same("x = typeof[1,[foo()]]");
11641166
fold_same("x = typeof{bathwater:baby()}");

0 commit comments

Comments
 (0)