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

keepNames with static class fields and initialization blocks #2389

Closed
esdmr opened this issue Jul 14, 2022 · 0 comments
Closed

keepNames with static class fields and initialization blocks #2389

esdmr opened this issue Jul 14, 2022 · 0 comments
Labels

Comments

@esdmr
Copy link

esdmr commented Jul 14, 2022

Static fields and initialization blocks in classes run before the name of the class is set. If the static part is referencing the name of the class, it fails in certain cases.

  • If I do not specify either bundle or format, esbuild does not rename the classes, and therefore the bug does not occur.
  • If I specify bundle, esbuild sets the static class fields after the name, but keeps the static initialization blocks in the class.
  • If I only specify format, esbuild keeps both static parts in the class just before setting the name.
class DirectlyReferenced {
    static type = DirectlyReferenced.name;
}

console.assert(DirectlyReferenced.type === 'DirectlyReferenced', 1);

class ReferencedViaThis {
    static type = this.name;
}

console.assert(ReferencedViaThis.type === 'ReferencedViaThis', 2);

class StaticBlock {
    static {
        console.assert(this.name === 'StaticBlock', 3);

        // StaticBlock is undefined here with --bundle.
        console.assert(StaticBlock?.name === 'StaticBlock', 4);
    }
}

I suggest moving the function call that sets the name like so:

class Data {
    static {
        setName(Data, 'Data');
    }

    static type = this.name;

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

No branches or pull requests

2 participants