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

Duplicated identifier used when mangle is enabled #1155

Closed
renanccastro opened this issue Feb 24, 2022 · 3 comments
Closed

Duplicated identifier used when mangle is enabled #1155

renanccastro opened this issue Feb 24, 2022 · 3 comments
Labels
bug mangle Issue in the mangler

Comments

@renanccastro
Copy link

renanccastro commented Feb 24, 2022

Bug report

Terser is using the same identifier for the function, and for the const defined inside the if. This produces code that can't be evaluated.

Here is a simple reproduction case:

function f1() {
	if (true) {
		function f2() {
			return 1;
		}

		const var1 = 666;
	}
}

If var1 is outside the "if", the correct identifier is used when minifying.

Used options:

{
  module: true,
  compress: {
    unused: false
  }
}

Curent terser output

function n(){{function n(){return 1}const n=666}}

note the inner function n() and the const n, they clash.

@fabiosantoscode
Copy link
Collaborator

fabiosantoscode commented Feb 27, 2022

Terser really can't deal with scopes of function declarations when they're inside of a block. This section of the spec isn't being properly followed.

I'm thinking that this pattern should trigger the scope's pinned property, so that inlining and dead code elimination is no longer attempted in these scopes.

I hate to learn that this ancient issue caused grief to someone.

@fabiosantoscode fabiosantoscode added mangle Issue in the mangler and removed compress Issue in the compression step labels Feb 27, 2022
@fabiosantoscode
Copy link
Collaborator

fabiosantoscode commented Mar 4, 2022

Note that declaring 'use strict', fixes the problem too. So this is really 2 bugs in one as module: true isn't being followed.

@fabiosantoscode
Copy link
Collaborator

fabiosantoscode commented Mar 4, 2022

I've been looking around and trying different things. I can fix this but haven't decided what's the best long term fix. To fix the case of section B 3.2.1 (funcdefs in block scopes) or avoid mangling 2 things in the same function to the same name altogether, which fixes all the cases in that section?

Since block scope conflicts are often causes of bugs (and incompatibility with older Safari) I'm thinking that the mangler should avoid reusing any names in the same scope. This will, though, make the output slightly larger.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug mangle Issue in the mangler
Projects
None yet
Development

No branches or pull requests

3 participants