Unnecessary rename of class inside function with same name
- Dominant language
- Go
- Stars
- 40.1k
- Forks
- 1.3k
- PR merge metrics
- No merged PRs in 30d
Description
I'm using esbuild to transpile ESM generated by TypeScript into CJS. The actual code is a bit more complex, but I was able to reduce it to a minimal case:
https://esbuild.github.io/try/#dAAwLjI0LjAAAGxldCBNeUNsYXNzID0gKCgpID0+IHsKICAgIHZhciBNeUNsYXNzID0gY2xhc3Mge30KICAgIHJldHVybiBNeUNsYXNzOwp9KSgpOwoKY29uc29sZS5sb2coTXlDbGFzcy5uYW1lKTs
```js
let MyClass = (() => {
var MyClass = class {}
return MyClass;
})();
console.log(MyClass.name);
// prints "MyClass"
```
When transformed, this code becomes - note that the behavior has changed:
```js
let MyClass = /* @__PURE__ */ (() => {
var MyClass2 = class {
};
return MyClass2;
})();
console.log(MyClass.name);
// prints "MyClass2"
```
I'm aware that the `keepNames` option exists, but I would argue that it should not be necessary in this case. `var MyClass` only exists in the function scope and there is no other identifier with the same name in there.
As a result, the `var` should not be renamed and esbuild should instead generate the following code:
```js
let MyClass = /* @__PURE__ */ (() => {
var MyClass = class {
};
return MyClass;
})();
console.log(MyClass.name);
// prints "MyClass"
```
Contributor guide
No contributing guide indexed for this repository
Research direction
Start with the linked esbuild playground reproduction and compare the transformed output with the reported expected output. Trace the name-renaming behavior for the inner class and verify completion by confirming that the generated code preserves the class name and runtime value of MyClass.name.
Written by the indexing model from the issue text.
Assessment
- Tech stack
- javascript
- Domain
- compilers
- Issue type
- Bug
- Difficulty
- 4/5
- Estimated time
- 3-5 days
- Activity status
- Stale
- Clarity
- Clearly specified
- Newbie friendliness
- 35/100