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
10 changes: 3 additions & 7 deletions crates/oxc_minifier/docs/ASSUMPTIONS.md
Original file line number Diff line number Diff line change
Expand Up @@ -98,17 +98,13 @@ eval('var x = 1');
new Function('return x');
```

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

`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.
`arguments` variables are only accessed inside functions. We intend to change this assumption to optional in the future.

```javascript
// The minifier assumes this never happens:
console.log(arguments); // This is not the arguments object
function f(a) {
arguments = 2; // This makes the arguments variable point not to point to the arguments object
return a;
}
console.log(arguments);
```

## Optional Assumptions
Expand Down
17 changes: 16 additions & 1 deletion crates/oxc_minifier/src/peephole/substitute_alternate_syntax.rs
Original file line number Diff line number Diff line change
Expand Up @@ -530,6 +530,11 @@ impl<'a> PeepholeOptimizations {
}
}

// In non-strict mode, a different value may be reassigned to the `arguments` variable
if !ctx.current_scope_flags().is_strict_mode() {
return;
}

// FIXME: this function treats `arguments` not inside a function scope as if they are inside it
// we should check in a different way than `ctx.is_global_reference`

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

use crate::{
CompressOptions, CompressOptionsUnused,
options::CompressOptionsKeepNames,
tester::{default_options, test, test_options, test_same, test_same_options},
tester::{
default_options, test, test_options, test_same, test_same_options,
test_same_options_source_type,
},
};

#[test]
Expand Down Expand Up @@ -2346,6 +2355,12 @@ mod test {
test_same(
"for (var e = arguments.length, r = Array(e > 1 ? e - 2 : 0), a = 2; a < e; a++) r[a - 2] = arguments[a];",
);

test_same_options_source_type(
"for (var e = arguments.length, r = Array(e), a = 0; a < e; a++) r[a] = arguments[a]; console.log(r)",
SourceType::cjs(),
&default_options(),
);
}

#[test]
Expand Down
Loading