Skip to content

Decorator as static class member fails with runtime error #3326

Closed

Description

@danielrentz

After upgrading Vite from 4.3.9 (esbuild 0.17.19) to Vite 4.4.9 (esbuild 0.18.20), decorators that are static class members stop working when the decorated static method instantiates the class.

In this case, the class will be created as temporary variable (e.g. _Test), and will be decorated using the original class name (Test) before that variable will be defined (let Test = _Test).

Original test code (Test1 works as expected, Test2 fails with esbuild 0.18):

export class Test1 {
    static deco(): void { }
    @Test1.deco static test(): void { }
}
export class Test2 {
    static deco(): void { }
    @Test2.deco static test(): Test2 { return new Test2(); }
}

esbuild 0.17.19

export class Test1 {
    static deco() {}
    static test() {}
}
__decorateClass([Test1.deco], Test1, "test", 1);
const _Test2 = class {
    static deco() {}
    static test() {
        return new _Test2();
    }
}
;
export let Test2 = _Test2;
__decorateClass([Test2.deco], Test2, "test", 1); // <============ works

esbuild 0.18.20

export class Test1 {
    static deco() {}
    static test() {}
}
__decorateClass([Test1.deco], Test1, "test", 1);
const _Test2 = class _Test2 {
    static deco() {}
    static test() {
        return new _Test2();
    }
}
;
__decorateClass([Test2.deco], _Test2, "test", 1); // <============= uses "Test2" instead of "_Test2"
export let Test2 = _Test2;

Importing the module immediately fails with

ReferenceError: Cannot access 'Test2' before initialization

Activity

  1. dlmartens commented on Aug 30, 2023

    @dlmartens

    I am having this issue as well. It looks like it occurs in any class with static members (which cause an underscore prefix rename on the class). It seems the problem is just that the renamed class is not used in the call to __decorateClass.

    Here is a basic example.

  2. viceice commented on Sep 7, 2023

    @viceice

    Seeing same error while trying to transition from jest (ts-jest) to vitest. For us esbuild v0.17 and v0.19 are failing too. 😕

  3. added a commit that references this issue on Jan 27, 2024
    0d9f765
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions