Fix: improve object rest handling in array pattern#17281
Conversation
| @@ -0,0 +1,7 @@ | |||
| let a = 0, result, y; | |||
|
|
|||
| for ({ [a++]: result, ...y} of [["0", "1"]]) { | |||
There was a problem hiding this comment.
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
nicolo-ribaudo
left a comment
There was a problem hiding this comment.
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.
liuxingbaoyu
left a comment
There was a problem hiding this comment.
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 } = rhsto 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 } = rhsis 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 } = rhsbecause |
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-privatetransform, which is a more ambitious goal. This PR, as a temporary fix, should unblock the discard binding transform PR.