-
-
Notifications
You must be signed in to change notification settings - Fork 5.6k
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鈥檒l occasionally send you account related emails.
Already on GitHub? Sign in to your account
[Bug]: Exception when combining a decorated class, a decorated method, and a static field #16373
Comments
Hey @SirPepe! We really appreciate you taking the time to report an issue. The collaborators on this project attempt to help as many people as possible, but we're a limited number of volunteers, so it's possible this won't be addressed swiftly. If you need any help, or just have general Babel or JavaScript questions, we have a vibrant Slack community that typically always has someone willing to help. You can sign-up here for an invite. |
Mhh, I cannot reproduce this on the repl 馃 If I copy the generated code and run it, it doesn't throw. Could you share a runnable example? |
I stripped almost everything from my project: https://github.com/SirPepe/babel-decorators-repro Running
|
As a workaround, please set the typescript preset option This is essentially a plugin ordering issue: the typescript transform currently runs after the decorator transform, so the |
Thank you, this works for this particular problem! But const key = Symbol();
function decorateClass(target: any, _: any): any {
return class extends target {
[key]!: any;
};
}
function decorateMethod(_: any, context: any) {
context.addInitializer(function (this: any) {
this[key] ??= 1;
});
}
@decorateClass
export class Test {
@decorateMethod
something() {}
}
console.log((new Test() as any)[key]); The following happens when this runs:
The idea behind my code is to have a set of decorators, where any of the decorator's initializers may lazily set up something on the class instance if and when that setup is needed. I guess the next workaround would be to remove the line |
In retrospect we should have chosen a better name for So when
The property defined in the derived class shadows the same property defined on the base class by the initializer of the
The property is not defined in the derived class, so the |
馃捇
How are you using Babel?
@rollup/plugin-babel
Input code
Configuration file name
babel.config.mjs
Configuration
Current and expected behavior
Current behavior: The code above results in
TypeError: _initClass is not a function
. This happens in my current setup when I combine a decorated class, a decorated method, and a static field. Removal of any of the aforementioned elements prevents the error from occurring.Expected behavior: no exceptions.
Environment
System:
OS: Linux 6.5 Ubuntu 22.04.4 LTS 22.04.4 LTS (Jammy Jellyfish)
Binaries:
Node: 20.11.0 - ~/.volta/tools/image/node/20.11.0/bin/node
npm: 10.2.4 - ~/.volta/tools/image/node/20.11.0/bin/npm
npmPackages:
@babel/cli: ^7.22.5 => 7.24.1
@babel/core: ^7.22.1 => 7.24.3
@babel/plugin-proposal-decorators: ^7.22.3 => 7.24.1
@babel/preset-env: ^7.22.4 => 7.24.3
@babel/preset-typescript: ^7.21.5 => 7.24.1
eslint: ^8.41.0 => 8.57.0
Possible solution
No response
Additional context
No response
The text was updated successfully, but these errors were encountered: