swc/crates/swc_ecma_minifier/tests/fixture/issues/7402/input.js
Austaras 5dbbbea2ef
fix(es/minifier): Mark all function params as potential property mutation (#7409)
**Description:**

This issue is more severe than I originally thought. It raises not in
array indexing, but in function calls and property mutation. We should
treat all function arguments as potentially be property mutated,
otherwise following example

```js
class A {
	a = 1

	toString() {
		return this.a
	}
}

const a = new A()

function foo(x) {
	x.a++
}

const b = a + 1

foo(a)

console.log(b)
```

would be error(It should log 2, but logs 3 after compress).

As the result, massive regressions is unavoidable, since some of these
optimizations may indeed cause error. Part of them can be mitigated with
following optimization -- allow inline of ident even if its original
value is mutated. Consider

```js
export function foo(x) {
   const y = x
   x.a = 1
   y.b = 2
}
```

If x is a primitive value, all mutations to its properties are ignored;
if x is a object, then y refers to the same object no matter what
mutation is performed.

And there's still room for more, currently following code
```js
export function foo(x) {
    const y = Math.floor(x);
    g(y);
}
```
But I'd rather do it in a separate PR.


**Related issue:**

 - Closes #7402.
2023-05-18 01:19:17 +00:00

26 lines
418 B
JavaScript

export function mutate(out) {
out[0] = 1;
out[1] = 2;
out[2] = 3;
return out;
}
export const myFunc = (function () {
const temp = [0, 0, 0];
return function (out) {
const scaling = temp;
mutate(scaling);
out[0] = 1 / scaling[0];
out[1] = 1 / scaling[1];
out[2] = 1 / scaling[2];
return out;
};
})();
const out = [1, 2, 3];
myFunc(out);