-
Notifications
You must be signed in to change notification settings - Fork 1.1k
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
DCE Idea: Unused static methods #1709
Comments
That comment still applies. In JavaScript, the class body of class Foo {
static bar() { console.log('bar') }
static qux() { console.log('qux') }
}
Foo.qux();
new Foo().constructor.bar(); To be able to safely remove Personally I think the reasonable approaches to this problem are to either a) just use plain functions instead of static methods, which are trivially analyzable or b) just use another programming language that doesn't allow you to access static methods indirectly like this in the first place, which also makes this trivially analyzable. Google Closure Compiler's approach of creating a custom type system on top of JavaScript with its own semantics, restrictions, and undefined behavior and then creating an optimizer for it is admirable, but extremely complex and unsafe, which puts it both out of scope and against esbuild's ideals, respectively. If you want this type of thing in your JavaScript code then I recommend that you just use Google Closure Compiler. The custom type system and automatic type inference features required to implement this feature safely is not a complexity threshold that I think esbuild should cross. |
Closing this issue as this is out of scope. |
Thank you for the highly detailed answer @evanw! Just in case others run into this issue, I'll include the workaround I've discovered. If the only intention of the class is to use it as a namespace for functions, then converting to a plain object provides a similar API surface that const Foo = {
bar: () => console.log('bar'),
qux: () => console.log('qux'),
}
Foo.bar(); I did find a couple of cases that unexpectedly deopt, so if using this workaround you'll need to be careful of those (self-referencing object, method shorthand, etc) |
summary
Given an input:
Is it possible to statically analyze the code and remove the definition of
Foo.bar
?Closure Compiler is able to reduce this to single
console.log
statement, whereas other bundlers likeesbuild
,rollup
, andparcel
retain the full definition of Foo.Note: I did find a thorough comment from earlier (#771 (comment)), but was wondering if the introduction of
static
changes feasabilityThe text was updated successfully, but these errors were encountered: