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
Original file line number Diff line number Diff line change
Expand Up @@ -329,6 +329,10 @@ impl<'a> MayHaveSideEffects<'a> for ArrayExpressionElement<'a> {
Expression::ArrayExpression(arr) => arr.may_have_side_effects(ctx),
Expression::StringLiteral(_) => false,
Expression::TemplateLiteral(t) => t.may_have_side_effects(ctx),
Expression::Identifier(ident) => {
// FIXME: we should treat `arguments` outside a function scope to have sideeffects
!(ident.name == "arguments" && ctx.is_global_reference(ident))
}
_ => true,
},
match_expression!(ArrayExpressionElement) => {
Expand Down
30 changes: 29 additions & 1 deletion crates/oxc_minifier/tests/ecmascript/may_have_side_effects.rs
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
use std::iter;

use javascript_globals::GLOBALS;

use rustc_hash::FxHashSet;
Expand All @@ -22,7 +24,11 @@ struct Ctx {
impl Default for Ctx {
fn default() -> Self {
Self {
global_variable_names: GLOBALS["builtin"].keys().copied().collect::<FxHashSet<_>>(),
global_variable_names: GLOBALS["builtin"]
.keys()
.copied()
.chain(iter::once("arguments"))
.collect::<FxHashSet<_>>(),
annotation: true,
pure_function_names: vec![],
property_read_side_effects: PropertyReadSideEffects::All,
Expand Down Expand Up @@ -91,6 +97,26 @@ fn test_with_ctx(source_text: &str, ctx: &Ctx, expected: bool) {
assert_eq!(stmt.expression.may_have_side_effects(ctx), expected, "{source_text}");
}

#[track_caller]
fn test_in_function(source_text: &str, expected: bool) {
let ctx = Ctx::default();
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 Some(Statement::FunctionDeclaration(stmt)) = &ret.program.body.first() else {
panic!("should have a function declaration: {source_text}");
};
let Some(Statement::ExpressionStatement(stmt)) =
&stmt.body.as_ref().expect("should have a body").statements.first()
else {
panic!("should have a expression statement body: {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>
#[test]
fn closure_compiler_tests() {
Expand Down Expand Up @@ -647,6 +673,8 @@ fn test_array_expression() {
test("[...`foo${foo}`]", true);
test("[...`foo${foo()}`]", true);
test("[...foo()]", true);
// test_in_function("[...arguments]", true);
test_in_function("function foo() { [...arguments] }", false);
}

#[test]
Expand Down
Loading