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
fix: push newClass
only when class is decorated
#14334
Conversation
@@ -1,8 +1,4 @@ | |||
function dec(fn, context) { |
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.
The original test case is renamed to *-with-initializers/exec.js
. They are replaced by a simpler test without initializers, which exactly catches this bug.
Build successful! You can test your changes in the REPL here: https://babeljs.io/repl/build/51410/ |
@@ -699,23 +696,19 @@ export default function applyDecs(targetClass, memberDecs, classDecs) { | |||
var ret = []; | |||
var staticMetadataMap = {}; | |||
|
|||
if (memberDecs) { |
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.
The guard is removed because the transformer always provides an array literal for memberDecs
.
pushInitializers(ret, staticInitializers); | ||
} | ||
pushInitializers(ret, protoInitializers); | ||
pushInitializers(ret, staticInitializers); |
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.
A dub initializer should be pushed when the member decorators do not introduce initializers, which can only be known at runtime.
|
||
if (classDecs) { |
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.
Same for classDecs
.
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.
cc @pzuraq if you have time to review!
newClass
only when class is decorated
I'm sorry, after some test, It's still have problem, Maybe this plugin need more testcase. This merge fixed my comment on #14242 , But class decorator and method decorator Cannot exist at the same time. function logged(value, { kind, name }) {
if (kind === 'method') {
return function () {}
}
return value
}
```javascript
@logged
class C {
@logged m(arg) {}
} TypeError: _initClass is not a function |
@wvq Turns out the |
We should also make CI fail when that happens. The tests aren't failing because |
:( still have problem with the generated source.
so, with a method decorator, It may compile to: [_initProto, _Todo, _initClass] = _applyDecs(this, [[log, 2, 'hello']], [log]) But there always have two function, It need to be: [_initProto, _initStatic, _Todo, _initClass] = _applyDecs(this, [[log, 2, "hello"]], [log]); Why not use an object with clear keys (or a fixed size array) instead of dynamic arrays, |
@wvq I believe it has been fixed in #14335, could you try that merge instead?
Array destructuring is indeed harder to maintain than object keys, however, it yields to smaller code after minification, since object keys is harder to mangle properly and thus not enabled by default in uglifyJS/terser. |
Before this PR we always push
newClass
to the return ofapplyDecs
but did not provide such bindings at the transformer when the class is not decorated. This PR resolves this issue.