Skip to content

Commit 5d898e8

Browse files
committed
fix(ecmascript): treat shadowed global calls correctly (#13292)
`var Object; Object.create()` was treated as side effect free, but this should be treated as side effectful.
1 parent 593f54c commit 5d898e8

File tree

2 files changed

+11
-6
lines changed

2 files changed

+11
-6
lines changed

crates/oxc_ecmascript/src/side_effects/may_have_side_effects.rs

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -577,7 +577,7 @@ impl<'a> MayHaveSideEffects<'a> for CallExpression<'a> {
577577
return self.arguments.iter().any(|e| e.may_have_side_effects(ctx));
578578
}
579579

580-
let (ident, name) = match &self.callee {
580+
let (object, name) = match &self.callee {
581581
Expression::StaticMemberExpression(member) if !member.optional => {
582582
(member.object.get_identifier_reference(), member.property.name.as_str())
583583
}
@@ -592,10 +592,13 @@ impl<'a> MayHaveSideEffects<'a> for CallExpression<'a> {
592592
_ => return true,
593593
};
594594

595-
let Some(object) = ident.map(|ident| ident.name.as_str()) else { return true };
595+
let Some(object) = object else { return true };
596+
if !ctx.is_global_reference(object) {
597+
return true;
598+
}
596599

597600
#[rustfmt::skip]
598-
let is_global = match object {
601+
let is_global = match object.name.as_str() {
599602
"Array" => matches!(name, "isArray" | "of"),
600603
"ArrayBuffer" => name == "isView",
601604
"Date" => matches!(name, "now" | "parse" | "UTC"),

crates/oxc_minifier/tests/ecmascript/may_have_side_effects.rs

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,3 @@
1-
use std::iter;
2-
31
use javascript_globals::GLOBALS;
42

53
use rustc_hash::FxHashSet;
@@ -27,7 +25,7 @@ impl Default for Ctx {
2725
global_variable_names: GLOBALS["builtin"]
2826
.keys()
2927
.copied()
30-
.chain(iter::once("arguments"))
28+
.chain(["arguments", "URL"])
3129
.collect::<FxHashSet<_>>(),
3230
annotation: true,
3331
pure_function_names: vec![],
@@ -927,6 +925,10 @@ fn test_call_expressions() {
927925
test("Uint32Array.of()", false);
928926
test("Uint8Array.of()", false);
929927
test("Uint8ClampedArray.of()", false);
928+
929+
// may have side effects if shadowed
930+
test_with_global_variables("Date()", &[], true);
931+
test_with_global_variables("Object.create()", &[], true);
930932
}
931933

932934
#[test]

0 commit comments

Comments
 (0)