Transform private async and generator functions (#9423)
This commit is contained in:
parent
9eb010da50
commit
d37c958637
@ -410,8 +410,14 @@ function buildPrivateInstanceMethodDeclaration(prop, privateNamesMap) {
|
|||||||
getterDeclared,
|
getterDeclared,
|
||||||
setterDeclared,
|
setterDeclared,
|
||||||
} = privateName;
|
} = privateName;
|
||||||
const { params, body } = prop.node;
|
const { params, body, generator, async } = prop.node;
|
||||||
const methodValue = t.functionExpression(methodId, params, body);
|
const methodValue = t.functionExpression(
|
||||||
|
methodId,
|
||||||
|
params,
|
||||||
|
body,
|
||||||
|
generator,
|
||||||
|
async,
|
||||||
|
);
|
||||||
const isGetter = getId && !getterDeclared && params.length === 0;
|
const isGetter = getId && !getterDeclared && params.length === 0;
|
||||||
const isSetter = setId && !setterDeclared && params.length > 0;
|
const isSetter = setId && !setterDeclared && params.length > 0;
|
||||||
|
|
||||||
|
|||||||
13
packages/babel-plugin-proposal-private-methods/test/fixtures/private-method-loose/async/exec.js
vendored
Normal file
13
packages/babel-plugin-proposal-private-methods/test/fixtures/private-method-loose/async/exec.js
vendored
Normal 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);
|
||||||
|
});
|
||||||
@ -0,0 +1,9 @@
|
|||||||
|
class Cl {
|
||||||
|
async #foo() {
|
||||||
|
return 2;
|
||||||
|
}
|
||||||
|
|
||||||
|
test() {
|
||||||
|
return this.#foo();
|
||||||
|
}
|
||||||
|
}
|
||||||
@ -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
|
||||||
|
}
|
||||||
|
}
|
||||||
@ -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;
|
||||||
|
};
|
||||||
@ -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 });
|
||||||
@ -0,0 +1,10 @@
|
|||||||
|
class Cl {
|
||||||
|
*#foo() {
|
||||||
|
yield 2;
|
||||||
|
return 3;
|
||||||
|
}
|
||||||
|
|
||||||
|
test() {
|
||||||
|
return this.#foo();
|
||||||
|
}
|
||||||
|
}
|
||||||
@ -0,0 +1,7 @@
|
|||||||
|
{
|
||||||
|
"plugins": [
|
||||||
|
["external-helpers", { "helperVersion": "7.1000.0" }],
|
||||||
|
["proposal-private-methods", { "loose": true }],
|
||||||
|
["proposal-class-properties", { "loose": true }]
|
||||||
|
]
|
||||||
|
}
|
||||||
@ -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;
|
||||||
|
};
|
||||||
13
packages/babel-plugin-proposal-private-methods/test/fixtures/private-method/async/exec.js
vendored
Normal file
13
packages/babel-plugin-proposal-private-methods/test/fixtures/private-method/async/exec.js
vendored
Normal 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);
|
||||||
|
});
|
||||||
9
packages/babel-plugin-proposal-private-methods/test/fixtures/private-method/async/input.js
vendored
Normal file
9
packages/babel-plugin-proposal-private-methods/test/fixtures/private-method/async/input.js
vendored
Normal file
@ -0,0 +1,9 @@
|
|||||||
|
class Cl {
|
||||||
|
async #foo() {
|
||||||
|
return 2;
|
||||||
|
}
|
||||||
|
|
||||||
|
test() {
|
||||||
|
return this.#foo();
|
||||||
|
}
|
||||||
|
}
|
||||||
11
packages/babel-plugin-proposal-private-methods/test/fixtures/private-method/async/options.json
vendored
Normal file
11
packages/babel-plugin-proposal-private-methods/test/fixtures/private-method/async/options.json
vendored
Normal 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
|
||||||
|
}
|
||||||
|
}
|
||||||
16
packages/babel-plugin-proposal-private-methods/test/fixtures/private-method/async/output.js
vendored
Normal file
16
packages/babel-plugin-proposal-private-methods/test/fixtures/private-method/async/output.js
vendored
Normal 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;
|
||||||
|
};
|
||||||
14
packages/babel-plugin-proposal-private-methods/test/fixtures/private-method/generator/exec.js
vendored
Normal file
14
packages/babel-plugin-proposal-private-methods/test/fixtures/private-method/generator/exec.js
vendored
Normal 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 });
|
||||||
10
packages/babel-plugin-proposal-private-methods/test/fixtures/private-method/generator/input.js
vendored
Normal file
10
packages/babel-plugin-proposal-private-methods/test/fixtures/private-method/generator/input.js
vendored
Normal file
@ -0,0 +1,10 @@
|
|||||||
|
class Cl {
|
||||||
|
*#foo() {
|
||||||
|
yield 2;
|
||||||
|
return 3;
|
||||||
|
}
|
||||||
|
|
||||||
|
test() {
|
||||||
|
return this.#foo();
|
||||||
|
}
|
||||||
|
}
|
||||||
@ -0,0 +1,7 @@
|
|||||||
|
{
|
||||||
|
"plugins": [
|
||||||
|
["external-helpers", { "helperVersion": "7.1000.0" }],
|
||||||
|
"proposal-private-methods",
|
||||||
|
"proposal-class-properties"
|
||||||
|
]
|
||||||
|
}
|
||||||
17
packages/babel-plugin-proposal-private-methods/test/fixtures/private-method/generator/output.js
vendored
Normal file
17
packages/babel-plugin-proposal-private-methods/test/fixtures/private-method/generator/output.js
vendored
Normal 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;
|
||||||
|
};
|
||||||
Loading…
x
Reference in New Issue
Block a user