Repository navigation
fix(transform-spread): copy constant array when later elements have side effects - #18613
Conversation
|
Build successful! You can test your changes in the REPL here: https://babeljs.io/repl/build/63469 |
|
commit: |
| let lastImpureIndex = -1; | ||
|
|
||
| for (const prop of props) { | ||
| if (!iterableIsArray) { |
There was a problem hiding this comment.
The lastImpureIndex is computed only when iterableIsArray is false, so under iterableIsArray assumption, the #18531 is still not fixed.
Given the input [...a, a.push(3)], under this assumption, we can emit
[].concat(a.slice(), [a.push(3)])so it still outputs [1, 2, 3]. Could you implement it and add a new test case for the iterableIsArray assumption case?
There was a problem hiding this comment.
Good catch, updated to emit .slice() under iterableIsArray when a later element has side effects and added a test fixture for it.
86ff844 to
ed044a8
Compare
|
@hoanle0126 I have rebased this PR, please update the test fixtures due to recent main change, thank you. |
|
@JLHwung Thanks for rebasing! I updated the two test fixtures for |
|
Backported to 7.x in #18664. |
When an array is spread before impure elements (such as
[...a, a.push(3)]), passingadirectly to.concat(...)evaluates the later arguments before readinga. Clone the spread argument whenever a subsequent element may have side effects (toConsumableArrayfor constantArraybindings by default, or.slice()under theiterableIsArrayassumption).