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
Provenance
- Commit
ac57da5- Tree
3f0a888- Author
Michel Weststrate <mweststrate@fb.com>- Committer
Michel Weststrate <mweststrate@fb.com>- Parent
737432a
10 files changed+45 added−58 removed
modified:src/plugins/mapset.ts+4 added-8 removed
14 unmodified lines15 createProxy,16 loadPlugin,17 markChanged18 } from "../internal"19+import invariant from "tiny-invariant"20 21 export function enableMapSet() {22 /* istanbul ignore next */23 var extendStatics = function(d: any, b: any): any {20 unmodified lines44 ((__.prototype = b.prototype), new __())45 }46 47 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 lines199 }200 }201 202 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")204+205 __extends(DraftSet, _super)206 // Create class manually, cause #502207 function DraftSet(this: any, target: AnySet, parent?: ImmerState) {208 this[DRAFT_STATE] = {132 unmodified linesmodified:src/plugins/patches.ts+4 added-4 removed
18 unmodified lines19 isSet,20 isMap,21 loadPlugin22 } from "../internal"23+import invariant from "tiny-invariant"24 25 export function enablePatches() {26 function generatePatches(27 state: ImmerState,167 unmodified lines195 196 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 }201 202 const type = getArchtype(base)203 const value = deepClonePatchValue(patch.value) // used to clone patch to ensure original patch is not modified, see #4114 unmodified lines208 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 patch22 unmodified lines239 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 })246 247 return draft21 unmodified linesmodified:src/common.ts+9 added-10 removed
11 unmodified lines12 ProxyType,13 Archtype,14 hasMap15 } from "./internal"16+import invariant from "tiny-invariant"17 18 /** Returns true if the given value is an Immer draft */19 /*#__PURE__*/20 export function isDraft(value: any): boolean {153 unmodified lines174 }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] = value20 unmodified lines203 if (deep) each(obj, (_, value) => freeze(value, true))204 }205 206 function 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 }209 210 export function createHiddenProperty(211 target: AnyObject,8 unmodified lines220 }221 222 /* istanbul ignore next */223 export function die(): never {225- throw new Error("Illegal state, please file a bug")224+ invariant(false, "Illegal state, please file a bug")225 }226 227 export 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 lines20 is,21 get,22 willFinalize23 } from "./internal"24+import invariant from "tiny-invariant"25 26 export 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 lines147 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")152 153 // In the `finalizeTree` method, only the `root` object may be a draft.154 const isDraftProp = !!rootState && parentValue === root155 const isSetMember = isSet(parentValue)52 unmodified linesmodified:src/immerClass.ts+12 added-18 removed
25 unmodified lines26 proxySet,27 markChangedProxy,28 createProxyProxy29 } from "./internal"30+import invariant from "tiny-invariant"31 32 declare const __DEV__: boolean33 /* istanbul ignore next */34 function verifyMinified() {}77 unmodified lines112 return self.produce(base, (draft: Drafted) => recipe.call(this, draft, ...args)) // prettier-ignore113 }114 }115 115- // 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+ )124 125 let result126 127 // Only plain objects, arrays, and "immerable classes" are drafted.48 unmodified lines176 return [nextState, patches!, inversePatches!]177 }178 179 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 lines189 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 linesmodified:src/proxy.ts+5 added-8 removed
15 unmodified lines16 ImmerScope,17 DRAFT_STATE,18 createProxy19 } from "./internal"20+import invariant from "tiny-invariant"21 22 interface ProxyBaseState extends ImmerBaseState {23 assigned: {24 [property: string]: boolean156 unmodified lines181 }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 }194 195 /**8 unmodified lines204 return fn.apply(this, arguments)205 }206 })207 arrayTraps.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 }211 arrayTraps.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 }215 216 /**39 unmodified linesmodified:src/scope.ts+1 added
6 unmodified lines78910111213141554 unmodified lines6 unmodified lines DRAFT_STATE} from "./internal"
/** Each scope represents a `produce` call. */// TODO: non-class?export class ImmerScope { static current?: ImmerScope
patches?: Patch[]54 unmodified linesmodified:package.json+1 added
72 unmodified lines7374757677787980813 unmodified lines72 unmodified lines "rimraf": "^2.6.2", "seamless-immutable": "^7.1.3", "semantic-release": "^17.0.2", "spec.ts": "^1.1.0", "tiny-invariant": "^1.1.0", "ts-jest": "^25.2.0", "tsdx": "^0.12.3", "typescript": "^3.7.3", "webpack": "^4.41.6",3 unmodified linesmodified:tsdx.config.js+1 added-6 removed
1 unmodified line2 // 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 lines11663 dependencies:11664 globalyzer "^0.1.0"11665 globrex "^0.1.1"11666 11667+tiny-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==11671+11672 tiny-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