Ensure array methods really do no-op correctly
Before adoption: this commit came in with the history the project adopted, so no landing records it.
authored by Mark Erikson · committed by Mark Erikson · parent 0f158b3 · Browse files · 1 file·+47 added−24 removed
This commit
State
Before adoption: this commit came in with the history the project adopted, so no landing records it.
Diff stats
1 file+47 added−24 removed
- other:+47 added−24 removed
Provenance
- Commit
eea464f- Tree
ff46527- Author
Mark Erikson <mark@isquaredsoftware.com>- Committer
Mark Erikson <mark@isquaredsoftware.com>- Parent
0f158b3
1 file changed+47 added−24 removed
modified:src/plugins/arrayMethods.ts+47 added-24 removed
244 unmodified lines245 state: ProxyArrayState,246 method: string,247 args: any[]248 ) {249+ // Detect no-op calls before creating a copy so structural sharing is250+ // preserved: push/unshift with no arguments add nothing, and pop/shift251+ // on an empty array remove nothing. The check must happen before252+ // prepareCopy - a stray copy_ on an unmodified state would be picked253+ // up by finalization of a parent Set and break identity there.254+ const isInsert = method === "push" || method === "unshift"255+ if (isInsert ? args.length === 0 : latest(state).length === 0) {256+ // Match the native return values: push/unshift return the257+ // (unchanged) length, pop/shift on an empty array return undefined258+ return isInsert ? latest(state).length : undefined259+ }260+261 prepareCopy(state)262 263 // For push/unshift, capture the length before the operation264 // so we can compute insertion indices for handleCrossReference265 const lengthBefore = state.copy_!.length266 267 const result = (state.copy_! as any)[method](...args)268 257- // Detect no-op calls so structural sharing is preserved:258- // push/unshift with no arguments add nothing, and pop/shift on an259- // empty array remove nothing. In those cases the array is unchanged.260- const changed =261- method === "push" || method === "unshift"262- ? args.length > 0263- : lengthBefore > 0 // pop / shift269+ markArrayChanged(state)270 265- if (changed) {266- markArrayChanged(state)271+ // Handle index reassignment for shifting methods272+ if (SHIFTING_METHODS.has(method as MutatingArrayMethod)) {273+ markAllIndicesReassigned(state)274+ }275 268- // Handle index reassignment for shifting methods269- if (SHIFTING_METHODS.has(method as MutatingArrayMethod)) {270- markAllIndicesReassigned(state)271- }272-273- // Handle cross-references for newly inserted values.274- // push appends at the end, unshift inserts at the beginning.275- if (method === "push") {276- handleInsertedValues(state, lengthBefore, args)277- } else if (method === "unshift") {278- handleInsertedValues(state, 0, args)276+ // Handle cross-references for newly inserted values.277+ // push appends at the end, unshift inserts at the beginning.278+ if (method === "push") {279+ handleInsertedValues(state, lengthBefore, args)280+ } else if (method === "unshift") {281+ handleInsertedValues(state, 0, args)279- }282 }283 284 // Return appropriate value based on method285 return RESULT_RETURNING_METHODS.has(method as MutatingArrayMethod)63 unmodified lines349 return handleReorderingOperation(state, method, args)350 }351 352 if (method === "splice") {351- prepareCopy(state)353 const insertCount = args.length > 2 ? args.length - 2 : 0354+355+ // Detect no-op calls before creating a copy so structural356+ // sharing is preserved (see handleSimpleOperation). A splice357+ // removes nothing when called with no arguments, when the358+ // normalized start is at/past the end, or when the delete359+ // count coerces to less than 1. splice(start) with a single360+ // argument removes everything from start onwards, so it must361+ // not match. Exotic arguments that slip through are caught362+ // by the post-copy check below.363+ if (insertCount === 0) {364+ const length = latest(state).length365+ if (366+ args.length === 0 ||367+ normalizeSliceIndex(args[0] ?? 0, length) === length ||368+ (args.length > 1 && !(args[1] >= 1))369+ ) {370+ return []371+ }372+ }373+374+ prepareCopy(state)375 const res = state.copy_!.splice(376 ...(args as [number, number, ...any[]])377 )378 357- // A splice that removes and inserts nothing leaves the array358- // unchanged, so structural sharing must be preserved.379+ // Backstop for exotic arguments the pre-check above missed:380+ // if nothing was removed and nothing inserted, the array is381+ // unchanged and must not be marked as modified.382 if (res.length === 0 && insertCount === 0) {383 return res384 }385 118 unmodified lines