Skip to content
Myr

Added istanbul rules to get 100% test coverage

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 617772f · Browse files · 6 files+23 added−12 removed
This commit

State

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

Diff stats

6 files+23 added−12 removed

  • other:+23 added−12 removed

6 files+23 added−12 removed

modified:src/common.ts+4 added-2 removed
86 unmodified lines
87 ? obj =>88 Object.getOwnPropertyNames(obj).concat(89 Object.getOwnPropertySymbols(obj) as any90 )91 : Object.getOwnPropertyNames91 : /* istanbul ignore next */ Object.getOwnPropertyNames9293export function each<T extends Objectish>(94 obj: T,95 iter: (key: string | number, value: any, source: T) => void
11 unmodified lines
107 return desc && desc.enumerable ? true : false108}109110export function getArchtype(thing: any): Archtype {111 /* istanbul ignore next */112 if (!thing) die()113 if (thing[DRAFT_STATE]) {114 switch ((thing as Drafted)[DRAFT_STATE].type) {115 case ProxyType.ES5Object:
94 unmodified lines
210 })211 return clone212}213213export function freeze(obj: any, deep: boolean = false): void {214export function freeze(obj: any, deep: boolean): void {215 if (!isDraftable(obj) || isDraft(obj) || Object.isFrozen(obj)) return216 const type = getArchtype(obj)217 if (type === Archtype.Set) {218 obj.add = obj.clear = obj.delete = dontMutateFrozenCollections as any
19 unmodified lines
238 writable: true239 })240}241242/* istanbul ignore next */243export function die(): never {244 throw new Error("Illegal state, please file a bug")245}
modified:src/finalize.ts+3 added-3 removed
76 unmodified lines
77 state.finalized = true78 finalizeTree(immer, state.draft, scope, path)7980 // We cannot really delete anything inside of a Set. We can only replace the whole Set.81 if (immer.onDelete && !isSet(state.base)) {81 if (immer.onDelete && state.type !== ProxyType.Set) {82 // The `assigned` object is unreliable with ES5 drafts.83 if (immer.useProxies) {84 const {assigned} = state85 each(assigned, (prop, exists) => {86 if (!exists) immer.onDelete?.(state, prop as any)86 if (!exists) immer.onDelete!(state, prop as any)87 })88 } else {89 const {base, copy} = state90 each(base, prop => {91 if (!has(copy, prop)) immer.onDelete?.(state, prop as any)91 if (!has(copy, prop)) immer.onDelete!(state, prop as any)92 })93 }94 }95 if (immer.onCopy) {
108 unmodified lines
modified:src/immer.ts+7 added-4 removed
7 unmodified lines
8 isSet,9 isMap,10 isDraftable,11 DRAFT_STATE,12 NOTHING12 NOTHING,13 die14} from "./common"15import {ImmerScope} from "./scope"16import {17 ImmerState,
8 unmodified lines
26import {proxyMap} from "./map"27import {proxySet} from "./set"28import {processResult, maybeFreeze} from "./finalize"2930/* istanbul ignore next */31function verifyMinified() {}3233const configDefaults = {34 useProxies:
2 unmodified lines
37 typeof Reflect !== "undefined",38 autoFreeze:39 typeof process !== "undefined"40 ? process.env.NODE_ENV !== "production"41 : /* istanbul ignore next */39 : verifyMinified.name === "verifyMinified",42 verifyMinified.name === "verifyMinified",43 onAssign: null,44 onDelete: null,45 onCopy: null46} as const
118 unmodified lines
165 return (state: any, ...args: any[]) =>166 this.produceWithPatches(state, (draft: any) => arg1(draft, ...args))167 }168 // non-curried form166 if (arg3)167 throw new Error("A patch listener cannot be passed to produceWithPatches")169 /* istanbul ignore next */170 if (arg3) die()171 let patches: Patch[], inversePatches: Patch[]172 const nextState = this.produce(arg1, arg2, (p: Patch[], ip: Patch[]) => {173 patches = p174 inversePatches = ip
102 unmodified lines
modified:src/map.ts+3 added
modified:src/patches.ts+4 added-2 removed
41 unmodified lines
42 patches: Patch[],43 inversePatches: Patch[]44) {45 let {base, assigned, copy} = state46 /* istanbul ignore next */47 if (!copy) die()4849 // Reduce complexity by ensuring `base` is never longer.49 if (copy!.length < base.length) {50 if (copy.length < base.length) {51 // @ts-ignore52 ;[base, copy] = [copy, base]53 ;[patches, inversePatches] = [inversePatches, patches]54 }
118 unmodified lines
173export function applyPatches<T>(draft: T, patches: Patch[]): T {174 patches.forEach(patch => {175 const {path, op} = patch176177 /* istanbul ignore next */178 if (!path.length) die()179180 let base: any = draft181 for (let i = 0; i < path.length - 1; i++) {
9 unmodified lines
191 case "replace":192 switch (type) {193 case Archtype.Map:194 return base.set(key, value)195 /* istanbul ignore next */196 case Archtype.Set:197 throw new Error('Sets cannot have "replace" patches.')198 default:199 // if value is an object, then it's assigned by reference
1 unmodified line
201 // so we use value from the cloned patch202 // @ts-ignore203 return (base[key] = value)204 }202 break205 case "add":206 switch (type) {207 case Archtype.Array:208 return base.splice(key as any, 0, value)
42 unmodified lines
modified:src/set.ts+2 added-1 removed
12 unmodified lines
13 draft: Drafted<AnySet, SetState>14}1516// Make sure DraftSet declarion doesn't die if Map is not avialable...17/* istanbul ignore next */18const SetBase: SetConstructor =19 typeof Set !== "undefined" ? Set : (function FakeSet() {} as any)2021export class DraftSet<K, V> extends SetBase implements Set<V> {
57 unmodified lines
79 return (80 state.copy!.delete(value) ||81 (state.drafts.has(value)82 ? state.copy!.delete(state.drafts.get(value))82 : false)83 : /* istanbul ignore next */ false)84 )85 }8687 clear() {
58 unmodified lines