-
-
Notifications
You must be signed in to change notification settings - Fork 5.7k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
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 |
@@ -337,6 +337,7 @@ const handle = { | |||
// [MEMBER = _VALUE] = ARR -> [_destructureSet(MEMBER) = _VALUE] = ARR | |||
// [...MEMBER] -> [..._destructureSet(MEMBER)] | |||
if ( | |||
parentPath.isForXStatement() || |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
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.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
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
?
@@ -337,6 +337,8 @@ const handle = { | |||
// [MEMBER = _VALUE] = ARR -> [_destructureSet(MEMBER) = _VALUE] = ARR | |||
// [...MEMBER] -> [..._destructureSet(MEMBER)] | |||
if ( | |||
(parentPath.isForXStatement() && | |||
parentPath.parentPath.isAssignmentPattern({ left: node })) || |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
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 === node
The 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.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
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.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
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.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
These should be reversed.
Please also add a test for for-in.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
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.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
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.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
👍 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.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
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.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
for sure, will remember for next time!
packages/babel-helper-member-expression-to-functions/src/index.js
Outdated
Show resolved
Hide resolved
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Awesome! Thank you @wlawt!
packages/babel-helper-member-expression-to-functions/src/index.js
Outdated
Show resolved
Hide resolved
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
#a
runs before thefor-of
transform, and that has been fixed by adding a pattern check at the start of theif
statement. Test cases included the original one from #11272 and the following:Input
Output