Private Name in the left ForOfStatement is not transformed
See original GitHub issueBug Report
- I would like to work on a fix!
Current Behavior The following snippet, when transformed via preset-env and stage-3 preset, will throw
Property left of ForOfStatement expected node to be of a type ["VariableDeclaration","LVal"] but instead got "CallExpression"
Input Code
class C {
#p;
m() {
for (this.#p of []);
}
}
Expected behavior/code It should be transformed into something like
// helpers is omissioned
var C = /*#__PURE__*/function () {
function C() {
_classCallCheck(this, C);
_p.set(this, {
writable: true,
value: void 0
});
}
_createClass(C, [{
key: "m",
value: function m() {
for (var _i = 0, _arr = []; _i < _arr.length; _i++) {
i = _arr[_i];
_classPrivateFieldSet(this, _p, i);
};
}
}]);
return C;
}();
Possible Solution
As Justin noted, we can fix it by detecting such patterns in @babel/helper-member-expression-to-functions
, so babel will apply destructureSet
private name handler, which will return a member expression – a valid LHS.
If you don’t know how to clone Babel, follow these steps: (you need to have make
and yarn
available on your machine).
- Write a comment there to let other possible contributors know that you are working on this bug.
- Fork the repo
- Run
git clone https://github.com/<YOUR_USERNAME>/babel.git && cd babel
- Run
yarn && make bootstrap
- Wait ⏳
- Run
make watch
- Add a test (only
input.js
;output.js
will be automatically generated) - Update the code!
yarn jest babel-plugin-proposal-class-properties
to run the tests- If some test outputs don’t match but the new results are correct, you can delete the bad
output.js
files and run the tests again
- If some test outputs don’t match but the new results are correct, you can delete the bad
- If it is working, run
make test
to run all the tests - Run
git push
and open a PR!
Additional context/Screenshots
This issue is likely due to plugin ordering: the private-class-properties
runs before for-of-transforms
after this.#p
is transformed to helper calls which is not a valid LHS, the for-of transforms throws on unsupported for-of left.
Issue Analytics
- State:
- Created 4 years ago
- Reactions:2
- Comments:13 (12 by maintainers)
Top GitHub Comments
No problem! This isn’t to make you feel guilty, I just want to avoid situation where you’re stuck and haven’t asked for help. Take the time you need, especially with all circumstances going on.
Looks like we just need to use the
destructureSet
transform when inside of theinit
. This could be a GFI.