Skip to content

Commit a2c49f4

Browse files
authored
Merge pull request #8926 from acdlite/fiberuseinvariant
[Fiber] Replace `throw new Error` with invariant module
2 parents e05f0a3 + 915a3e8 commit a2c49f4

File tree

7 files changed

+217
-103
lines changed

7 files changed

+217
-103
lines changed

src/renderers/shared/fiber/ReactChildFiber.js

Lines changed: 24 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -72,6 +72,9 @@ const {
7272
Deletion,
7373
} = ReactTypeOfSideEffect;
7474

75+
const internalErrorMessage =
76+
'This error is likely caused by a bug in React. Please file an issue.';
77+
7578
function coerceRef(current: ?Fiber, element: ReactElement) {
7679
let mixedRef = element.ref;
7780
if (mixedRef != null && typeof mixedRef !== 'function') {
@@ -88,7 +91,11 @@ function coerceRef(current: ?Fiber, element: ReactElement) {
8891
inst = (owner : any).getPublicInstance();
8992
}
9093
}
91-
invariant(inst, 'Missing owner for string ref %s', mixedRef);
94+
invariant(
95+
inst, 'Missing owner for string ref %s. (%s)',
96+
mixedRef,
97+
internalErrorMessage
98+
);
9299
const stringRef = String(mixedRef);
93100
// Check if previous string ref matches new string ref
94101
if (current && current.ref && current.ref._stringRef === stringRef) {
@@ -797,29 +804,31 @@ function ChildReconciler(shouldClone, shouldTrackSideEffects) {
797804
// but using the iterator instead.
798805

799806
const iteratorFn = getIteratorFn(newChildrenIterable);
800-
if (typeof iteratorFn !== 'function') {
801-
throw new Error('An object is not an iterable.');
802-
}
807+
invariant(
808+
typeof iteratorFn === 'function',
809+
'An object is not an iterable. (%s)',
810+
internalErrorMessage
811+
);
803812

804813
if (__DEV__) {
805814
// First, validate keys.
806815
// We'll get a different iterator later for the main pass.
807816
const newChildren = iteratorFn.call(newChildrenIterable);
808-
if (newChildren == null) {
809-
throw new Error('An iterable object provided no iterator.');
810-
}
811-
let knownKeys = null;
812-
let step = newChildren.next();
813-
for (; !step.done; step = newChildren.next()) {
814-
const child = step.value;
815-
knownKeys = warnOnDuplicateKey(child, knownKeys);
817+
if (newChildren) {
818+
let knownKeys = null;
819+
let step = newChildren.next();
820+
for (; !step.done; step = newChildren.next()) {
821+
const child = step.value;
822+
knownKeys = warnOnDuplicateKey(child, knownKeys);
823+
}
816824
}
817825
}
818826

819827
const newChildren = iteratorFn.call(newChildrenIterable);
820-
if (newChildren == null) {
821-
throw new Error('An iterable object provided no iterator.');
822-
}
828+
invariant(
829+
newChildren != null,
830+
'An iterable object provided no iterator.',
831+
);
823832

824833
let resultingFirstChild : ?Fiber = null;
825834
let previousNewFiber : ?Fiber = null;

src/renderers/shared/fiber/ReactFiberBeginWork.js

Lines changed: 36 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -62,14 +62,18 @@ var {
6262
} = require('ReactTypeOfSideEffect');
6363
var ReactCurrentOwner = require('ReactCurrentOwner');
6464
var ReactFiberClassComponent = require('ReactFiberClassComponent');
65-
var warning = require('warning');
65+
var invariant = require('invariant');
6666

6767
if (__DEV__) {
6868
var ReactDebugCurrentFiber = require('ReactDebugCurrentFiber');
69+
var warning = require('warning');
6970

7071
var warnedAboutStatelessRefs = {};
7172
}
7273

74+
const internalErrorMessage =
75+
'This error is likely caused by a bug in React. Please file an issue.';
76+
7377
module.exports = function<T, P, I, TI, PI, C, CX, PL>(
7478
config : HostConfig<T, P, I, TI, PI, C, CX, PL>,
7579
hostContext : HostContext<C, CX>,
@@ -341,9 +345,11 @@ module.exports = function<T, P, I, TI, PI, C, CX, PL>(
341345
// we don't do the bailout and we have to reuse existing props instead.
342346
if (nextProps === null) {
343347
nextProps = memoizedProps;
344-
if (!nextProps) {
345-
throw new Error('We should always have pending or current props.');
346-
}
348+
invariant(
349+
nextProps !== null,
350+
'We should always have pending or current props. (%s)',
351+
internalErrorMessage
352+
);
347353
}
348354
} else if (nextProps === null || memoizedProps === nextProps) {
349355
if (memoizedProps.hidden &&
@@ -442,9 +448,11 @@ module.exports = function<T, P, I, TI, PI, C, CX, PL>(
442448
}
443449

444450
function mountIndeterminateComponent(current, workInProgress, priorityLevel) {
445-
if (current) {
446-
throw new Error('An indeterminate component should never have mounted.');
447-
}
451+
invariant(
452+
current === null,
453+
'An indeterminate component should never have mounted. (%s)',
454+
internalErrorMessage
455+
);
448456
var fn = workInProgress.type;
449457
var props = workInProgress.pendingProps;
450458
var unmaskedContext = getUnmaskedContext(workInProgress);
@@ -511,9 +519,11 @@ module.exports = function<T, P, I, TI, PI, C, CX, PL>(
511519
// we don't do the bailout and we have to reuse existing props instead.
512520
if (nextCoroutine === null) {
513521
nextCoroutine = current && current.memoizedProps;
514-
if (!nextCoroutine) {
515-
throw new Error('We should always have pending or current props.');
516-
}
522+
invariant(
523+
nextCoroutine != null,
524+
'We should always have pending or current props. (%s)',
525+
internalErrorMessage
526+
);
517527
}
518528
} else if (nextCoroutine === null || workInProgress.memoizedProps === nextCoroutine) {
519529
nextCoroutine = workInProgress.memoizedProps;
@@ -575,9 +585,11 @@ module.exports = function<T, P, I, TI, PI, C, CX, PL>(
575585
// we don't do the bailout and we have to reuse existing props instead.
576586
if (nextChildren === null) {
577587
nextChildren = current && current.memoizedProps;
578-
if (!nextChildren) {
579-
throw new Error('We should always have pending or current props.');
580-
}
588+
invariant(
589+
nextChildren != null,
590+
'We should always have pending or current props. (%s)',
591+
internalErrorMessage
592+
);
581593
}
582594
} else if (nextChildren === null || workInProgress.memoizedProps === nextChildren) {
583595
return bailoutOnAlreadyFinishedWork(current, workInProgress);
@@ -727,15 +739,21 @@ module.exports = function<T, P, I, TI, PI, C, CX, PL>(
727739
case Fragment:
728740
return updateFragment(current, workInProgress);
729741
default:
730-
throw new Error('Unknown unit of work tag');
742+
invariant(
743+
false,
744+
'Unknown unit of work tag. (%s)',
745+
internalErrorMessage
746+
);
731747
}
732748
}
733749

734750
function beginFailedWork(current : ?Fiber, workInProgress : Fiber, priorityLevel : PriorityLevel) {
735-
if (workInProgress.tag !== ClassComponent &&
736-
workInProgress.tag !== HostRoot) {
737-
throw new Error('Invalid type of work');
738-
}
751+
invariant(
752+
workInProgress.tag === ClassComponent ||
753+
workInProgress.tag === HostRoot,
754+
'Invalid type of work. (%s)',
755+
internalErrorMessage
756+
);
739757

740758
// Add an error effect so we can handle the error during the commit phase
741759
workInProgress.effectTag |= Err;

src/renderers/shared/fiber/ReactFiberClassComponent.js

Lines changed: 18 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,9 @@ var invariant = require('invariant');
4040

4141
const isArray = Array.isArray;
4242

43+
const internalErrorMessage =
44+
'This error is likely caused by a bug in React. Please file an issue.';
45+
4346
module.exports = function(
4447
scheduleUpdate : (fiber : Fiber, priorityLevel : PriorityLevel) => void,
4548
getPriorityContext : () => PriorityLevel,
@@ -257,9 +260,11 @@ module.exports = function(
257260
const state = instance.state || null;
258261

259262
let props = workInProgress.pendingProps;
260-
if (!props) {
261-
throw new Error('There must be pending props for an initial mount.');
262-
}
263+
invariant(
264+
props,
265+
'There must be pending props for an initial mount. (%s)',
266+
internalErrorMessage
267+
);
263268

264269
const unmaskedContext = getUnmaskedContext(workInProgress);
265270

@@ -298,9 +303,11 @@ module.exports = function(
298303
// If there isn't any new props, then we'll reuse the memoized props.
299304
// This could be from already completed work.
300305
newProps = workInProgress.memoizedProps;
301-
if (!newProps) {
302-
throw new Error('There should always be pending or memoized props.');
303-
}
306+
invariant(
307+
newProps != null,
308+
'There should always be pending or memoized props. (%s)',
309+
internalErrorMessage
310+
);
304311
}
305312
const newUnmaskedContext = getUnmaskedContext(workInProgress);
306313
const newContext = getMaskedContext(workInProgress, newUnmaskedContext);
@@ -363,9 +370,11 @@ module.exports = function(
363370
// If there aren't any new props, then we'll reuse the memoized props.
364371
// This could be from already completed work.
365372
newProps = oldProps;
366-
if (!newProps) {
367-
throw new Error('There should always be pending or memoized props.');
368-
}
373+
invariant(
374+
newProps != null,
375+
'There should always be pending or memoized props. (%s)',
376+
internalErrorMessage
377+
);
369378
}
370379
const oldContext = instance.context;
371380
const newUnmaskedContext = getUnmaskedContext(workInProgress);

src/renderers/shared/fiber/ReactFiberCommitWork.js

Lines changed: 39 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,11 @@ var {
3434
ContentReset,
3535
} = require('ReactTypeOfSideEffect');
3636

37+
var invariant = require('invariant');
38+
39+
const internalErrorMessage =
40+
'This error is likely caused by a bug in React. Please file an issue.';
41+
3742
module.exports = function<T, P, I, TI, PI, C, CX, PL>(
3843
config : HostConfig<T, P, I, TI, PI, C, CX, PL>,
3944
captureError : (failedFiber : Fiber, error: Error) => ?Fiber
@@ -94,7 +99,11 @@ module.exports = function<T, P, I, TI, PI, C, CX, PL>(
9499
}
95100
parent = parent.return;
96101
}
97-
throw new Error('Expected to find a host parent.');
102+
invariant(
103+
false,
104+
'Expected to find a host parent. (%s)',
105+
internalErrorMessage
106+
);
98107
}
99108

100109
function getHostParentFiber(fiber : Fiber) : Fiber {
@@ -105,7 +114,11 @@ module.exports = function<T, P, I, TI, PI, C, CX, PL>(
105114
}
106115
parent = parent.return;
107116
}
108-
throw new Error('Expected to find a host parent.');
117+
invariant(
118+
false,
119+
'Expected to find a host parent. (%s)',
120+
internalErrorMessage
121+
);
109122
}
110123

111124
function isHostParent(fiber : Fiber) : boolean {
@@ -173,7 +186,11 @@ module.exports = function<T, P, I, TI, PI, C, CX, PL>(
173186
parent = parentFiber.stateNode.containerInfo;
174187
break;
175188
default:
176-
throw new Error('Invalid host parent fiber.');
189+
invariant(
190+
false,
191+
'Invalid host parent fiber. (%s)',
192+
internalErrorMessage
193+
);
177194
}
178195
if (parentFiber.effectTag & ContentReset) {
179196
// Reset the text content of the parent before doing any insertions
@@ -374,9 +391,11 @@ module.exports = function<T, P, I, TI, PI, C, CX, PL>(
374391
return;
375392
}
376393
case HostText: {
377-
if (finishedWork.stateNode == null || !current) {
378-
throw new Error('This should only be done during updates.');
379-
}
394+
invariant(
395+
finishedWork.stateNode !== null && current != null,
396+
'This should only be done during updates. (%s)',
397+
internalErrorMessage
398+
);
380399
const textInstance : TI = finishedWork.stateNode;
381400
const newText : string = finishedWork.memoizedProps;
382401
const oldText : string = current.memoizedProps;
@@ -389,8 +408,13 @@ module.exports = function<T, P, I, TI, PI, C, CX, PL>(
389408
case HostPortal: {
390409
return;
391410
}
392-
default:
393-
throw new Error('This unit of work tag should not have side-effects.');
411+
default: {
412+
invariant(
413+
false,
414+
'This unit of work tag should not have side-effects. (%s)',
415+
internalErrorMessage
416+
);
417+
}
394418
}
395419
}
396420

@@ -450,8 +474,13 @@ module.exports = function<T, P, I, TI, PI, C, CX, PL>(
450474
// We have no life-cycles associated with portals.
451475
return;
452476
}
453-
default:
454-
throw new Error('This unit of work tag should not have side-effects.');
477+
default: {
478+
invariant(
479+
false,
480+
'This unit of work tag should not have side-effects. (%s)',
481+
internalErrorMessage
482+
);
483+
}
455484
}
456485
}
457486

0 commit comments

Comments
 (0)