Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 12 additions & 0 deletions crates/oxc_ecmascript/src/side_effects/may_have_side_effects.rs
Original file line number Diff line number Diff line change
Expand Up @@ -82,6 +82,12 @@ pub trait MayHaveSideEffects {
!(matches!(ident.name.as_str(), "Infinity" | "NaN" | "undefined")
&& self.is_global_reference(ident))
}
Expression::ArrayExpression(arr) => {
self.array_expression_may_have_side_effects(arr)
}
// unless `Symbol.toPrimitive`, `valueOf`, `toString` is overridden,
// ToPrimitive for an object returns `"[object Object]"`
Expression::ObjectExpression(obj) => !obj.properties.is_empty(),
// ToNumber throws an error when the argument is Symbol / BigInt / an object that
// returns Symbol or BigInt from ToPrimitive
_ => true,
Expand All @@ -98,6 +104,12 @@ pub trait MayHaveSideEffects {
!(matches!(ident.name.as_str(), "Infinity" | "NaN" | "undefined")
&& self.is_global_reference(ident))
}
Expression::ArrayExpression(arr) => {
self.array_expression_may_have_side_effects(arr)
}
// unless `Symbol.toPrimitive`, `valueOf`, `toString` is overridden,
// ToPrimitive for an object returns `"[object Object]"`
Expression::ObjectExpression(obj) => !obj.properties.is_empty(),
// ToNumber throws an error when the argument is Symbol an object that
// returns Symbol from ToPrimitive
_ => true,
Expand Down
6 changes: 6 additions & 0 deletions crates/oxc_minifier/tests/ecmascript/may_have_side_effects.rs
Original file line number Diff line number Diff line change
Expand Up @@ -364,9 +364,12 @@ fn test_unary_expressions() {
test_with_global_variables("+Infinity", vec!["Infinity".to_string()], false);
test_with_global_variables("+NaN", vec!["NaN".to_string()], false);
test_with_global_variables("+undefined", vec!["undefined".to_string()], false); // NaN
test("+[]", false); // 0
test("+[foo()]", true);
test("+foo()", true);
test("+foo", true); // foo can be Symbol or BigInt
test("+Symbol()", true);
test("+{}", false); // NaN
test("+{ valueOf() { return Symbol() } }", true);

test("-0", false);
Expand All @@ -377,9 +380,12 @@ fn test_unary_expressions() {
test_with_global_variables("-Infinity", vec!["Infinity".to_string()], false);
test_with_global_variables("-NaN", vec!["NaN".to_string()], false);
test_with_global_variables("-undefined", vec!["undefined".to_string()], false); // NaN
test("-[]", false); // -0
test("-[foo()]", true);
test("-foo()", true);
test("-foo", true); // foo can be Symbol
test("-Symbol()", true);
test("-{}", false); // NaN
test("-{ valueOf() { return Symbol() } }", true);

test("~0", false);
Expand Down
Loading