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
8 changes: 7 additions & 1 deletion crates/oxc_ecmascript/src/side_effects/context.rs
Original file line number Diff line number Diff line change
@@ -1,3 +1,9 @@
use crate::is_global_reference::IsGlobalReference;

pub trait MayHaveSideEffectsContext: IsGlobalReference {}
pub trait MayHaveSideEffectsContext: IsGlobalReference {
/// Whether to respect the pure annotations.
///
/// Pure annotations are the comments that marks that a expression is pure.
/// For example, `/* @__PURE__ */`, `/* #__NO_SIDE_EFFECTS__ */`.
fn respect_annotations(&self) -> bool;
}
12 changes: 10 additions & 2 deletions crates/oxc_ecmascript/src/side_effects/may_have_side_effects.rs
Original file line number Diff line number Diff line change
Expand Up @@ -486,13 +486,21 @@ fn get_array_minimum_length(arr: &ArrayExpression) -> usize {

impl MayHaveSideEffects for CallExpression<'_> {
fn may_have_side_effects(&self, ctx: &impl MayHaveSideEffectsContext) -> bool {
if self.pure { self.arguments.iter().any(|e| e.may_have_side_effects(ctx)) } else { true }
if self.pure && ctx.respect_annotations() {
self.arguments.iter().any(|e| e.may_have_side_effects(ctx))
} else {
true
}
}
}

impl MayHaveSideEffects for NewExpression<'_> {
fn may_have_side_effects(&self, ctx: &impl MayHaveSideEffectsContext) -> bool {
if self.pure { self.arguments.iter().any(|e| e.may_have_side_effects(ctx)) } else { true }
if self.pure && ctx.respect_annotations() {
self.arguments.iter().any(|e| e.may_have_side_effects(ctx))
} else {
true
}
}
}

Expand Down
6 changes: 5 additions & 1 deletion crates/oxc_minifier/src/ctx.rs
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,11 @@ impl oxc_ecmascript::is_global_reference::IsGlobalReference for Ctx<'_, '_> {
}
}

impl oxc_ecmascript::side_effects::MayHaveSideEffectsContext for Ctx<'_, '_> {}
impl oxc_ecmascript::side_effects::MayHaveSideEffectsContext for Ctx<'_, '_> {
fn respect_annotations(&self) -> bool {
true
}
}

impl<'a> ConstantEvaluationCtx<'a> for Ctx<'a, '_> {
fn ast(&self) -> AstBuilder<'a> {
Expand Down
23 changes: 18 additions & 5 deletions crates/oxc_minifier/tests/ecmascript/may_have_side_effects.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,34 +9,43 @@ use oxc_span::SourceType;

struct Ctx {
global_variable_names: Vec<String>,
annotation: bool,
}
impl IsGlobalReference for Ctx {
fn is_global_reference(&self, ident: &IdentifierReference<'_>) -> Option<bool> {
Some(self.global_variable_names.iter().any(|name| name == ident.name.as_str()))
}
}
impl MayHaveSideEffectsContext for Ctx {}
impl MayHaveSideEffectsContext for Ctx {
fn respect_annotations(&self) -> bool {
self.annotation
}
}

fn test(source_text: &str, expected: bool) {
test_with_global_variables(source_text, vec![], expected);
let ctx = Ctx { global_variable_names: vec![], annotation: true };
test_with_ctx(source_text, &ctx, expected);
}

fn test_with_global_variables(
source_text: &str,
global_variable_names: Vec<String>,
expected: bool,
) {
let ctx = Ctx { global_variable_names, annotation: true };
test_with_ctx(source_text, &ctx, expected);
}

fn test_with_ctx(source_text: &str, ctx: &Ctx, expected: bool) {
let allocator = Allocator::default();
let ret = Parser::new(&allocator, source_text, SourceType::mjs()).parse();
assert!(!ret.panicked, "{source_text}");
assert!(ret.errors.is_empty(), "{source_text}");

let ctx = Ctx { global_variable_names };

let Some(Statement::ExpressionStatement(stmt)) = &ret.program.body.first() else {
panic!("should have a expression statement body: {source_text}");
};
assert_eq!(stmt.expression.may_have_side_effects(&ctx), expected, "{source_text}");
assert_eq!(stmt.expression.may_have_side_effects(ctx), expected, "{source_text}");
}

/// <https://github.com/google/closure-compiler/blob/v20240609/test/com/google/javascript/jscomp/AstAnalyzerTest.java#L362>
Expand Down Expand Up @@ -701,6 +710,10 @@ fn test_call_like_expressions() {
test("/* #__PURE__ */ new Foo(...`${bar}`)", true);
test("/* #__PURE__ */ new Foo(...`${bar()}`)", true);
test("/* #__PURE__ */ new class { constructor() { foo() } }()", false);

let ctx = Ctx { global_variable_names: vec![], annotation: false };
test_with_ctx("/* #__PURE__ */ foo()", &ctx, true);
test_with_ctx("/* #__PURE__ */ new Foo()", &ctx, true);
}

#[test]
Expand Down