[decorators] Support async and generator methods (#8742)

This commit is contained in:
Nicolò Ribaudo 2018-09-25 10:03:03 +02:00 committed by GitHub
parent a19b72e46d
commit af694ebae1
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
4 changed files with 55 additions and 3 deletions

View File

@ -7,8 +7,11 @@ function prop(key, value) {
return t.objectProperty(t.identifier(key), value); return t.objectProperty(t.identifier(key), value);
} }
function value(body, params = []) { function value(body, params = [], async, generator) {
return t.objectMethod("method", t.identifier("value"), params, body); const method = t.objectMethod("method", t.identifier("value"), params, body);
method.async = !!async;
method.generator = !!generator;
return method;
} }
function hasDecorators({ node }) { function hasDecorators({ node }) {
@ -77,7 +80,7 @@ function getSingleElementDefinition(path, superRef, classRef, file) {
prop("static", node.static && t.booleanLiteral(true)), prop("static", node.static && t.booleanLiteral(true)),
prop("key", getKey(node)), prop("key", getKey(node)),
isMethod isMethod
? value(node.body, node.params) ? value(node.body, node.params, node.async, node.generator)
: node.value : node.value
? value(template.ast`{ return ${node.value} }`) ? value(template.ast`{ return ${node.value} }`)
: prop("value", scope.buildUndefinedNode()), : prop("value", scope.buildUndefinedNode()),

View File

@ -0,0 +1,8 @@
@decorator
class Foo {
async f1() {}
*f2() {}
async *f3() {}
}

View File

@ -0,0 +1,8 @@
{
"plugins": [
["proposal-decorators", { "decoratorsBeforeExport": false }],
"proposal-class-properties",
"syntax-async-generators",
["external-helpers", { "helperVersion": "7.0.2" }]
]
}

View File

@ -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() {}
}]
};
});