mirror of
https://github.com/oxc-project/oxc.git
synced 2026-11-04 18:43:44 +00:00
feat(ecmascript): add unknown_global_side_effects to MayHaveSideEffectsContext (#10130)
For [`treeshake.unknownGlobalSideEffects`](https://rollupjs.org/configuration-options/#treeshake-unknownglobalsideeffects) support.
This commit is contained in:
4 files changed
+33
-1
No files matched your search
@@ -29,4 +29,10 @@ pub trait MayHaveSideEffectsContext: IsGlobalReference {
|
||||
|
||||
/// Whether property read accesses have side effects.
|
||||
fn property_read_side_effects(&self) -> PropertyReadSideEffects;
|
||||
|
||||
/// Whether accessing a global variable has side effects.
|
||||
///
|
||||
/// Accessing a non-existing global variable will throw an error.
|
||||
/// Global variable may be a getter that has side effects.
|
||||
fn unknown_global_side_effects(&self) -> bool;
|
||||
}
|
||||
@@ -75,7 +75,7 @@ impl MayHaveSideEffects for IdentifierReference<'_> {
|
||||
// Reading global variables may have a side effect.
|
||||
// NOTE: It should also return true when the reference might refer to a reference value created by a with statement
|
||||
// NOTE: we ignore TDZ errors
|
||||
_ => ctx.is_global_reference(self) != Some(false),
|
||||
_ => ctx.unknown_global_side_effects() && ctx.is_global_reference(self) != Some(false),
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -37,6 +37,10 @@ impl oxc_ecmascript::side_effects::MayHaveSideEffectsContext for Ctx<'_, '_> {
|
||||
fn property_read_side_effects(&self) -> PropertyReadSideEffects {
|
||||
PropertyReadSideEffects::All
|
||||
}
|
||||
|
||||
fn unknown_global_side_effects(&self) -> bool {
|
||||
true
|
||||
}
|
||||
}
|
||||
|
||||
impl<'a> ConstantEvaluationCtx<'a> for Ctx<'a, '_> {
|
||||
|
||||
@@ -12,6 +12,7 @@ struct Ctx {
|
||||
annotation: bool,
|
||||
pure_function_names: Vec<String>,
|
||||
property_read_side_effects: PropertyReadSideEffects,
|
||||
unknown_global_side_effects: bool,
|
||||
}
|
||||
impl Default for Ctx {
|
||||
fn default() -> Self {
|
||||
@@ -20,6 +21,7 @@ impl Default for Ctx {
|
||||
annotation: true,
|
||||
pure_function_names: vec![],
|
||||
property_read_side_effects: PropertyReadSideEffects::All,
|
||||
unknown_global_side_effects: true,
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -44,6 +46,10 @@ impl MayHaveSideEffectsContext for Ctx {
|
||||
fn property_read_side_effects(&self) -> PropertyReadSideEffects {
|
||||
self.property_read_side_effects
|
||||
}
|
||||
|
||||
fn unknown_global_side_effects(&self) -> bool {
|
||||
self.unknown_global_side_effects
|
||||
}
|
||||
}
|
||||
|
||||
fn test(source_text: &str, expected: bool) {
|
||||
@@ -785,6 +791,22 @@ fn test_property_read_side_effects_support() {
|
||||
// test_with_ctx("({ bar } = foo)", &none_ctx, false);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test_unknown_global_side_effects_support() {
|
||||
let true_ctx = Ctx {
|
||||
unknown_global_side_effects: true,
|
||||
global_variable_names: vec!["foo".to_string()],
|
||||
..Default::default()
|
||||
};
|
||||
let false_ctx = Ctx {
|
||||
unknown_global_side_effects: false,
|
||||
global_variable_names: vec!["foo".to_string()],
|
||||
..Default::default()
|
||||
};
|
||||
test_with_ctx("foo", &true_ctx, true);
|
||||
test_with_ctx("foo", &false_ctx, false);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test_object_with_to_primitive_related_properties_overridden() {
|
||||
test("+{}", false);
|
||||
|
||||
Reference in new issue
Block a user