Skip to content
Myr

use invariant -321 bytes

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 737432a · Browse files · 10 files+45 added−58 removed
This commit

State

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

Diff stats

10 files+45 added−58 removed

  • other:+45 added−58 removed

10 files+45 added−58 removed

modified:src/plugins/mapset.ts+4 added-8 removed
14 unmodified lines
15 createProxy,16 loadPlugin,17 markChanged18} from "../internal"19import invariant from "tiny-invariant"2021export function enableMapSet() {22 /* istanbul ignore next */23 var extendStatics = function(d: any, b: any): any {
20 unmodified lines
44 ((__.prototype = b.prototype), new __())45 }4647 const DraftMap = (function(_super) {47 if (!_super) {48 /* istanbul ignore next */49 throw new Error("Map is not polyfilled")48 invariant(_super, "Map is not polyfilled")50 }49 __extends(DraftMap, _super)50 // Create class manually, cause #50251 function DraftMap(this: any, target: AnyMap, parent?: ImmerState): any {52 this[DRAFT_STATE] = {
146 unmodified lines
199 }200 }201202 const DraftSet = (function(_super) {205 if (!_super) {206 /* istanbul ignore next */207 throw new Error("Set is not polyfilled")208 }203 invariant(_super, "Set is not polyfilled")204205 __extends(DraftSet, _super)206 // Create class manually, cause #502207 function DraftSet(this: any, target: AnySet, parent?: ImmerState) {208 this[DRAFT_STATE] = {
132 unmodified lines
modified:src/plugins/patches.ts+4 added-4 removed
18 unmodified lines
19 isSet,20 isMap,21 loadPlugin22} from "../internal"23import invariant from "tiny-invariant"2425export function enablePatches() {26 function generatePatches(27 state: ImmerState,
167 unmodified lines
195196 let base: any = draft197 for (let i = 0; i < path.length - 1; i++) {198 base = get(base, path[i])198 if (!base || typeof base !== "object")199 throw new Error("Cannot apply patch, path doesn't resolve: " + path.join("/")) // prettier-ignore199 invariant(typeof base === "object","Cannot apply patch, path doesn't resolve: " + path.join("/")) // prettier-ignore200 }201202 const type = getArchtype(base)203 const value = deepClonePatchValue(patch.value) // used to clone patch to ensure original patch is not modified, see #411
4 unmodified lines
208 case Archtype.Map:209 return base.set(key, value)210 /* istanbul ignore next */211 case Archtype.Set:212 throw new Error('Sets cannot have "replace" patches.')212 invariant(false, 'Sets cannot have "replace" patches.')213 default:214 // if value is an object, then it's assigned by reference215 // in the following add or remove ops, the value field inside the patch will also be modifyed216 // so we use value from the cloned patch
22 unmodified lines
239 default:240 return delete base[key]241 }242 default:243 throw new Error("Unsupported patch operation: " + op)243 invariant(false, "Unsupported patch operation: " + op)244 }245 })246247 return draft
21 unmodified lines
modified:src/common.ts+9 added-10 removed
11 unmodified lines
12 ProxyType,13 Archtype,14 hasMap15} from "./internal"16import invariant from "tiny-invariant"1718/** Returns true if the given value is an Immer draft */19/*#__PURE__*/20export function isDraft(value: any): boolean {
153 unmodified lines
174 }175 const desc = Object.getOwnPropertyDescriptor(base, key)!176 let {value} = desc177 if (desc.get) {177 if (!invokeGetters) {178 throw new Error("Immer drafts cannot have computed properties")178 invariant(invokeGetters, "Immer drafts cannot have computed properties")179 }179 value = desc.get.call(base)180 }181 if (desc.enumerable) {182 clone[key] = value
20 unmodified lines
203 if (deep) each(obj, (_, value) => freeze(value, true))204}205206function dontMutateFrozenCollections() {208 throw new Error("This object has been frozen and should not be mutated")207 invariant(false, "This object has been frozen and should not be mutated")208}209210export function createHiddenProperty(211 target: AnyObject,
8 unmodified lines
220}221222/* istanbul ignore next */223export function die(): never {225 throw new Error("Illegal state, please file a bug")224 invariant(false, "Illegal state, please file a bug")225}226227export function assertUnrevoked(state: any /*ES5State | MapState | SetState*/) {229 if (state.revoked === true)230 throw new Error(231 "Cannot use a proxy that has been revoked. Did you pass an object from inside an immer function to an async process? " +232 JSON.stringify(latest(state))233 )228 invariant(229 !state.revoked,230 "Cannot use a proxy that has been revoked. Did you pass an object from inside an immer function to an async process? " +231 JSON.stringify(latest(state))232 )233}
modified:src/finalize.ts+3 added-4 removed
19 unmodified lines
20 is,21 get,22 willFinalize23} from "./internal"24import invariant from "tiny-invariant"2526export function processResult(immer: Immer, result: any, scope: ImmerScope) {27 const baseDraft = scope.drafts![0]28 const isReplaced = result !== undefined && result !== baseDraft29 willFinalize(immer, scope, result, isReplaced)30 if (isReplaced) {31 if (baseDraft[DRAFT_STATE].modified) {32 scope.revoke()32 throw new Error("An immer producer returned a new value *and* modified its draft. Either return a new value *or* modify the draft.") // prettier-ignore33 invariant(false, "An immer producer returned a new value *and* modified its draft. Either return a new value *or* modify the draft.") // prettier-ignore34 }35 if (isDraftable(result)) {36 // Finalize the result in case it contains (or is) a subset of the draft.37 result = finalize(immer, result, scope)
109 unmodified lines
147 prop: string | number,148 childValue: any,149 rootPath?: PatchPath150) {150 if (childValue === parentValue) {151 throw Error("Immer forbids circular references")152 }151 invariant(childValue !== parentValue, "Immer forbids circular references")152153 // In the `finalizeTree` method, only the `root` object may be a draft.154 const isDraftProp = !!rootState && parentValue === root155 const isSetMember = isSet(parentValue)
52 unmodified lines
modified:src/immerClass.ts+12 added-18 removed
25 unmodified lines
26 proxySet,27 markChangedProxy,28 createProxyProxy29} from "./internal"30import invariant from "tiny-invariant"3132declare const __DEV__: boolean33/* istanbul ignore next */34function verifyMinified() {}
77 unmodified lines
112 return self.produce(base, (draft: Drafted) => recipe.call(this, draft, ...args)) // prettier-ignore113 }114 }115115 // prettier-ignore116 {117 if (typeof recipe !== "function") {118 throw new Error("The first or second argument to `produce` must be a function")119 }120 if (patchListener !== undefined && typeof patchListener !== "function") {121 throw new Error("The third argument to `produce` must be a function or undefined")122 }123 }116 invariant(117 typeof recipe === "function",118 "The first or second argument to `produce` must be a function"119 )120 invariant(121 patchListener === undefined || typeof patchListener === "function",122 "The third argument to `produce` must be a function or undefined"123 )124125 let result126127 // Only plain objects, arrays, and "immerable classes" are drafted.
48 unmodified lines
176 return [nextState, patches!, inversePatches!]177 }178179 createDraft<T extends Objectish>(base: T): Draft<T> {180 if (!isDraftable(base)) {181 throw new Error("First argument to `createDraft` must be a plain object, an array, or an immerable object") // prettier-ignore180 invariant(isDraftable(base), "First argument to `createDraft` must be a plain object, an array, or an immerable object") // prettier-ignore182 }181 const scope = ImmerScope.enter(this)182 const proxy = createProxy(this, base, undefined)183 proxy[DRAFT_STATE].isManual = true184 scope.leave()
4 unmodified lines
189 draft: D,190 patchListener?: PatchListener191 ): D extends Draft<infer T> ? T : never {192 const state: ImmerState = draft && draft[DRAFT_STATE]195 if (!state || !state.isManual) {196 throw new Error("First argument to `finishDraft` must be a draft returned by `createDraft`") // prettier-ignore197 }198 if (state.finalized) {199 throw new Error("The given draft is already finalized") // prettier-ignore200 }193 invariant(state && state.isManual, "First argument to `finishDraft` must be a draft returned by `createDraft`") // prettier-ignore194 invariant(!state.finalized, "The given draft is already finalized") // prettier-ignore195 const {scope} = state196 scope.usePatches(patchListener)197 return processResult(this, undefined, scope)198 }
78 unmodified lines
modified:src/proxy.ts+5 added-8 removed
15 unmodified lines
16 ImmerScope,17 DRAFT_STATE,18 createProxy19} from "./internal"20import invariant from "tiny-invariant"2122interface ProxyBaseState extends ImmerBaseState {23 assigned: {24 [property: string]: boolean
156 unmodified lines
181 }182 return desc183 },184 defineProperty() {184 throw new Error("Object.defineProperty() cannot be used on an Immer draft") // prettier-ignore185 invariant(false, "Object.defineProperty() cannot be used on an Immer draft") // prettier-ignore186 },187 getPrototypeOf(state) {188 return Object.getPrototypeOf(state.base)189 },190 setPrototypeOf() {190 throw new Error("Object.setPrototypeOf() cannot be used on an Immer draft") // prettier-ignore191 invariant(false, "Object.setPrototypeOf() cannot be used on an Immer draft") // prettier-ignore192 }193}194195/**
8 unmodified lines
204 return fn.apply(this, arguments)205 }206})207arrayTraps.deleteProperty = function(state, prop) {207 if (isNaN(parseInt(prop as any))) {208 throw new Error("Immer only supports deleting array indices") // prettier-ignore208 invariant(!isNaN(parseInt(prop as any)), "Immer only supports deleting array indices") // prettier-ignore209 }209 return objectTraps.deleteProperty!.call(this, state[0], prop)210}211arrayTraps.set = function(state, prop, value) {213 if (prop !== "length" && isNaN(parseInt(prop as any))) {214 throw new Error("Immer only supports setting array indices and the 'length' property") // prettier-ignore212 invariant(prop === "length" || !isNaN(parseInt(prop as any)), "Immer only supports setting array indices and the 'length' property") // prettier-ignore215 }213 return objectTraps.set!.call(this, state[0], prop, value, state[0])214}215216/**
39 unmodified lines
modified:src/scope.ts+1 added
modified:package.json+1 added
modified:tsdx.config.js+1 added-6 removed
1 unmodified line
2 // This function will run for each entry/format/env combination3 rollup(config, options) {4 return options.format === "esm"5 ? {6 ...config,7 // this makes sure sideEffects: true can clean up files8 // preserveModules: true,9 output: {10 dir: "dist"11 }6 ...config7 }8 : config9 }10}
modified:yarn.lock+5 added
11662 unmodified lines
11663 dependencies:11664 globalyzer "^0.1.0"11665 globrex "^0.1.1"1166611667tiny-invariant@^1.1.0:11668 version "1.1.0"11669 resolved "https://registry.yarnpkg.com/tiny-invariant/-/tiny-invariant-1.1.0.tgz#634c5f8efdc27714b7f386c35e6760991d230875"11670 integrity sha512-ytxQvrb1cPc9WBEI/HSeYYoGD0kWnGEOR8RY6KomWLBVhqz0RgTwVO9dLrGz7dC+nN9llyI7OKAgRq8Vq4ZBSw==1167111672tiny-relative-date@^1.3.0:11673 version "1.3.0"11674 resolved "https://registry.yarnpkg.com/tiny-relative-date/-/tiny-relative-date-1.3.0.tgz#fa08aad501ed730f31cc043181d995c39a935e07"11675 integrity sha512-MOQHpzllWxDCHHaDno30hhLfbouoYlOI8YlMNtvKe1zXbjEVhbcEovQxvZrPvtiYW630GQDoMMarCnjfyfHA+A==
1024 unmodified lines