Skip to content
Myr

fix: Make `applyPatches` to accept `readonly Patch[]` (#1094)

Before adoption: this commit came in with the history the project adopted, so no landing records it.

* Demonstrate possible regression * Make `Patch[]` arrays `readonly` on the outer bounds * Limit the change to `applyPatches` ---------

authored by Mateusz Burzyński · committed by GitHub · parent 073d634 · Browse files · 4 files+19 added−4 removed
This commit

State

Before adoption: this commit came in with the history the project adopted, so no landing records it.

Trailers

  • Co-authored-by: Minh Nguyen <2852660+NMinhNguyen@users.noreply.github.com>

Diff stats

4 files+19 added−4 removed

  • tests:+16 added−1 removed
  • other:+3 added−3 removed

4 files+19 added−4 removed

modified:__tests__/produce.ts+16 added-1 removed
6 unmodified lines
7 Draft,8 Immutable,9 Immer,10 enableMapSet,11 enablePatches11 enablePatches,12 produceWithPatches13} from "../src/immer"1415enableMapSet()16enablePatches()
145 unmodified lines
162163 expect(applyPatches({}, patches)).toEqual({x: 4})164})165166it("can apply readonly patches", () => {167 const [, patches]: readonly [168 {169 x: number170 },171 readonly Patch[],172 readonly Patch[]173 ] = produceWithPatches({x: 3}, d => {174 d.x++175 })176177 expect(applyPatches({}, patches)).toEqual({x: 4})178})179180describe("curried producer", () => {181 it("supports rest parameters", () => {182 type State = {readonly a: 1}183
577 unmodified lines
modified:src/core/immerClass.ts+1 added-1 removed
170 unmodified lines
171 setUseStrictShallowCopy(value: StrictMode) {172 this.useStrictShallowCopy_ = value173 }174175 applyPatches<T extends Objectish>(base: T, patches: Patch[]): T {175 applyPatches<T extends Objectish>(base: T, patches: readonly Patch[]): T {176 // If a patch replaces the entire state, take that replacement as base177 // before applying patches178 let i: number179 for (i = patches.length - 1; i >= 0; i--) {
39 unmodified lines
modified:src/plugins/patches.ts+1 added-1 removed
209 unmodified lines
210 value: baseValue211 })212 }213214 function applyPatches_<T>(draft: T, patches: Patch[]): T {214 function applyPatches_<T>(draft: T, patches: readonly Patch[]): T {215 patches.forEach(patch => {216 const {path, op} = patch217218 let base: any = draft
99 unmodified lines
modified:src/utils/plugins.ts+1 added-1 removed