test(transformer/async-to-generator): failing test for async arrow function in class static block#8387
Conversation
How to use the Graphite Merge QueueAdd either label to this PR to merge it via the merge queue:
You must have a Graphite account in order to use the merge queue. Sign up using this link. An organization admin has enabled the Graphite Merge Queue in this repository. Please do not merge from GitHub as this will restart CI on PRs being processed by the merge queue. This stack of pull requests is managed by Graphite. Learn more about stacking. |
|
Just found that Babel's output is incorrect, |
|
Even if the async arrow function is not in the class static block, it also needs to transform But there is a troublesome example Input: class C { static f = async () => super.prop; }Babel's Output: var _superprop_getProp = () => super.prop;
class C {
static f = babelHelpers.asyncToGenerator(function* () {
return _superprop_getProp();
});
}Babel's implementation is wrong because it inserts |
46d2acc to
5a81005
Compare
Oh bollocks sorry my fault. I posted the Babel REPL link meaning to say "look Babel is wrong again!" but I forgot to actually write that. The test in this PR shows what I think the correct output is for static blocks. For static blocks, I don't expect it's too hard because there's already a block for the |
|
For properties (instance prop or static prop), it's much more annoying. Babel's solution for instance props looks correct, but for some reason it gets it wrong for static props: Babel REPL But I'm not sure we should attempt to cover async arrow functions in class properties. It's maybe not worth the work. I only suggested that we might want to deal with static blocks because I thought it'd be quite easy. We also have this problem with other transforms. e.g. We have other weird cases where we need to create temp vars as part of a transform inside:
I put class property initializers in the same category. Rather than tackling this in individual transforms, I think we should probably try to figure out a way to handle these problems universally with helpers that create temp vars, and handle these edge cases for us. |
|
It may also be easier to deal with when we have our own helper library. e.g. something like: class C {
static f = async (x, y) => super.prop;
}-> class C {
static f = oxcHelpers.asyncToGenerator(
function*(x, y, _superprop_getProp) {
return _superprop_getProp();
},
() => super.prop
);
} |
I just came up with a way by var _superprop_getProp;
class C {
static f =
((_superprop_getProp = () => super.prop),
/*#__PURE__*/ (function () {
var _ref = babelHelpers.asyncToGenerator(function* (x, y) {
return _superprop_getProp();
});
return function (_x, _x2) {
return _ref.apply(this, arguments);
};
})());
}It doesn't rely on our own helper, so we can support it soon. |
|
Nice! That does work for static props, but it won't work for instance props because you need a different Developing your idea further, I think this works for both static and instance: Input: class C {
f = async (x, y) => [this, super.prop];
static f = async (x, y) => [this, super.prop];
}Output: class C {
f = (function(_this, _superprop_getProp) {
var _ref = babelHelpers.asyncToGenerator(function*(x, y) {
return [_this, _superprop_getProp()];
});
return function (_x, _x2) {
return _ref.apply(this, arguments);
};
})(this, () => super.prop);
static f = (function(_this2, _superprop_getProp2) {
var _ref = babelHelpers.asyncToGenerator(function*(x, y) {
return [_this2, _superprop_getProp2()];
});
return function (_x, _x2) {
return _ref.apply(this, arguments);
};
})(this, () => super.prop);
}BUT... Do we want to do this now? I think in the end we can do it better once we have our own helpers lib, so we will end up rewriting it anyway. And supporting async-to-generator transform without class properties transform is not required for our core purpose which is to support |
5a81005 to
953d1b5
Compare
|
I have tried to solve this problem at #8435, luckily it didn't take me a long time. |
Merge activity
|
953d1b5 to
9b60f5f
Compare
…nction in class static block (#8387) It's arguable whether we should support this, because async functions is ES2018 and class static blocks is ES2022. So there should not be any browser which needs async-to-generator transform, and not the static block transform too. And when class-static-block transform is enabled, this works fine. But personally, I think we should support it, because: 1. We allow user to enable/disable arbitrary transforms via `TransformOptions`. 2. We support `this` in async arrow functions in class static blocks, so it makes sense to complete that support. 3. I don't think it should be too hard, as the framework for it is already there in the async-to-generator transform. [Babel REPL](https://babeljs.io/repl#?browsers=defaults%2C%20not%20ie%2011%2C%20not%20ie_mob%2011&build=&builtIns=false&corejs=3.21&spec=false&loose=false&code_lz=MYGwhgzhAEDC0FMAeAXBA7AJjAytA3gFDTQQpgoCWwBxJ0KAFpRAHQBm60AvNJAJ7oaACgCUPAHykArgAcEAJw4B7ZQG46AX0LagA&debug=false&forceAllTransforms=false&modules=false&shippedProposals=false&evaluate=false&fileSize=false&timeTravel=false&sourceType=module&lineWrap=true&presets=&prettier=true&targets=&version=7.26.4&externalPlugins=%40babel%2Fplugin-transform-async-to-generator%407.25.9%2C%40babel%2Fplugin-external-helpers%407.25.9&assumptions=%7B%7D)
9b60f5f to
0358c6f
Compare
…ectly in async arrow function (#8435) close: #8385 This PR is to solve the missing `super` transform in the async arrow function, and `_this = this` inserts to an incorrect place. These problems are all about the async arrow function, which is a part of the init of class property. Learn more at #8387. The output matches Babel's output except for static prop as Babel transforms incorrectly.

It's arguable whether we should support this, because async functions is ES2018 and class static blocks is ES2022. So there should not be any browser which needs async-to-generator transform, and not the static block transform too. And when class-static-block transform is enabled, this works fine.
But personally, I think we should support it, because:
TransformOptions.thisin async arrow functions in class static blocks, so it makes sense to complete that support.Babel REPL