|
6 | 6 | */
|
7 | 7 |
|
8 | 8 | import {
|
9 |
| - ArrayPattern, |
10 | 9 | BlockId,
|
11 | 10 | HIRFunction,
|
12 | 11 | Identifier,
|
@@ -184,29 +183,28 @@ function rewriteInstruction(instr: Instruction, state: State): void {
|
184 | 183 | switch (instr.value.lvalue.pattern.kind) {
|
185 | 184 | case 'ArrayPattern': {
|
186 | 185 | /*
|
187 |
| - * For arrays, we can only eliminate unused items from the end of the array, |
188 |
| - * so we iterate from the end and break once we find a used item. Note that |
189 |
| - * we already know at least one item is used, from the pruneableValue check. |
| 186 | + * For arrays, we can prune items prior to the end by replacing |
| 187 | + * them with a hole. Items at the end can simply be dropped. |
190 | 188 | */
|
191 |
| - let nextItems: ArrayPattern['items'] | null = null; |
192 |
| - const originalItems = instr.value.lvalue.pattern.items; |
193 |
| - for (let i = originalItems.length - 1; i >= 0; i--) { |
194 |
| - const item = originalItems[i]; |
| 189 | + let lastEntryIndex = 0; |
| 190 | + const items = instr.value.lvalue.pattern.items; |
| 191 | + for (let i = 0; i < items.length; i++) { |
| 192 | + const item = items[i]; |
195 | 193 | if (item.kind === 'Identifier') {
|
196 |
| - if (state.isIdOrNameUsed(item.identifier)) { |
197 |
| - nextItems = originalItems.slice(0, i + 1); |
198 |
| - break; |
| 194 | + if (!state.isIdOrNameUsed(item.identifier)) { |
| 195 | + items[i] = {kind: 'Hole'}; |
| 196 | + } else { |
| 197 | + lastEntryIndex = i; |
199 | 198 | }
|
200 | 199 | } else if (item.kind === 'Spread') {
|
201 |
| - if (state.isIdOrNameUsed(item.place.identifier)) { |
202 |
| - nextItems = originalItems.slice(0, i + 1); |
203 |
| - break; |
| 200 | + if (!state.isIdOrNameUsed(item.place.identifier)) { |
| 201 | + items[i] = {kind: 'Hole'}; |
| 202 | + } else { |
| 203 | + lastEntryIndex = i; |
204 | 204 | }
|
205 | 205 | }
|
206 | 206 | }
|
207 |
| - if (nextItems !== null) { |
208 |
| - instr.value.lvalue.pattern.items = nextItems; |
209 |
| - } |
| 207 | + items.length = lastEntryIndex + 1; |
210 | 208 | break;
|
211 | 209 | }
|
212 | 210 | case 'ObjectPattern': {
|
|
0 commit comments