Skip to content
Sarj Code Standards

no-mutating-container-transform

Return array replacements without mutating borrowed array inputs.

Why

A returned array does not undo writes visible through the caller's original aliases. Copying only after mutation leaves the caller changed.

Fix

Copy before changing the array and return the replacement, or use an explicit in-place command returning void. Preserve scalar command/result contracts.

Examples

Before — flagged Returning an array does not hide the mutation
src/order.ts
function ordered(values: number[]): number[] {
values.sort();
return values;
}
After — preferred Copy before mutating
src/order.ts
function ordered(values: number[]): number[] {
const result = [...values];
result.sort();
return result;
}