Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix(es/minifier): Mark all function params as potential property mutation #7409

Merged
merged 1 commit into from
May 18, 2023

Conversation

Austaras
Copy link
Member

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

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

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

export function foo(x) {
    const y = Math.floor(x);
    g(y);
}

But I'd rather do it in a separate PR.

BREAKING CHANGE:

Related issue (if exists):
closes #7402

@kdy1 kdy1 added this to the Planned milestone May 17, 2023
@kdy1 kdy1 requested a review from jridgewell May 17, 2023 22:21
Copy link
Member

@kdy1 kdy1 left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thank you!


swc-bump:

  • swc_ecma_usage_analyzer

@kdy1 kdy1 enabled auto-merge (squash) May 18, 2023 01:19
@kdy1 kdy1 merged commit 5dbbbea into swc-project:main May 18, 2023
@kdy1 kdy1 modified the milestones: Planned, v1.3.59 May 19, 2023
@swc-project swc-project locked as resolved and limited conversation to collaborators Jun 18, 2023
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Development

Successfully merging this pull request may close these issues.

Minified app doesn't work correctly when mutate arrays
3 participants