Transform private async and generator functions (#9423)

This commit is contained in:
Nicolò Ribaudo 2019-02-04 15:10:46 +01:00 committed by GitHub
parent 9eb010da50
commit d37c958637
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
17 changed files with 206 additions and 2 deletions

View File

@ -410,8 +410,14 @@ function buildPrivateInstanceMethodDeclaration(prop, privateNamesMap) {
getterDeclared,
setterDeclared,
} = privateName;
const { params, body } = prop.node;
const methodValue = t.functionExpression(methodId, params, body);
const { params, body, generator, async } = prop.node;
const methodValue = t.functionExpression(
methodId,
params,
body,
generator,
async,
);
const isGetter = getId && !getterDeclared && params.length === 0;
const isSetter = setId && !setterDeclared && params.length > 0;

View File

@ -0,0 +1,13 @@
class Cl {
async #foo() {
return 2;
}
test() {
return this.#foo();
}
}
return new Cl().test().then(val => {
expect(val).toBe(2);
});

View File

@ -0,0 +1,9 @@
class Cl {
async #foo() {
return 2;
}
test() {
return this.#foo();
}
}

View File

@ -0,0 +1,11 @@
{
"minNodeVersion": "8.0.0",
"plugins": [
["external-helpers", { "helperVersion": "7.1000.0" }],
["proposal-private-methods", { "loose": true }],
["proposal-class-properties", { "loose": true }]
],
"parserOpts": {
"allowReturnOutsideFunction": true
}
}

View File

@ -0,0 +1,18 @@
class Cl {
constructor() {
Object.defineProperty(this, _foo, {
value: _foo2
});
}
test() {
return babelHelpers.classPrivateFieldLooseBase(this, _foo)[_foo]();
}
}
var _foo = babelHelpers.classPrivateFieldLooseKey("foo");
var _foo2 = async function _foo2() {
return 2;
};

View File

@ -0,0 +1,14 @@
class Cl {
*#foo() {
yield 2;
return 3;
}
test() {
return this.#foo();
}
}
const val = new Cl().test();
expect(val.next()).toEqual({ value: 2, done: false });
expect(val.next()).toEqual({ value: 3, done: true });

View File

@ -0,0 +1,10 @@
class Cl {
*#foo() {
yield 2;
return 3;
}
test() {
return this.#foo();
}
}

View File

@ -0,0 +1,7 @@
{
"plugins": [
["external-helpers", { "helperVersion": "7.1000.0" }],
["proposal-private-methods", { "loose": true }],
["proposal-class-properties", { "loose": true }]
]
}

View File

@ -0,0 +1,19 @@
class Cl {
constructor() {
Object.defineProperty(this, _foo, {
value: _foo2
});
}
test() {
return babelHelpers.classPrivateFieldLooseBase(this, _foo)[_foo]();
}
}
var _foo = babelHelpers.classPrivateFieldLooseKey("foo");
var _foo2 = function* _foo2() {
yield 2;
return 3;
};

View File

@ -0,0 +1,13 @@
class Cl {
async #foo() {
return 2;
}
test() {
return this.#foo();
}
}
return new Cl().test().then(val => {
expect(val).toBe(2);
});

View File

@ -0,0 +1,9 @@
class Cl {
async #foo() {
return 2;
}
test() {
return this.#foo();
}
}

View File

@ -0,0 +1,11 @@
{
"minNodeVersion": "8.0.0",
"plugins": [
["external-helpers", { "helperVersion": "7.1000.0" }],
"proposal-private-methods",
"proposal-class-properties"
],
"parserOpts": {
"allowReturnOutsideFunction": true
}
}

View File

@ -0,0 +1,16 @@
class Cl {
constructor() {
_foo.add(this);
}
test() {
return babelHelpers.classPrivateMethodGet(this, _foo, _foo2).call(this);
}
}
var _foo = new WeakSet();
var _foo2 = async function _foo2() {
return 2;
};

View File

@ -0,0 +1,14 @@
class Cl {
*#foo() {
yield 2;
return 3;
}
test() {
return this.#foo();
}
}
const val = new Cl().test();
expect(val.next()).toEqual({ value: 2, done: false });
expect(val.next()).toEqual({ value: 3, done: true });

View File

@ -0,0 +1,10 @@
class Cl {
*#foo() {
yield 2;
return 3;
}
test() {
return this.#foo();
}
}

View File

@ -0,0 +1,7 @@
{
"plugins": [
["external-helpers", { "helperVersion": "7.1000.0" }],
"proposal-private-methods",
"proposal-class-properties"
]
}

View File

@ -0,0 +1,17 @@
class Cl {
constructor() {
_foo.add(this);
}
test() {
return babelHelpers.classPrivateMethodGet(this, _foo, _foo2).call(this);
}
}
var _foo = new WeakSet();
var _foo2 = function* _foo2() {
yield 2;
return 3;
};