-
-
Notifications
You must be signed in to change notification settings - Fork 5.7k
Fix: improve object rest handling in array pattern #17281
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
Conversation
@@ -0,0 +1,7 @@ | |||
let a = 0, result, y; | |||
|
|||
for ({ [a++]: result, ...y} of [["0", "1"]]) { |
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.
This bug is fixed by the second refactoring commit.
Build successful! You can test your changes in the REPL here: https://babeljs.io/repl/build/59270 |
...-plugin-transform-object-rest-spread/test/fixtures/object-rest/for-x-array-pattern/output.js
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.
This looks good to me. I wonder whether eventually we should implement transform for object rest, destructuring, and private destructuring all in a single package, similarly to how we do for class features and regexps.
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.
On gist.github.com/nicolo-ribaudo/f8ac7916f89450f2ead77d99855b2098, I plan to implement the object pattern split as we have already done in the destructuring-private transform, which is a more ambitious goal.
It would be nice if we could do it without using iterators. :)
Could you elaborate why you would like to avoid iterators? |
|
Thanks. We are on the same page, what I was meant for the "object pattern split" is that we handle the object rest within an object pattern as same as how we currently handle private destructuring. For example, the current private destructuring transforms var { a, b: { i: x, #p: p }, c } = rhs to var { a } = rhs,
_m = rhs.b,
{ i: x } = _m,
p = _m.#p,
{ c } = rhs; where the object pattern is split into three parts: part before the private access, at the private access and after the private. Now if we take a look at the current object rest transforms, var { a, b: { i: x, ...p }, c } = rhs is currently transformed to const _excluded = ["i"];
var { a, b: { i: x }, c } = rhs,
p = _objectWithoutProperties(rhs.b, _excluded); where const _excluded = ["i"];
var { a } = rhs,
_m = rhs.b,
{ i: x } = _m,
p = _objectWithoutProperties(_m, _excluded),
{ c } = rhs; where the The private-destructuring assumes the iterable is pure: it does not affect how other nested patterns are evaluated. If this assumption is not held, then we will have to implement the step iterator, which, like you said, will be difficult for users without polyfill. Since we don't implement the step iterator for private-destructuring, I don't plan do so for the object-rest either. |
Thanks for the detailed explanation! That sounds good.
Sorry I didn't understand, can you provide an example that is not pure? |
See babel/packages/babel-plugin-proposal-destructuring-private/src/util.ts Lines 439 to 463 in 83c72d0
This test has been disabled because we don't support it: A proper support would require the aforementioned step iterator. |
Even with the const { a: { ...r }, b = r } = rhs because |
This PR contains two refactors and the array pattern fix. On https://gist.github.com/nicolo-ribaudo/f8ac7916f89450f2ead77d99855b2098, I plan to implement the object pattern split as we have already done in the
destructuring-private
transform, which is a more ambitious goal. This PR, as a temporary fix, should unblock the discard binding transform PR.