Borrowed data through a copy or a looser type: a shallow copy shares its parts, readonly dropped by an annotation (readonly-dropped) #141
Loading…
Reference in a new issue
No description provided.
Delete branch "shared-copies"
Deleting a branch is permanent. Although the deleted branch may continue to exist for a short time before it actually gets removed, it CANNOT be undone in most cases. Continue?
Based on
ways-out(#140), and contains it: the diff shown is this piece alone.Two ways to change data that is not yours to change, both passing
tscstrict,safe-ts check --idiomatic --deny-warningsand oxlint. Both came out of an outside audit ofmain; I confirmed each on the binary first.A shallow copy is a new object whose parts are still whoever's they were, and the checker followed only a list made of a single spread. And TypeScript lets
readonlybe dropped by any annotation, so a frozen object can be handed around as writable.What changes
no-param-mutationfollows shallow copies. An object spread,Object.assign({}, x), a destructured rest, a list with a spread among other elements,slice(),Array.from, and a copy a helper hands back are new objects whose own slots belong to the function and whose parts stay the parameter's. Changing a part through the copy is reported, and so is lending it withmut(...).Lending a whole copy to a
Mutparameter is reported only where the callee could write something the caller still holds. A field counts as shared when all of these hold:readonly T[]orReadonlyMapof read-only parts is not shared);The finding names the fields, and its help says to copy them or to write them read-only in the type. That last option turns a convention ("these lists are replaced whole, never changed in place") into something checked.
New rule
readonly-dropped(error, with a quick fix). A value whose written type has areadonlyproperty, given a place that can write it: an annotated binding, a declared return type, a field, an element of a typed list or map. Types are opened across files. Passing to a parameter that is notMutis a read and stays silent.Evidence
structuredClone, aMutparameter, the function's own data).readonly-dropped: 14 of 14 forms reported, 7 fine forms silent.maincatches 3 of 9 ways to write it. After the quick fix (Readonly<T>on the annotation),tscrefuses the direct writes and the lend, and this rule reports the next step: the constant handed out by a function, kept in a field, pushed into a list.no-param-mutation: 5 new findings, each a copy lent whole. Four are a hand-written clone of a large object that shares twelve lists, some on purpose, passed to a function that may change anything. One is a copy whose callee only reads the shared part (coarse, below).readonly-dropped: 282, all of one kind: a frozen object literal declared under a writable type. None is a defect today. On a copy of one of its packages the fix command removed 272 of 273 in one run and surfaced four more of the same kind one step on (the implementer's measurement).shallow-copy-shares-object,spread-list-shares-elements). 16 of 105 escape.readonly-droppedopening types across files; it now decides most sites in the file and remembers each pair of types for the pass. The work guard's allocation and memo-question counts each rose about 1%.What turns red on upgrade
readonly-droppedis an error, and it will fire once per frozen constant typed writable. That is 282 lines on the adopter measured.safe-ts fix readonly-dropped --writerepairs them. I kept it an error because the type is false and the fix is mechanical; making it a warning is one line, and it is the owner's call.no-param-mutationreports changes through a copy it did not follow before. Copy the part too, or declare the parameterMut<...>.rest.count = 1is no longer reported,rest.meta.tag = ...is.What stays coarse or unseen
thisand changed later.readonly-droppeddoes not open package types, mapped or computed types, or unions of object types.Created with Claude Opus 5.5 (implementation) and Claude Fable 5.2 (review, acceptance tests, the override fix and the merge) in Claude Code.
Two ways to change data that is not the function's to change passed every gate. A shallow copy of a borrowed parameter (`{ ...order }`, `Object.assign({}, order)`, a rest, a list with a spread among other elements, a copy a helper hands back) is a new object whose parts are still the caller's. `no-param-mutation` followed only a list made of one spread. It now follows the others: a change of a part through the copy is reported, and so is lending the part. A whole copy lent to a `Mut` parameter is reported where the callee could write something the copy still shares: a field the copy took, that something can be written through as far as its type is written, that the callee's parameter type has, and that the callee names (a function, a static method, or a method no override can replace). TypeScript lets `readonly` be dropped by any annotation, so a frozen object can be handed around as writable and a write to it throws when it runs. New rule `readonly-dropped` (error, with a quick fix): a value whose written type has a `readonly` property given a place that can write it, with types opened across files. Soundness corpus: `shallow-copy-shares-object` and `spread-list-shares-elements` added and closed. Contains the ways-out piece it was merged with: the rewrite list and the work ceilings are those of the merged tree. Co-Authored-By: Claude Opus 5.5 <[email protected]> Co-Authored-By: Claude Fable 5.2 <[email protected]>