Be explicit about not supporting arbitrary properties on arrays
Before adoption: this commit came in with the history the project adopted, so no landing records it.
authored by Michel Weststrate · committed by Michel Weststrate · parent 4b9a7d3 · Browse files · 4 files·+55 added−2 removed
This commit
State
Before adoption: this commit came in with the history the project adopted, so no landing records it.
Diff stats
4 files+55 added−2 removed
- tests:+31 added−0 removed
- other:+24 added−2 removed
Provenance
- Commit
0a80c2c- Tree
97bf322- Author
Michel Weststrate <mweststrate@gmail.com>- Committer
Michel Weststrate <mweststrate@gmail.com>- Parent
4b9a7d3
4 files changed+55 added−2 removed
modified:__tests__/base.js+31 added
569 unmodified lines570 listener571 )572 }).toThrowError(/not support/)573 })574+575+ it("should not be possible to add properties to arrays", () => {576+ expect(() => {577+ produce([], d => {578+ d.x = 3579+ })580+ }).toThrow(581+ "Immer does not support setting non-numeric properties on arrays"582+ )583+ })584+585+ it("should not be possible to remove properties from arrays", () => {586+ expect(() => {587+ const base = []588+ base.x = 7589+ produce(base, d => {590+ delete d.x591+ })592+ }).toThrow(593+ "Immer does not support deleting properties from arrays"594+ )595+ })596 }597 598+ it("non-numeric array properties will be lost", () => {599+ const base = []600+ base.x = 7601+ const next = produce(base, d => {602+ d.push(3)603+ })604+ expect(next.x).toBe(undefined)605+ })606+607 it("should not throw error, see #53 - 1", () => {608 const base = {arr: [{count: 1}, {count: 2}, {count: 3}]}609 const result = produce(610 base,675 unmodified linesmodified:src/proxy.js+15 added
37 unmodified lines3839404142434445464748495051525354555657585960132 unmodified linesExpand all37 unmodified lines arguments[0] = arguments[0][0] return fn.apply(this, arguments) }})arrayTraps.deleteProperty = function(state, prop) { if (isNaN(parseInt(prop))) throw new Error( "Immer does not support deleting properties from arrays: " + prop ) return objectTraps.deleteProperty.call(this, state[0], prop)}arrayTraps.set = function(state, prop, value) { if (prop !== "length" && isNaN(parseInt(prop))) throw new Error( "Immer does not support setting non-numeric properties on arrays: " + prop ) return objectTraps.set.call(this, state[0], prop, value)}
function createState(parent, base) { return { modified: false, // this tree is modified (either this object or one of it's children)132 unmodified linesExpand allmodified:changelog.md+5 added
1 # Changelog2 3+### 1.6.04+5+* Introduced the `original(draft)` function, that, given an object from the draft, returns the original object from the base state. This can be useful if you need to do reference equality checks, or comparisons with the base state. See [#179](https://github.com/mweststrate/immer/pull/179) by [@RichieAHB](https://github.com/RichieAHB).6+* Writing or removing non-numeric properties on arrays is now strictly forbidden (Immer didn't throw before, but correct behavior wasn't guaranteed either)7+8 ### 1.5.09 10 * Added support for patches, through [#168](https://github.com/mweststrate/immer/pull/168). See the [patches](https://github.com/mweststrate/immer#patches) section for details11 165 unmodified linesmodified:readme.md+4 added-2 removed
383 unmodified lines384 ```385 386 ## Inline shortcuts using `void`387 388-Draft mutations in Immer usually warrant a code block, since a return denotes an overwrite. Sometimes that can stretch code a little more than you might be comfortable with. 388+Draft mutations in Immer usually warrant a code block, since a return denotes an overwrite. Sometimes that can stretch code a little more than you might be comfortable with.389 390 In such cases you can use javascripts [`void`](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Operators/void) operator, which evaluates expressions and returns `undefined`.391 392 ```javascript3 unmodified lines396 // Multiple mutations397 produce(draft => void (draft.user.age += 1, draft.user.height = 186))398 ```399 400+Code style is highly personal, but for code bases that are to be understood by many, we recommend to stick to the classic `draft = { draft.user.age += 1}` to avoid cognitive overhead.401+402 ## TypeScript or Flow403 404 The Immer package ships with type definitions inside the package, which should be picked up by TypeScript and Flow out of the box and without further configuration.405 43 unmodified lines449 450 ## Limitations / pitfalls451 452 1. Don't redefine draft like, `draft = myCoolNewState`. Instead, either modify the `draft` or return a new state. See [Returning data from producers](#returning-data-from-producers).451-1. Immer only supports plain objects and arrays.453+1. Immer only supports plain objects and arrays. For arrays it is assumed only numeric properties (and length) are used.454 1. Immer assumes your state to be a unidirectional tree. That is, no object should appear twice and there should be no circular references.455 1. Class instances are not, and will not be supported. Read here more on why that would be a [confusing, conceptual mismatch]456 1. Immer only processes native arrays and plain objects (with a prototype of `null` or `Object`). Any other type of value will be treated verbatim! So if you modify a `Map` or `Buffer` (or whatever complex object from the draft state), the changes will be persisted. But, both in your new and old state! So, in such cases, make sure to always produce fresh instances if you want to keep your state truly immutable.457 1. For example, working with `Date` objects is no problem, just make sure you never modify them (by using methods like `setYear` on an existing instance). Instead, always create fresh `Date` instances. Which is probably what you were unconsciously doing already.166 unmodified lines