Skip to content

Commit a889e82

Browse files
committed
[Automated] Codemod invariant -> Error
This commit contains only automated changes: npx jscodeshift -t scripts/codemod-invariant.js packages --ignore-pattern="node_modules/**/*" yarn linc --fix yarn prettier I will do any manual touch ups in separate commits so they're easier to review.
1 parent 05a554f commit a889e82

File tree

100 files changed

+1593
-1462
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

100 files changed

+1593
-1462
lines changed

packages/create-subscription/src/createSubscription.js

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,6 @@
88
*/
99

1010
import * as React from 'react';
11-
import invariant from 'shared/invariant';
1211

1312
type Unsubscribe = () => void;
1413

@@ -128,10 +127,12 @@ export function createSubscription<Property, Value>(
128127

129128
// Store the unsubscribe method for later (in case the subscribable prop changes).
130129
const unsubscribe = subscribe(source, callback);
131-
invariant(
132-
typeof unsubscribe === 'function',
133-
'A subscription must return an unsubscribe function.',
134-
);
130+
131+
if (typeof unsubscribe !== 'function') {
132+
throw new Error(
133+
'A subscription must return an unsubscribe function.',
134+
);
135+
}
135136

136137
// It's safe to store unsubscribe on the instance because
137138
// We only read or write that property during the "commit" phase.

packages/react-art/src/ReactARTHostConfig.js

Lines changed: 13 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,6 @@
77

88
import Transform from 'art/core/transform';
99
import Mode from 'art/modes/current';
10-
import invariant from 'shared/invariant';
1110

1211
import {TYPES, EVENT_TYPES, childrenAsString} from './ReactARTInternals';
1312

@@ -248,7 +247,8 @@ export * from 'react-reconciler/src/ReactFiberHostConfigWithNoMicrotasks';
248247
export function appendInitialChild(parentInstance, child) {
249248
if (typeof child === 'string') {
250249
// Noop for string children of Text (eg <Text>{'foo'}{'bar'}</Text>)
251-
invariant(false, 'Text children should already be flattened.');
250+
throw new Error('Text children should already be flattened.');
251+
252252
return;
253253
}
254254

@@ -282,7 +282,9 @@ export function createInstance(type, props, internalInstanceHandle) {
282282
break;
283283
}
284284

285-
invariant(instance, 'ReactART does not support the type "%s"', type);
285+
if (!instance) {
286+
throw new Error(`ReactART does not support the type "${type}"`);
287+
}
286288

287289
instance._applyProps(instance, props);
288290

@@ -367,18 +369,18 @@ export function appendChildToContainer(parentInstance, child) {
367369
}
368370

369371
export function insertBefore(parentInstance, child, beforeChild) {
370-
invariant(
371-
child !== beforeChild,
372-
'ReactART: Can not insert node before itself',
373-
);
372+
if (child === beforeChild) {
373+
throw new Error('ReactART: Can not insert node before itself');
374+
}
375+
374376
child.injectBefore(beforeChild);
375377
}
376378

377379
export function insertInContainerBefore(parentInstance, child, beforeChild) {
378-
invariant(
379-
child !== beforeChild,
380-
'ReactART: Can not insert node before itself',
381-
);
380+
if (child === beforeChild) {
381+
throw new Error('ReactART: Can not insert node before itself');
382+
}
383+
382384
child.injectBefore(beforeChild);
383385
}
384386

packages/react-client/src/ReactFlightClientHostConfig.js

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -9,8 +9,6 @@
99

1010
/* eslint-disable react-internal/invariant-args */
1111

12-
import invariant from 'shared/invariant';
13-
1412
// We expect that our Rollup, Jest, and Flow configurations
1513
// always shim this module with the corresponding host config
1614
// (either provided by a renderer, or a generic shim for npm).
@@ -19,4 +17,4 @@ import invariant from 'shared/invariant';
1917
// sure that if we *do* accidentally break the configuration,
2018
// the failure isn't silent.
2119

22-
invariant(false, 'This module must be shimmed by a specific renderer.');
20+
throw new Error('This module must be shimmed by a specific renderer.');

packages/react-debug-tools/src/ReactDebugHooks.js

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,6 @@ import type {OpaqueIDType} from 'react-reconciler/src/ReactFiberHostConfig';
2323
import {NoMode} from 'react-reconciler/src/ReactTypeOfMode';
2424

2525
import ErrorStackParser from 'error-stack-parser';
26-
import invariant from 'shared/invariant';
2726
import ReactSharedInternals from 'shared/ReactSharedInternals';
2827
import {REACT_OPAQUE_ID_TYPE} from 'shared/ReactSymbols';
2928
import {
@@ -107,7 +106,7 @@ function nextHook(): null | Hook {
107106
}
108107

109108
function getCacheForType<T>(resourceType: () => T): T {
110-
invariant(false, 'Not implemented.');
109+
throw new Error('Not implemented.');
111110
}
112111

113112
function readContext<T>(context: ReactContext<T>): T {

packages/react-dom/src/client/ReactDOM.js

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,6 @@ import {
3939
import {createPortal as createPortalImpl} from 'react-reconciler/src/ReactPortal';
4040
import {canUseDOM} from 'shared/ExecutionEnvironment';
4141
import ReactVersion from 'shared/ReactVersion';
42-
import invariant from 'shared/invariant';
4342
import {
4443
warnUnstableRenderSubtreeIntoContainer,
4544
enableNewReconciler,
@@ -108,10 +107,10 @@ function createPortal(
108107
container: Container,
109108
key: ?string = null,
110109
): React$Portal {
111-
invariant(
112-
isValidContainer(container),
113-
'Target container is not a DOM element.',
114-
);
110+
if (!isValidContainer(container)) {
111+
throw new Error('Target container is not a DOM element.');
112+
}
113+
115114
// TODO: pass ReactDOM portal implementation as third argument
116115
// $FlowFixMe The Flow type is opaque but there's no way to actually create it.
117116
return createPortalImpl(children, container, null, key);

packages/react-dom/src/client/ReactDOMComponentTree.js

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,6 @@ import {
3030

3131
import {getParentSuspenseInstance} from './ReactDOMHostConfig';
3232

33-
import invariant from 'shared/invariant';
3433
import {enableScopeAPI} from 'shared/ReactFeatureFlags';
3534

3635
const randomKey = Math.random()
@@ -190,7 +189,7 @@ export function getNodeFromInstance(inst: Fiber): Instance | TextInstance {
190189

191190
// Without this first invariant, passing a non-DOM-component triggers the next
192191
// invariant for a missing parent, which is super confusing.
193-
invariant(false, 'getNodeFromInstance: Invalid argument.');
192+
throw new Error('getNodeFromInstance: Invalid argument.');
194193
}
195194

196195
export function getFiberCurrentPropsFromNode(

packages/react-dom/src/client/ReactDOMEventHandle.js

Lines changed: 13 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,6 @@ import {
2828
enableScopeAPI,
2929
enableCreateEventHandleAPI,
3030
} from 'shared/ReactFeatureFlags';
31-
import invariant from 'shared/invariant';
3231

3332
type EventHandleOptions = {|
3433
capture?: boolean,
@@ -73,8 +72,7 @@ function registerReactDOMEvent(
7372
eventTarget,
7473
);
7574
} else {
76-
invariant(
77-
false,
75+
throw new Error(
7876
'ReactDOM.createEventHandle: setter called on an invalid ' +
7977
'target. Provide a valid EventTarget or an element managed by React.',
8078
);
@@ -97,11 +95,11 @@ export function createEventHandle(
9795
// Unfortunately, the downside of this invariant is that *removing* a native
9896
// event from the list of known events has now become a breaking change for
9997
// any code relying on the createEventHandle API.
100-
invariant(
101-
allNativeEvents.has(domEventName),
102-
'Cannot call unstable_createEventHandle with "%s", as it is not an event known to React.',
103-
domEventName,
104-
);
98+
if (!allNativeEvents.has(domEventName)) {
99+
throw new Error(
100+
`Cannot call unstable_createEventHandle with "${domEventName}", as it is not an event known to React.`,
101+
);
102+
}
105103

106104
let isCapturePhaseListener = false;
107105
if (options != null) {
@@ -115,11 +113,13 @@ export function createEventHandle(
115113
target: EventTarget | ReactScopeInstance,
116114
callback: (SyntheticEvent<EventTarget>) => void,
117115
) => {
118-
invariant(
119-
typeof callback === 'function',
120-
'ReactDOM.createEventHandle: setter called with an invalid ' +
121-
'callback. The callback must be a function.',
122-
);
116+
if (typeof callback !== 'function') {
117+
throw new Error(
118+
'ReactDOM.createEventHandle: setter called with an invalid ' +
119+
'callback. The callback must be a function.',
120+
);
121+
}
122+
123123
if (!doesTargetHaveEventHandle(target, eventHandle)) {
124124
addEventHandleToTarget(target, eventHandle);
125125
registerReactDOMEvent(target, domEventName, isCapturePhaseListener);

packages/react-dom/src/client/ReactDOMInput.js

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,6 @@
99

1010
// TODO: direct imports like some-package/src/* are bad. Fix me.
1111
import {getCurrentFiberOwnerNameInDevOrNull} from 'react-reconciler/src/ReactCurrentFiber';
12-
import invariant from 'shared/invariant';
1312

1413
import {setValueForProperty} from './DOMPropertyOperations';
1514
import {getFiberCurrentPropsFromNode} from './ReactDOMComponentTree';
@@ -379,11 +378,13 @@ function updateNamedCousins(rootNode, props) {
379378
// That's probably okay; we don't support it just as we don't support
380379
// mixing React radio buttons with non-React ones.
381380
const otherProps = getFiberCurrentPropsFromNode(otherNode);
382-
invariant(
383-
otherProps,
384-
'ReactDOMInput: Mixing React and non-React radio inputs with the ' +
385-
'same `name` is not supported.',
386-
);
381+
382+
if (!otherProps) {
383+
throw new Error(
384+
'ReactDOMInput: Mixing React and non-React radio inputs with the ' +
385+
'same `name` is not supported.',
386+
);
387+
}
387388

388389
// We need update the tracked value on the named cousin since the value
389390
// was changed but the input saw no event or value set

packages/react-dom/src/client/ReactDOMLegacy.js

Lines changed: 21 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,6 @@ import {
3636
} from 'react-reconciler/src/ReactFiberReconciler';
3737
import {LegacyRoot} from 'react-reconciler/src/ReactRootTags';
3838
import getComponentNameFromType from 'shared/getComponentNameFromType';
39-
import invariant from 'shared/invariant';
4039
import ReactSharedInternals from 'shared/ReactSharedInternals';
4140
import {has as hasInstance} from 'shared/ReactInstanceMap';
4241

@@ -238,10 +237,10 @@ export function hydrate(
238237
);
239238
}
240239

241-
invariant(
242-
isValidContainerLegacy(container),
243-
'Target container is not a DOM element.',
244-
);
240+
if (!isValidContainerLegacy(container)) {
241+
throw new Error('Target container is not a DOM element.');
242+
}
243+
245244
if (__DEV__) {
246245
const isModernRoot =
247246
isContainerMarkedAsRoot(container) &&
@@ -278,10 +277,10 @@ export function render(
278277
);
279278
}
280279

281-
invariant(
282-
isValidContainerLegacy(container),
283-
'Target container is not a DOM element.',
284-
);
280+
if (!isValidContainerLegacy(container)) {
281+
throw new Error('Target container is not a DOM element.');
282+
}
283+
285284
if (__DEV__) {
286285
const isModernRoot =
287286
isContainerMarkedAsRoot(container) &&
@@ -309,14 +308,14 @@ export function unstable_renderSubtreeIntoContainer(
309308
containerNode: Container,
310309
callback: ?Function,
311310
) {
312-
invariant(
313-
isValidContainerLegacy(containerNode),
314-
'Target container is not a DOM element.',
315-
);
316-
invariant(
317-
parentComponent != null && hasInstance(parentComponent),
318-
'parentComponent must be a valid React Component',
319-
);
311+
if (!isValidContainerLegacy(containerNode)) {
312+
throw new Error('Target container is not a DOM element.');
313+
}
314+
315+
if (parentComponent == null || !hasInstance(parentComponent)) {
316+
throw new Error('parentComponent must be a valid React Component');
317+
}
318+
320319
return legacyRenderSubtreeIntoContainer(
321320
parentComponent,
322321
element,
@@ -327,10 +326,11 @@ export function unstable_renderSubtreeIntoContainer(
327326
}
328327

329328
export function unmountComponentAtNode(container: Container) {
330-
invariant(
331-
isValidContainerLegacy(container),
332-
'unmountComponentAtNode(...): Target container is not a DOM element.',
333-
);
329+
if (!isValidContainerLegacy(container)) {
330+
throw new Error(
331+
'unmountComponentAtNode(...): Target container is not a DOM element.',
332+
);
333+
}
334334

335335
if (__DEV__) {
336336
const isModernRoot =

packages/react-dom/src/client/ReactDOMRoot.js

Lines changed: 8 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -63,7 +63,6 @@ import {
6363
findHostInstanceWithNoPortals,
6464
registerMutableSourceForHydration,
6565
} from 'react-reconciler/src/ReactFiberReconciler';
66-
import invariant from 'shared/invariant';
6766
import {ConcurrentRoot} from 'react-reconciler/src/ReactRootTags';
6867
import {allowConcurrentByDefault} from 'shared/ReactFeatureFlags';
6968

@@ -119,10 +118,10 @@ export function createRoot(
119118
container: Container,
120119
options?: CreateRootOptions,
121120
): RootType {
122-
invariant(
123-
isValidContainerLegacy(container),
124-
'createRoot(...): Target container is not a DOM element.',
125-
);
121+
if (!isValidContainerLegacy(container)) {
122+
throw new Error('createRoot(...): Target container is not a DOM element.');
123+
}
124+
126125
warnIfReactDOMContainerInDEV(container);
127126

128127
// TODO: Delete these options
@@ -176,10 +175,10 @@ export function hydrateRoot(
176175
initialChildren: ReactNodeList,
177176
options?: HydrateRootOptions,
178177
): RootType {
179-
invariant(
180-
isValidContainer(container),
181-
'hydrateRoot(...): Target container is not a DOM element.',
182-
);
178+
if (!isValidContainer(container)) {
179+
throw new Error('hydrateRoot(...): Target container is not a DOM element.');
180+
}
181+
183182
warnIfReactDOMContainerInDEV(container);
184183

185184
// For now we reuse the whole bag of options since they contain

0 commit comments

Comments
 (0)