Skip to content
Myr

Introduce `class_only` strict mode setting

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 7f1b3b9 · Browse files · 4 files+108 added−64 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+108 added−64 removed

  • tests:+68 added−31 removed
  • other:+40 added−33 removed

4 files+108 added−64 removed

modified:__tests__/not-strict-copy.ts+68 added-31 removed
modified:src/core/immerClass.ts+6 added-3 removed
31 unmodified lines
32}3334export class Immer implements ProducersFns {35 autoFreeze_: boolean = true36 useStrictShallowCopy_: boolean = false36 useStrictShallowCopy_: boolean | "class_only" = false3738 constructor(config?: {autoFreeze?: boolean; useStrictShallowCopy?: boolean}) {38 constructor(config?: {39 autoFreeze?: boolean40 useStrictShallowCopy?: boolean | "class_only"41 }) {42 if (typeof config?.autoFreeze === "boolean")43 this.setAutoFreeze(config!.autoFreeze)44 if (typeof config?.useStrictShallowCopy === "boolean")45 this.setUseStrictShallowCopy(config!.useStrictShallowCopy)
119 unmodified lines
165 * Pass true to enable strict shallow copy.166 *167 * By default, immer does not copy the object descriptors such as getter, setter and non-enumrable properties.168 */166 setUseStrictShallowCopy(value: boolean) {169 setUseStrictShallowCopy(value: boolean | "class_only") {170 this.useStrictShallowCopy_ = value171 }172173 applyPatches<T extends Objectish>(base: T, patches: Patch[]): T {
43 unmodified lines
modified:src/utils/common.ts+30 added-26 removed
138 unmodified lines
139 return state.copy_ || state.base_140}141142/*#__PURE__*/143export function shallowCopy(base: any, strict: boolean) {143export function shallowCopy(base: any, strict: boolean | "class_only") {144 if (isMap(base)) {145 return new Map(base)146 }147 if (isSet(base)) {148 return new Set(base)149 }150 if (Array.isArray(base)) return Array.prototype.slice.call(base)151152 if (!strict) {152 const isPlain = isPlainObject(base)153154 if (strict === true || (strict === "class_only" && !isPlain)) {155 // Perform a strict copy156 const descriptors = Object.getOwnPropertyDescriptors(base)157 delete descriptors[DRAFT_STATE as any]158 let keys = Reflect.ownKeys(descriptors)159 for (let i = 0; i < keys.length; i++) {160 const key: any = keys[i]161 const desc = descriptors[key]162 if (desc.writable === false) {163 desc.writable = true164 desc.configurable = true165 }166 // like object.assign, we will read any _own_, get/set accessors. This helps in dealing167 // with libraries that trap values, like mobx or vue168 // unlike object.assign, non-enumerables will be copied as well169 if (desc.get || desc.set)170 descriptors[key] = {171 configurable: true,172 writable: true, // could live with !!desc.set as well here...173 enumerable: desc.enumerable,174 value: base[key]175 }176 }177 return Object.create(getPrototypeOf(base), descriptors)178 } else {179 // perform a sloppy copy180 const proto = getPrototypeOf(base)154 if (proto !== null && isPlainObject(base)) {181 if (proto !== null && isPlain) {182 return {...base} // assumption: better inner class optimization than the assign below183 }184 const obj = Object.create(proto)185 return Object.assign(obj, base)186 }160161 const descriptors = Object.getOwnPropertyDescriptors(base)162 delete descriptors[DRAFT_STATE as any]163 let keys = Reflect.ownKeys(descriptors)164 for (let i = 0; i < keys.length; i++) {165 const key: any = keys[i]166 const desc = descriptors[key]167 if (desc.writable === false) {168 desc.writable = true169 desc.configurable = true170 }171 // like object.assign, we will read any _own_, get/set accessors. This helps in dealing172 // with libraries that trap values, like mobx or vue173 // unlike object.assign, non-enumerables will be copied as well174 if (desc.get || desc.set)175 descriptors[key] = {176 configurable: true,177 writable: true, // could live with !!desc.set as well here...178 enumerable: desc.enumerable,179 value: base[key]180 }181 }182 return Object.create(getPrototypeOf(base), descriptors)187}188189/**190 * Freezes draftable objects. Returns the original object.
26 unmodified lines
modified:website/docs/complex-objects.md+4 added-4 removed