Fix: made sure non-plain & non-draftable objects are not cloned when applying patches
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 af3a59d · Browse files · 3 files·+102 added−99 removed
This commit
State
Before adoption: this commit came in with the history the project adopted, so no landing records it.
Diff stats
3 files+102 added−99 removed
- other:+102 added−99 removed
Provenance
- Commit
99de883- Tree
978090d- Author
Michel Weststrate <mweststrate@gmail.com>- Committer
Michel Weststrate <mweststrate@gmail.com>- Parent
af3a59d
3 files changed+102 added−99 removed
modified:src/common.js+16 added-2 removed
16 unmodified lines17181920212223242526272829302425313233343575 unmodified lines11111211311411511611711811912012112216 unmodified lines return !!value && !!value[DRAFT_STATE]}
export function isDraftable(value) { if (!value) return false return ( isPlainObject(value) || !!value[DRAFTABLE] || !!value.constructor[DRAFTABLE] )}
export function isPlainObject(value) { if (!value || typeof value !== "object") return false if (Array.isArray(value)) return true const proto = Object.getPrototypeOf(value) if (!proto || proto === Object.prototype) return true return !!value[DRAFTABLE] || !!value.constructor[DRAFTABLE] return !proto || proto === Object.prototype}
export function original(value) { if (value && value[DRAFT_STATE]) {75 unmodified lines } else { return x !== x && y !== y }}
export function clone(obj) { if (!isDraftable(obj)) return obj if (Array.isArray(obj)) return obj.map(clone) const cloned = Object.create(Object.getPrototypeOf(obj)) for (const key in obj) cloned[key] = clone(obj[key]) return cloned}modified:src/patches.js+2 added-14 removed
1-import {each} from "./common"1+import {each, clone} from "./common"2 import {createDraft} from "./immer"3 4 export function generatePatches(state, basePath, patches, inversePatches) {5 Array.isArray(state.base)88 unmodified lines94 )95 })96 }97 98-// used to clone patch to ensure original patch is not modified99-const clone = obj => {100- if (obj === null || typeof obj !== "object") return obj101-102- if (Array.isArray(obj)) return obj.map(clone)103-104- const cloned = {}105- for (const key in obj) cloned[key] = clone(obj[key])106-107- return cloned108-}109-98 export const applyPatches = (draft, patches) => {99 for (const patch of patches) {100 const {path, op} = patch113- const value = clone(patch.value)101+ const value = clone(patch.value) // used to clone patch to ensure original patch is not modified, see #411102 103 if (!path.length) throw new Error("Illegal state")104 105 let base = draft36 unmodified linesmodified:package.json+84 added-83 removed
1 {2+ "name": "immer",2- "name": "immer",3- "version": "0.0.0-development",4- "description": "Create your next immutable state by mutating the current one",5- "main": "dist/immer.js",6- "umd:main": "dist/immer.umd.js",7- "unpkg": "dist/immer.umd.js",8- "jsdelivr": "dist/immer.umd.js",9- "module": "dist/immer.module.js",10- "jsnext:main": "dist/immer.module.js",11- "react-native": "dist/immer.module.js",12- "types": "./dist/immer.d.ts",13- "scripts": {14- "test": "jest",15- "test:perf": "NODE_ENV=production yarn-or-npm build && cd __performance_tests__ && babel-node add-data.js && babel-node todo.js && babel-node incremental.js",16- "test:flow": "yarn-or-npm flow check __tests__/flow",17- "test:dts": "tsc -p __tests__/tsconfig.json --noEmit",18- "coveralls": "jest --coverage && cat ./coverage/lcov.info | ./node_modules/.bin/coveralls && rm -rf ./coverage",19- "build": "rimraf dist/ && yarn-or-npm bili && yarn-or-npm typed",20- "typed": "cpx 'src/immer.{d.ts,js.flow}' dist -v"21- },22- "husky": {23- "hooks": {24- "pre-commit": "pretty-quick --staged"25- }26- },27- "repository": {28- "type": "git",29- "url": "https://github.com/immerjs/immer.git"30- },31- "keywords": [32- "immutable",33- "mutable",34- "copy-on-write"35- ],36- "author": "Michel Weststrate",37- "license": "MIT",38- "bugs": {39- "url": "https://github.com/immerjs/immer/issues"40- },41- "homepage": "https://github.com/immerjs/immer#readme",42- "files": [43- "dist",44- "src"45- ],46- "devDependencies": {47- "@babel/cli": "^7.2.3",48- "@babel/core": "^7.3.4",49- "@babel/node": "^7.2.2",50- "@babel/plugin-external-helpers": "^7.2.0",51- "@babel/preset-env": "^7.3.4",52- "@types/jest": "^24.0.11",53- "babel-jest": "^24.4.0",54- "babel-preset-modern-browsers": "^13.1.0",55- "bili": "^4.7.0",56- "coveralls": "^3.0.0",57- "cpx": "^1.5.0",58- "cross-env": "^5.1.3",59- "deep-freeze": "^0.0.1",60- "expect": "^24.7.1",61- "flow-bin": "^0.68.0",62- "husky": "^1.2.0",63- "immutable": "^3.8.2",64- "jest": "^24.7.1",65- "lodash": "^4.17.4",66- "lodash.clonedeep": "^4.5.0",67- "prettier": "1.17.0",68- "pretty-quick": "^1.8.0",69- "regenerator-runtime": "^0.11.1",70- "rimraf": "^2.6.2",71- "seamless-immutable": "^7.1.3",72- "spec.ts": "^1.1.0",73- "typescript": "3.4.3",74- "yarn-or-npm": "^2.0.4"75- },76- "jest": {77- "moduleFileExtensions": [78- "js"79- ],80- "testURL": "http://localhost",81- "transform": {82- "\\.js$": "babel-jest"83- }84- }3+ "version": "0.0.0-development",4+ "description": "Create your next immutable state by mutating the current one",5+ "main": "dist/immer.js",6+ "umd:main": "dist/immer.umd.js",7+ "unpkg": "dist/immer.umd.js",8+ "jsdelivr": "dist/immer.umd.js",9+ "module": "dist/immer.module.js",10+ "jsnext:main": "dist/immer.module.js",11+ "react-native": "dist/immer.module.js",12+ "types": "./dist/immer.d.ts",13+ "scripts": {14+ "test": "jest",15+ "test:perf": "NODE_ENV=production yarn-or-npm build && cd __performance_tests__ && babel-node add-data.js && babel-node todo.js && babel-node incremental.js",16+ "test:flow": "yarn-or-npm flow check __tests__/flow",17+ "test:dts": "tsc -p __tests__/tsconfig.json --noEmit",18+ "watch": "jest --watch",19+ "coveralls": "jest --coverage && cat ./coverage/lcov.info | ./node_modules/.bin/coveralls && rm -rf ./coverage",20+ "build": "rimraf dist/ && yarn-or-npm bili && yarn-or-npm typed",21+ "typed": "cpx 'src/immer.{d.ts,js.flow}' dist -v"22+ },23+ "husky": {24+ "hooks": {25+ "pre-commit": "pretty-quick --staged"26+ }27+ },28+ "repository": {29+ "type": "git",30+ "url": "https://github.com/immerjs/immer.git"31+ },32+ "keywords": [33+ "immutable",34+ "mutable",35+ "copy-on-write"36+ ],37+ "author": "Michel Weststrate",38+ "license": "MIT",39+ "bugs": {40+ "url": "https://github.com/immerjs/immer/issues"41+ },42+ "homepage": "https://github.com/immerjs/immer#readme",43+ "files": [44+ "dist",45+ "src"46+ ],47+ "devDependencies": {48+ "@babel/cli": "^7.2.3",49+ "@babel/core": "^7.3.4",50+ "@babel/node": "^7.2.2",51+ "@babel/plugin-external-helpers": "^7.2.0",52+ "@babel/preset-env": "^7.3.4",53+ "@types/jest": "^24.0.11",54+ "babel-jest": "^24.4.0",55+ "babel-preset-modern-browsers": "^13.1.0",56+ "bili": "^4.7.0",57+ "coveralls": "^3.0.0",58+ "cpx": "^1.5.0",59+ "cross-env": "^5.1.3",60+ "deep-freeze": "^0.0.1",61+ "expect": "^24.7.1",62+ "flow-bin": "^0.68.0",63+ "husky": "^1.2.0",64+ "immutable": "^3.8.2",65+ "jest": "^24.7.1",66+ "lodash": "^4.17.4",67+ "lodash.clonedeep": "^4.5.0",68+ "prettier": "1.17.0",69+ "pretty-quick": "^1.8.0",70+ "regenerator-runtime": "^0.11.1",71+ "rimraf": "^2.6.2",72+ "seamless-immutable": "^7.1.3",73+ "spec.ts": "^1.1.0",74+ "typescript": "3.4.3",75+ "yarn-or-npm": "^2.0.4"76+ },77+ "jest": {78+ "moduleFileExtensions": [79+ "js"80+ ],81+ "testURL": "http://localhost",82+ "transform": {83+ "\\.js$": "babel-jest"84+ }85+ }86 }