-
-
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’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Fixed refs to decorated class from itself methods (legacy decorators) #12007
base: main
Are you sure you want to change the base?
Conversation
Build successful! You can test your changes in the REPL here: https://babeljs.io/repl/build/27921/ |
This pull request is automatically built and testable in CodeSandbox. To see build info of the built libraries, click here or the icon next to each commit SHA. Latest deployment of this branch, based on commit 77ac0ca:
|
} | ||
|
||
method() { | ||
return Parent |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Can you add test cases on property
? i.e.
@dec
@dec2
class Parent {
property = Parent,
}
What will p.property
be? Same for private properties.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Solved by inserting in all methods (including static, #private ones and constructor) let ClassName = _someGeneratedVariable, where _someGeneratedVariable hold "final" class, after all decorators applied.
References in class properties still isn't supported.
Property isn't working currently (will hold undecorated class). For full solution we should replace all class references in read positions of class body to outer reference, but I ain't good enough at babel for such tricks.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Hi! Catch issue with property yesterday, similar to @JLHwung case, but with static property)
example source:
function dec(cls) {
return cls
}
@dec
class Decorated {
static property = Decorated
}
and output:
"use strict";
var _class, _class2, _temp;
function dec(cls) {
return cls;
}
let Decorated = dec(_class = (_temp = _class2 = class Decorated {}, _class2.property = Decorated, _temp)) || _class;
This code produce runtime error ReferenceError: Cannot access 'Decorated' before initialization
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@Amareis about class properties, fix references require changes in @babel/plugin-proposal-class-properties
plugin?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@Amareis about class properties, fix references require changes in
@babel/plugin-proposal-class-properties
plugin?
Don't think so. If we get full solution with replacing of original class reference in class body, this will be working without any additional efforts.
@nicolo-ribaudo what's missing so this PR gets merged? just curious |
Would love to see this merged :) |
Let's get this merged |
I was going to rebase and finish this, but I'm not sure that we should do it. While I agree that the proposed behavior makes sense, the current behavior makes sense too: // input.js
@dec class A { method() { A } }
// current.js
let A = dec(class A { method() { A } });
// proposed.js
let A = dec(class { method() { A } }); However, there is a big reason to keep the current behavior: changing it is not clearly a bugfix, and the code written in the past ~6 years relying on the current behavior risks to stop working. I strongly encourage you to migrate to the current stage 3 version of the decorators proposal (https://github.com/tc39/proposal-decorators/), passing |
I'd say one good reason could be to be able to use babel to transpile typescript without a compatibility issue since this plugin is often used to do so. If the old behavior wants to be kept (understandable), then at least it could be offered as a "legacy-ts" mode or similar. Using the new stage 3 proposal would not fix the typescript compatibility issue. |
TypeScript plans to support standard decorators in the next version: microsoft/TypeScript#49074 |
Problem described in #11131
Solved by inserting in all methods (including static, #private ones and constructor)
let ClassName = _someGeneratedVariable
, where_someGeneratedVariable
hold "final" class, after all decorators applied.References in class properties still isn't supported.