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

[decorators] Support async and generator methods #8742

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
9 changes: 6 additions & 3 deletions packages/babel-plugin-proposal-decorators/src/transformer.js
Expand Up @@ -7,8 +7,11 @@ function prop(key, value) {
return t.objectProperty(t.identifier(key), value);
}

function value(body, params = []) {
return t.objectMethod("method", t.identifier("value"), params, body);
function value(body, params = [], async, generator) {
const method = t.objectMethod("method", t.identifier("value"), params, body);
Copy link
Member

@hzoo hzoo Sep 24, 2018

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I guess this is because we don't require you do to async/generator in t.X? A weird bug just since we don't pass it down but not sure what we would want to do in general for it

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

t.functionDeclaration accepts async/generator but t.objectMethod doesn't.
I'd love to have a function to specify the fields I want, like

t.objectMethod.with({
  kind: "method",
  key: t.identifier("value"),
  params, body, async, generator
});

method.async = !!async;
method.generator = !!generator;
return method;
}

function hasDecorators({ node }) {
Expand Down Expand Up @@ -77,7 +80,7 @@ function getSingleElementDefinition(path, superRef, classRef, file) {
prop("static", node.static && t.booleanLiteral(true)),
prop("key", getKey(node)),
isMethod
? value(node.body, node.params)
? value(node.body, node.params, node.async, node.generator)
: node.value
? value(template.ast`{ return ${node.value} }`)
: prop("value", scope.buildUndefinedNode()),
Expand Down
@@ -0,0 +1,8 @@
@decorator
class Foo {
async f1() {}

*f2() {}

async *f3() {}
}
@@ -0,0 +1,8 @@
{
"plugins": [
["proposal-decorators", { "decoratorsBeforeExport": false }],
"proposal-class-properties",
"syntax-async-generators",
["external-helpers", { "helperVersion": "7.0.2" }]
]
}
@@ -0,0 +1,33 @@
let Foo = babelHelpers.decorate([decorator], function (_initialize) {
"use strict";

class Foo {
constructor() {
_initialize(this);
}

}

return {
F: Foo,
d: [{
kind: "method",
key: "f1",

async value() {}

}, {
kind: "method",
key: "f2",

*value() {}

}, {
kind: "method",
key: "f3",

async *value() {}

}]
};
});