-
Notifications
You must be signed in to change notification settings - Fork 49.2k
[Fiber] Replace throw new Error
with invariant module
#8926
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -72,6 +72,9 @@ const { | |
Deletion, | ||
} = ReactTypeOfSideEffect; | ||
|
||
const internalErrorMessage = | ||
'This error is likely caused by a bug in React. Please file an issue.'; | ||
|
||
function coerceRef(current: ?Fiber, element: ReactElement) { | ||
let mixedRef = element.ref; | ||
if (mixedRef != null && typeof mixedRef !== 'function') { | ||
|
@@ -88,7 +91,11 @@ function coerceRef(current: ?Fiber, element: ReactElement) { | |
inst = (owner : any).getPublicInstance(); | ||
} | ||
} | ||
invariant(inst, 'Missing owner for string ref %s', mixedRef); | ||
invariant( | ||
inst, 'Missing owner for string ref %s. (%s)', | ||
mixedRef, | ||
internalErrorMessage | ||
); | ||
const stringRef = String(mixedRef); | ||
// Check if previous string ref matches new string ref | ||
if (current && current.ref && current.ref._stringRef === stringRef) { | ||
|
@@ -797,29 +804,31 @@ function ChildReconciler(shouldClone, shouldTrackSideEffects) { | |
// but using the iterator instead. | ||
|
||
const iteratorFn = getIteratorFn(newChildrenIterable); | ||
if (typeof iteratorFn !== 'function') { | ||
throw new Error('An object is not an iterable.'); | ||
} | ||
invariant( | ||
typeof iteratorFn === 'function', | ||
'An object is not an iterable. (%s)', | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Not sure we can count this as an internal error? The object is supplied by the user code. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Yep, my bad There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I think this is correct because if it's not an iterator then we shouldn't have reached this code path. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Unless user specified code returns something different the second time we read There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. It could be a getter. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. That check doesn't check the type of the iterator. |
||
internalErrorMessage | ||
); | ||
|
||
if (__DEV__) { | ||
// First, validate keys. | ||
// We'll get a different iterator later for the main pass. | ||
const newChildren = iteratorFn.call(newChildrenIterable); | ||
if (newChildren == null) { | ||
throw new Error('An iterable object provided no iterator.'); | ||
} | ||
let knownKeys = null; | ||
let step = newChildren.next(); | ||
for (; !step.done; step = newChildren.next()) { | ||
const child = step.value; | ||
knownKeys = warnOnDuplicateKey(child, knownKeys); | ||
if (newChildren) { | ||
let knownKeys = null; | ||
let step = newChildren.next(); | ||
for (; !step.done; step = newChildren.next()) { | ||
const child = step.value; | ||
knownKeys = warnOnDuplicateKey(child, knownKeys); | ||
} | ||
} | ||
} | ||
|
||
const newChildren = iteratorFn.call(newChildrenIterable); | ||
if (newChildren == null) { | ||
throw new Error('An iterable object provided no iterator.'); | ||
} | ||
invariant( | ||
newChildren != null, | ||
'An iterable object provided no iterator.', | ||
); | ||
|
||
let resultingFirstChild : ?Fiber = null; | ||
let previousNewFiber : ?Fiber = null; | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This will count against the byte size, is this intentional?
It doesn’t matter that much in the grand scheme of things, just wondering if this was intended or not.
If you just hardcoded it everywhere then it would get stripped out.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Hrrm, didn't think about that. Don't think it makes a substantial difference but we can change later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I'd prefer we do it now so it (or similar patterns) don't keep spreading.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Me too, let's inline please.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Yes plz.