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

Decorator as static class member fails with runtime error #3326

Closed
danielrentz opened this issue Aug 21, 2023 · 2 comments
Closed

Decorator as static class member fails with runtime error #3326

danielrentz opened this issue Aug 21, 2023 · 2 comments

Comments

@danielrentz
Copy link

danielrentz commented Aug 21, 2023

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
@dlmartens
Copy link

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.

@viceice
Copy link

viceice commented Sep 7, 2023

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

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

3 participants