fix:added check for forXstatement pattern#11703
Conversation
|
This pull request is automatically built and testable in CodeSandbox. To see build info of the built libraries, click here or the icon next to each commit SHA. Latest deployment of this branch, based on commit d07e916:
|
|
Build successful! You can test your changes in the REPL here: https://babeljs.io/repl/build/23819/ |
|
In the example it should be |
| // [MEMBER = _VALUE] = ARR -> [_destructureSet(MEMBER) = _VALUE] = ARR | ||
| // [...MEMBER] -> [..._destructureSet(MEMBER)] | ||
| if ( | ||
| parentPath.isForXStatement() || |
There was a problem hiding this comment.
We should also check if node is the left of the ForXStatement. Consider this case
for (const el of this.#arr);this.#x should not be transformed into _destructureSet(this, x).
There was a problem hiding this comment.
alright, just to clarify its 1st checking if its a for-of statement then checking if the next node is left of the for-of ?
| // [...MEMBER] -> [..._destructureSet(MEMBER)] | ||
| if ( | ||
| (parentPath.isForXStatement() && | ||
| parentPath.parentPath.isAssignmentPattern({ left: node })) || |
There was a problem hiding this comment.
If a path is a ForXStatement, how can its parentPath be an AssignmentPattern? Note that the children of an Pattern is always an expression. A statement can not be an expression.
Here is an AST example of for (o.p of arr);
// for (o.p of arr);
{
"type": "ForOfStatement",
"await": false,
"left": {
"type": "MemberExpression",
"object": { "type": "Identifier", "name": "o" },
"property": { "type": "Identifier", "name": "p" }
},
"right": { "type": "Identifier", "name": "arr" }
}The parentPath here refers to the NodePath of ForOfStatement. NodePath is a wrapper of the AST node that provides parent association with other NodePath. In this PR We would like to make sure that member, which refers to MemberExpression here, is indeed the left of the underlying AST node of parentPath.
So we can use
parentPath.isForXStatement() && parentPath.node.left === nodeThe query function isForXStatement accepts an additional parameter as query, so the code above can be simplified as
parentPath.isForXStatement({ left: node })There was a problem hiding this comment.
ok, when I run the test I seem to get the same results, which shouldn't happen?
input
class D {
#arr;
f() {
for (const el of this.#arr);
}
}
output
var _arr = new WeakMap();
var D = /*#__PURE__*/function () {
"use strict";
function D() {
babelHelpers.classCallCheck(this, D);
_arr.set(this, {
writable: true,
value: void 0
});
}
babelHelpers.createClass(D, [{
key: "f",
value: function f() {
for (var el of babelHelpers.classPrivateFieldGet(this, _arr)) {
;
}
}
}]);
return D;
}();
There was a problem hiding this comment.
The output is expected. Can you add a test case from the original issue?
class D {
#arr;
f() {
for (const this.#arr of [1, 2]);
}
}| class D { | ||
| #arr; | ||
| f() { | ||
| for (const el of this.#arr); |
There was a problem hiding this comment.
These should be reversed.
Please also add a test for for-in.
There was a problem hiding this comment.
It is a regression check on the first draft implementation: https://github.com/babel/babel/pull/11703/files/d4c0a5e8b75306f918cf2a2307e4f65130f9ea05#diff-decaf745088978f783d453709a8a8479
There was a problem hiding this comment.
Ooo, ok. So we need 2 new test cases (for-of and for-in) with it appearing in the left.
There was a problem hiding this comment.
👍 can just do it in the next line, or leave a comment that it shouldn't be transformed
There was a problem hiding this comment.
personally I think just doing them all in the same class is fine
for (const el of this.#arr);
for (this.#arr of [1, 2]);
for (this.#arr in [1,2,3]);the empty arr seems to be the same case?
and I would rename 1-helpermemberexpressionfunction to something like for-of-member-expression or something
There was a problem hiding this comment.
for sure, will remember for next time!
Co-authored-by: Huáng Jùnliàng <jlhwung@gmail.com>
Co-authored-by: Huáng Jùnliàng <jlhwung@gmail.com>
|
Looks like there are failing tests - do you mind fixing those up? |
@existentialism said it had to do with an upstream dep breaking the tests, not sure how I'd go about those |
|
CI failure is unrelated and will be fixed by #11709. |
|
@wlawt great work! thank you! |
Issue Ref: Private Name in the left ForOfStatement is not transformed, #11272
The problem was that the private label
#aruns before thefor-oftransform, and that has been fixed by adding a pattern check at the start of theifstatement. Test cases included the original one from #11272 and the following:Input
Output