Skip to content
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

Unnecessary rename of class inside function with same name #3983

Open
AlCalzone opened this issue Nov 20, 2024 · 0 comments
Open

Unnecessary rename of class inside function with same name #3983

AlCalzone opened this issue Nov 20, 2024 · 0 comments

Comments

@AlCalzone
Copy link

AlCalzone commented Nov 20, 2024

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

let MyClass = (() => {
    var MyClass = class {}
    return MyClass;
})();

console.log(MyClass.name);
// prints "MyClass"

When transformed, this code becomes - note that the behavior has changed:

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:

let MyClass = /* @__PURE__ */ (() => {
  var MyClass = class {
  };
  return MyClass;
})();
console.log(MyClass.name);
// prints "MyClass"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant