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

No more class chaining since 5.15.1 (class C extends class B extends class A) #1308

Closed
VandeurenGlenn opened this issue Nov 19, 2022 · 4 comments

Comments

@VandeurenGlenn
Copy link

Bug report or Feature request?
One of the two ^^

Version (complete output of terser -V or specific git commit)
v5.15.1
Complete CLI command or minify() options used

{
    mangle: false,
    keep_classnames: true,
    ecma: 6
}

terser input

class A  {
  constructor() {
    console.log('super')
  }
}

class B extends A {
  constructor() {
    super()
  }
}

class C extends B {
  constructor() {
    super()
  }
}

export { C as default } 

terser output or error

class A {constructor(){console.log('super')}}class B extends A{constructor(){super()}}class C extendsB{constructor(){super}}

Expected result

class C extends class B extends class A {constructor(){console.log('super')}}{constructor(){super()}}{constructor(){super()}}
VandeurenGlenn added a commit to leofcoin/monorepo that referenced this issue Nov 19, 2022
@fabiosantoscode
Copy link
Collaborator

Classes aren't inlined anymore in some cases to prevent them being created multiple times and then being !== from themselves after a compilation (eg #1267).

It's really tricky to analyze whether something will be executed multiple times (and specializing that for Extends), so I think this is a good trade-off for less scoping bugs, assuming it doesn't increase source code size a lot.

@VandeurenGlenn
Copy link
Author

@fabiosantoscode Thanks for the answer, only problem here is that if I try to put the result as text in a 'new Function(code)'
It fails now, I tried multiple ways like adding semicolon after each class etc but no success, only seems to work when the classes are inlined, any ideas?

@fabiosantoscode
Copy link
Collaborator

fabiosantoscode commented Nov 25, 2022

Try returning the class you want the function to return. You'll need to add the bare_returns parse option so Terser doesn't think the return outside a function is a syntax error.

@VandeurenGlenn
Copy link
Author

VandeurenGlenn commented Nov 25, 2022

Thanks, I missed the bare_returns option

edit: realized I don't need that option since I do a regex check for exported es and replace it with a return etc https://github.com/ArteonToken/monorepo/blob/main/scripts/create-genesis.js#L13
So just made it so it returns the (desired) class now.
I couldn't seem to figure out the return before (while experimenting in devconsole)
so after you said try to return I tried again and relalized a newline (\n) at the end of the code (for the rest no newlines) was causing the problem.

Thanks for the time!

edit2: so, realised something else 😅 shouldn't Terser remove that newline?

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

No branches or pull requests

2 participants