Missed minify optimization opportunity for self-assignement
- Dominant language
- Go
- Stars
- 40.1k
- Forks
- 1.3k
- PR merge metrics
- No merged PRs in 30d
Description
Options: `--tree-shaking=true --minify`
Input:
```js
function $(a) {
return a;
}
let x = 3;
x = $(x);
```
Output:
```js
let x=3;x=x;
```
[⏯️ Try on esbuild](https://esbuild.github.io/try/#YgAwLjE4LjEzAC0tdHJlZS1zaGFraW5nPXRydWUgLS1taW5pZnkAZQBlbnRyeS5qcwBmdW5jdGlvbiAkKGEpIHsKIHJldHVybiBhOwp9CgpsZXQgeCA9IDM7CnggPSAkKHgpOw)
The `x=x;` can be removed to give the following output:
```js
let x=3;
```
I understand that it might not be easy to implement at the moment, and that it might be very low on the list of optimizations to add to esbuild. Just signalling its existance 😄.
### Context
When using TypeScript's JSDoc type annotations in a JS project, I found myself needing TS's [Non-null Assertion Operator (Postfix `!`)](https://www.typescriptlang.org/docs/handbook/2/everyday-types.html#non-null-assertion-operator-postfix-), so I defined the `$()` function from the example that returns the passed value unchanged but with a different type.
Plus in some cases an `x = $(x)` can be used to let TS's type inference know that `x` is now non-nullable.
Contributor guide
No contributing guide indexed for this repository
Research direction
Reproduce the example with esbuild's `--tree-shaking=true --minify` options using the linked Try URL, then trace the minifier and tree-shaking handling for `x = $(x)`. Done means recognizing this identity assignment as removable in the shown case and producing `let x=3;` without changing observable behavior.
Written by the indexing model from the issue text.
Assessment
- Tech stack
- javascript, typescript
- Domain
- compilers
- Issue type
- Feature
- Difficulty
- 4/5
- Estimated time
- 3-5 days
- Activity status
- Stale
- Clarity
- Mostly clear
- Newbie friendliness
- 35/100