Skip to content

Commit 8b14ec9

Browse files
committed
fix(minifier): handle { __proto__: null } instanceof Object correctly (#15217)
`{ __proto__: null } instanceof Object` should be `false` or kept as-is, or it was compressed to `true`.
1 parent 4668004 commit 8b14ec9

File tree

3 files changed

+25
-6
lines changed

3 files changed

+25
-6
lines changed

crates/oxc_ecmascript/src/constant_evaluation/mod.rs

Lines changed: 19 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -350,9 +350,25 @@ fn binary_operation_evaluate_value_to<'a>(
350350
if left_ty.is_undetermined() {
351351
return None;
352352
}
353-
return Some(ConstantValue::Boolean(
354-
name == "Object" && left.value_type(ctx).is_object(),
355-
));
353+
if name == "Object" {
354+
if !left_ty.is_object() {
355+
return Some(ConstantValue::Boolean(false));
356+
}
357+
if matches!(
358+
left,
359+
Expression::ArrayExpression(_)
360+
| Expression::RegExpLiteral(_)
361+
| Expression::FunctionExpression(_)
362+
| Expression::ArrowFunctionExpression(_)
363+
| Expression::ClassExpression(_)
364+
) | matches!(left, Expression::ObjectExpression(obj_expr) if obj_expr.properties.is_empty())
365+
{
366+
return Some(ConstantValue::Boolean(true));
367+
}
368+
// NOTE: `{ __proto__: null } instanceof Object` is false
369+
return None;
370+
}
371+
return Some(ConstantValue::Boolean(false));
356372
}
357373
}
358374
None

crates/oxc_minifier/src/peephole/fold_constants.rs

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1819,6 +1819,11 @@ mod test {
18191819
fn test_fold_instance_of_additional() {
18201820
fold("(typeof {}) instanceof Object", "!1");
18211821
fold("(+{}) instanceof Number", "!1");
1822+
fold_same("({ __proto__: null }) instanceof Object");
1823+
fold("/foo/ instanceof Object", "!0");
1824+
fold("(() => {}) instanceof Object", "!0");
1825+
fold("(function(){}) instanceof Object", "!0");
1826+
fold("(class{}) instanceof Object", "!0");
18221827
}
18231828

18241829
#[test]

tasks/coverage/snapshots/minifier_node_compat.snap

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,9 +2,7 @@ commit: 17ac85ca
22

33
minifier_node_compat Summary:
44
AST Parsed : 938/938 (100.00%)
5-
Positive Passed: 929/938 (99.04%)
6-
execution_result: tasks/coverage/ES2015/annex b__proto__ in object literalsbasic support
7-
5+
Positive Passed: 930/938 (99.15%)
86
execution_result: tasks/coverage/ES2015/built-insProxy"getOwnPropertyDescriptor" handler invariants
97

108
execution_result: tasks/coverage/ES2015/miscProxy, internal 'get' callsHasBinding

0 commit comments

Comments
 (0)