evanw / evanw/esbuild

Classes with constant computed property names aren't three-shaken

Open
#3,940 0 comments 1 reaction 0 assignees View on GitHub
suboptimal-output
Dominant language
Go
Stars
40.1k
Forks
1.3k
PR merge metrics
No merged PRs in 30d

Description

The following code is not being tree shaken awy:

```
const prop = "prop";

class Money {
[prop] = true;

constructor() {
}
}
```

`.bin/esbuild test.ts --minify --bundle --format=esm --target=esnext` returns: `var c="prop",r=class{[c]=!0;constructor(){}};` - I'd expect nothing

Setting the prop to a Symbol() or Symbol.for(...) (which is what I was actually trying to do) doesn't work either

If I replace prop with a constant it does work:

```
class Money {
["prop"] = true;

constructor() {
}
}
```

(no output)

Contributor guide

No contributing guide indexed for this repository

Research direction

Reproduce the issue with the provided test.ts input and the .bin/esbuild command, then trace the tree-shaking behavior for classes with computed property names. Done means unused classes are removed when the property name is a constant or computed from a constant, including the reported Symbol cases, without regressing the literal-property case.

Written by the indexing model from the issue text.

Assessment

Tech stack
javascript
Domain
compilers
Issue type
Bug
Difficulty
3/5
Estimated time
1-2 days
Activity status
Stale
Clarity
Mostly clear
Newbie friendliness
45/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.