Skip to content

Commit b149a97

Browse files
committed
feat(minifier): only apply arguments copy loop transformation in strict mode
1 parent 8f83a58 commit b149a97

File tree

2 files changed

+19
-8
lines changed

2 files changed

+19
-8
lines changed

crates/oxc_minifier/docs/ASSUMPTIONS.md

Lines changed: 3 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -98,17 +98,13 @@ eval('var x = 1');
9898
new Function('return x');
9999
```
100100

101-
### `arguments` is always [the arguments object](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Functions/arguments)
101+
### No access to a variable named `arguments` outside functions
102102

103-
`arguments` variables are only accessed inside functions and not assigned with a different value. We intend to change this assumption to optional in the future.
103+
`arguments` variables are only accessed inside functions. We intend to change this assumption to optional in the future.
104104

105105
```javascript
106106
// The minifier assumes this never happens:
107-
console.log(arguments); // This is not the arguments object
108-
function f(a) {
109-
arguments = 2; // This makes the arguments variable point not to point to the arguments object
110-
return a;
111-
}
107+
console.log(arguments);
112108
```
113109

114110
## Optional Assumptions

crates/oxc_minifier/src/peephole/substitute_alternate_syntax.rs

Lines changed: 16 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -530,6 +530,11 @@ impl<'a> PeepholeOptimizations {
530530
}
531531
}
532532

533+
// In non-strict mode, a different value may be reassigned to the `arguments` variable
534+
if !ctx.current_scope_flags().is_strict_mode() {
535+
return;
536+
}
537+
533538
// FIXME: this function treats `arguments` not inside a function scope as if they are inside it
534539
// we should check in a different way than `ctx.is_global_reference`
535540

@@ -1517,12 +1522,16 @@ where
15171522
/// Port from <https://github.com/google/closure-compiler/blob/v20240609/test/com/google/javascript/jscomp/PeepholeSubstituteAlternateSyntaxTest.java>
15181523
#[cfg(test)]
15191524
mod test {
1525+
use oxc_span::SourceType;
15201526
use oxc_syntax::es_target::ESTarget;
15211527

15221528
use crate::{
15231529
CompressOptions, CompressOptionsUnused,
15241530
options::CompressOptionsKeepNames,
1525-
tester::{default_options, test, test_options, test_same, test_same_options},
1531+
tester::{
1532+
default_options, test, test_options, test_same, test_same_options,
1533+
test_same_options_source_type,
1534+
},
15261535
};
15271536

15281537
#[test]
@@ -2346,6 +2355,12 @@ mod test {
23462355
test_same(
23472356
"for (var e = arguments.length, r = Array(e > 1 ? e - 2 : 0), a = 2; a < e; a++) r[a - 2] = arguments[a];",
23482357
);
2358+
2359+
test_same_options_source_type(
2360+
"for (var e = arguments.length, r = Array(e), a = 0; a < e; a++) r[a] = arguments[a]; console.log(r)",
2361+
SourceType::cjs(),
2362+
&default_options(),
2363+
);
23492364
}
23502365

23512366
#[test]

0 commit comments

Comments
 (0)