evanw / esbuild

An extremely fast bundler for the web
https://esbuild.github.io/
MIT License
38.21k stars 1.15k forks source link

Class name in static field initializer collide with import, causing runtime error #3923

Open yume-chan opened 1 month ago

yume-chan commented 1 month ago

Input

a.js:

export class A {
  constructor() {
    console.log("success");
  }
}

entry.js:

import {A as B} from './a.js'

class A {
   static C = class A extends B {}
}

new A.C()

Playground: link

Expected

> node entry.js
success

Actual

(() => {
  // a.js
  var A = class {
    constructor() {
      console.log("success");
    }
  };

  // entry.js
  var A2 = class {
    static C = class A extends A {
    };
  };
  new A2.C();
})();
VM15:11 Uncaught ReferenceError: Cannot access 'A' before initialization
    at <static_initializer> (<anonymous>:11:32)
    at <anonymous>:10:12
    at <anonymous>:15:3