6
*/
7
8
import {
9
- ArrayPattern,
9
BlockId,
10
HIRFunction,
11
Identifier,
183
switch (instr.value.lvalue.pattern.kind) {
184
case 'ArrayPattern': {
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.
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];
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;
198
}
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
}
205
}
206
}
207
- if (nextItems !== null) {
208
- instr.value.lvalue.pattern.items = nextItems;
209
- }
207
+ items.length = lastEntryIndex + 1;
208
break;
209
}
210
case 'ObjectPattern': {