evanw / evanw/esbuild

Variable assignment could not be repeated

Open
#3,906 0 comments 0 reactions 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

Think I looked through existing issues, sorry if I missed this, and thanks for esbuild :) When a variable is set in a bunch of if statements, with nothing else, would it be possible for the assignment to be done once rather than in each conditional? Here's a cut down example:

```
$ cat foo.js
if (code === 1) {
link = "1";
} else if (code === 2) {
link = "2";
} else if (code === 3) {
link = "3";
} else {
link = "0";
}

$ cat foo.js | node_modules/.bin/esbuild --minify
code===1?link="1":code===2?link="2":code===3?link="3":link="0";
```

I think this is equivalent to the shorter `link=code===1?"1":code===2?"2":code===3?"3":"0";` - every part sets link, so it can be extracted and used once at the start. Hope that's of interest, though realise might not be possible/easy/worth it

Contributor guide

No contributing guide indexed for this repository

Assessment

This issue has not been assessed yet.

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.