GeezerVison is showing github.com — [RFC] Codemod invariant -> throw new Error (#22435) · react/react@a724a3b
File treeExpand file treeCollapse file tree Open diff view settings react-native-renderer/src __mocks__/react-native/Libraries/ReactPrivate
react-suspense-test-utils/src
Some content is hiddenLarge Commits have some content hidden by default. Use the searchbox below for content that may be hidden. Dismiss bannerExpand file treeCollapse file tree Open diff view settings Collapse file Copy file name to clipboardExpand all lines: .eslintrc.js +35Lines changed: 35 additions & 0 deletions | Original file line number | Diff line number | Diff line change |
|---|
@@ -127,6 +127,41 @@ module.exports = { | 127 | 127 | }, | 128 | 128 |
| 129 | 129 | overrides: [ | | 130 | + { | | 131 | + // By default, anything error message that appears the packages directory | | 132 | + // must have a corresponding error code. The exceptions are defined | | 133 | + // in the next override entry. | | 134 | + files: ['packages/**/*.js'], | | 135 | + rules: { | | 136 | + 'react-internal/prod-error-codes': ERROR, | | 137 | + }, | | 138 | + }, | | 139 | + { | | 140 | + // These are files where it's OK to have unminified error messages. These | | 141 | + // are environments where bundle size isn't a concern, like tests | | 142 | + // or Node. | | 143 | + files: [ | | 144 | + 'packages/react-dom/src/test-utils/**/*.js', | | 145 | + 'packages/react-devtools-shared/**/*.js', | | 146 | + 'packages/react-noop-renderer/**/*.js', | | 147 | + 'packages/react-pg/**/*.js', | | 148 | + 'packages/react-fs/**/*.js', | | 149 | + 'packages/react-refresh/**/*.js', | | 150 | + 'packages/react-server-dom-webpack/**/*.js', | | 151 | + 'packages/react-test-renderer/**/*.js', | | 152 | + 'packages/react-debug-tools/**/*.js', | | 153 | + 'packages/react-devtools-extensions/**/*.js', | | 154 | + 'packages/react-devtools-scheduling-profiler/**/*.js', | | 155 | + 'packages/react-native-renderer/**/*.js', | | 156 | + 'packages/eslint-plugin-react-hooks/**/*.js', | | 157 | + 'packages/jest-react/**/*.js', | | 158 | + 'packages/**/__tests__/*.js', | | 159 | + 'packages/**/npm/*.js', | | 160 | + ], | | 161 | + rules: { | | 162 | + 'react-internal/prod-error-codes': OFF, | | 163 | + }, | | 164 | + }, | 130 | 165 | { | 131 | 166 | // We apply these settings to files that we ship through npm. | 132 | 167 | // They must be ES5. | |
Collapse file Copy file name to clipboardExpand all lines: packages/create-subscription/src/createSubscription.js +6-5Lines changed: 6 additions & 5 deletions | Original file line number | Diff line number | Diff line change |
|---|
| 8 | 8 | */ | 9 | 9 |
| 10 | 10 | import * as React from 'react'; | 11 | | -import invariant from 'shared/invariant'; | 12 | 11 |
| 13 | 12 | type Unsubscribe = () => void; | 14 | 13 |
| @@ -128,10 +127,12 @@ export function createSubscription<Property, Value>( | 128 | 127 |
| 129 | 128 | // Store the unsubscribe method for later (in case the subscribable prop changes). | 130 | 129 | 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 | + } | 135 | 136 |
| 136 | 137 | // It's safe to store unsubscribe on the instance because | 137 | 138 | // We only read or write that property during the "commit" phase. | |
Collapse file Copy file name to clipboardExpand all lines: packages/jest-react/src/JestReact.js +6-6Lines changed: 6 additions & 6 deletions | Original file line number | Diff line number | Diff line change |
|---|
| 7 | 7 |
| 8 | 8 | import {REACT_ELEMENT_TYPE, REACT_FRAGMENT_TYPE} from 'shared/ReactSymbols'; | 9 | 9 |
| 10 | | -import invariant from 'shared/invariant'; | 11 | 10 | import isArray from 'shared/isArray'; | 12 | 11 |
| 13 | 12 | export {act} from './internalAct'; | @@ -31,11 +30,12 @@ function captureAssertion(fn) { | 31 | 30 | function assertYieldsWereCleared(root) { | 32 | 31 | const Scheduler = root._Scheduler; | 33 | 32 | const actualYields = Scheduler.unstable_clearYields(); | 34 | | - invariant( | 35 | | - actualYields.length === 0, | 36 | | - 'Log of yielded values is not empty. ' + | 37 | | - 'Call expect(ReactTestRenderer).unstable_toHaveYielded(...) first.', | 38 | | - ); | | 33 | + if (actualYields.length !== 0) { | | 34 | + throw new Error( | | 35 | + 'Log of yielded values is not empty. ' + | | 36 | + 'Call expect(ReactTestRenderer).unstable_toHaveYielded(...) first.', | | 37 | + ); | | 38 | + } | 39 | 39 | } | 40 | 40 |
| 41 | 41 | export function unstable_toMatchRenderedOutput(root, expectedJSX) { | |
Collapse file Copy file name to clipboardExpand all lines: packages/react-art/src/ReactARTHostConfig.js +17-17Lines changed: 17 additions & 17 deletions | Original file line number | Diff line number | Diff line change |
|---|
| 7 | 7 |
| 8 | 8 | import Transform from 'art/core/transform'; | 9 | 9 | import Mode from 'art/modes/current'; | 10 | | -import invariant from 'shared/invariant'; | 11 | 10 |
| 12 | 11 | import {TYPES, EVENT_TYPES, childrenAsString} from './ReactARTInternals'; | 13 | 12 |
| @@ -248,8 +247,7 @@ export * from 'react-reconciler/src/ReactFiberHostConfigWithNoMicrotasks'; | 248 | 247 | export function appendInitialChild(parentInstance, child) { | 249 | 248 | if (typeof child === 'string') { | 250 | 249 | // Noop for string children of Text (eg <Text>{'foo'}{'bar'}</Text>) | 251 | | - invariant(false, 'Text children should already be flattened.'); | 252 | | - return; | | 250 | + throw new Error('Text children should already be flattened.'); | 253 | 251 | } | 254 | 252 |
| 255 | 253 | child.inject(parentInstance); | @@ -282,7 +280,9 @@ export function createInstance(type, props, internalInstanceHandle) { | 282 | 280 | break; | 283 | 281 | } | 284 | 282 |
| 285 | | - invariant(instance, 'ReactART does not support the type "%s"', type); | | 283 | + if (!instance) { | | 284 | + throw new Error(`ReactART does not support the type "${type}"`); | | 285 | + } | 286 | 286 |
| 287 | 287 | instance._applyProps(instance, props); | 288 | 288 |
| @@ -367,18 +367,18 @@ export function appendChildToContainer(parentInstance, child) { | 367 | 367 | } | 368 | 368 |
| 369 | 369 | export function insertBefore(parentInstance, child, beforeChild) { | 370 | | - invariant( | 371 | | - child !== beforeChild, | 372 | | - 'ReactART: Can not insert node before itself', | 373 | | - ); | | 370 | + if (child === beforeChild) { | | 371 | + throw new Error('ReactART: Can not insert node before itself'); | | 372 | + } | | 373 | + | 374 | 374 | child.injectBefore(beforeChild); | 375 | 375 | } | 376 | 376 |
| 377 | 377 | export function insertInContainerBefore(parentInstance, child, beforeChild) { | 378 | | - invariant( | 379 | | - child !== beforeChild, | 380 | | - 'ReactART: Can not insert node before itself', | 381 | | - ); | | 378 | + if (child === beforeChild) { | | 379 | + throw new Error('ReactART: Can not insert node before itself'); | | 380 | + } | | 381 | + | 382 | 382 | child.injectBefore(beforeChild); | 383 | 383 | } | 384 | 384 |
| @@ -433,25 +433,25 @@ export function clearContainer(container) { | 433 | 433 | } | 434 | 434 |
| 435 | 435 | export function getInstanceFromNode(node) { | 436 | | - throw new Error('Not yet implemented.'); | | 436 | + throw new Error('Not implemented.'); | 437 | 437 | } | 438 | 438 |
| 439 | 439 | export function isOpaqueHydratingObject(value: mixed): boolean { | 440 | | - throw new Error('Not yet implemented'); | | 440 | + throw new Error('Not implemented.'); | 441 | 441 | } | 442 | 442 |
| 443 | 443 | export function makeOpaqueHydratingObject( | 444 | 444 | attemptToReadValue: () => void, | 445 | 445 | ): OpaqueIDType { | 446 | | - throw new Error('Not yet implemented.'); | | 446 | + throw new Error('Not implemented.'); | 447 | 447 | } | 448 | 448 |
| 449 | 449 | export function makeClientId(): OpaqueIDType { | 450 | | - throw new Error('Not yet implemented'); | | 450 | + throw new Error('Not implemented.'); | 451 | 451 | } | 452 | 452 |
| 453 | 453 | export function makeClientIdInDEV(warnOnAccessInDEV: () => void): OpaqueIDType { | 454 | | - throw new Error('Not yet implemented'); | | 454 | + throw new Error('Not implemented.'); | 455 | 455 | } | 456 | 456 |
| 457 | 457 | export function beforeActiveInstanceBlur(internalInstanceHandle: Object) { | |
Collapse file Copy file name to clipboardExpand all lines: packages/react-cache/src/ReactCacheOld.js +2Lines changed: 2 additions & 0 deletions | Original file line number | Diff line number | Diff line change |
|---|
@@ -49,6 +49,8 @@ const ReactCurrentDispatcher = | 49 | 49 | function readContext(Context) { | 50 | 50 | const dispatcher = ReactCurrentDispatcher.current; | 51 | 51 | if (dispatcher === null) { | | 52 | + // This wasn't being minified but we're going to retire this package anyway. | | 53 | + // eslint-disable-next-line react-internal/prod-error-codes | 52 | 54 | throw new Error( | 53 | 55 | 'react-cache: read and preload may only be called from within a ' + | 54 | 56 | "component's render. They are not supported in event handlers or " + | |
Collapse file Copy file name to clipboardExpand all lines: packages/react-client/src/ReactFlightClient.js +1Lines changed: 1 addition & 0 deletions | Original file line number | Diff line number | Diff line change |
|---|
@@ -397,6 +397,7 @@ export function resolveError( | 397 | 397 | message: string, | 398 | 398 | stack: string, | 399 | 399 | ): void { | | 400 | + // eslint-disable-next-line react-internal/prod-error-codes | 400 | 401 | const error = new Error(message); | 401 | 402 | error.stack = stack; | 402 | 403 | const chunks = response._chunks; | |
Collapse file Copy file name to clipboardExpand all lines: packages/react-client/src/ReactFlightClientHostConfig.js +2-4Lines changed: 2 additions & 4 deletions | Original file line number | Diff line number | Diff line change |
|---|
| 7 | 7 | * @flow | 8 | 8 | */ | 9 | 9 |
| 10 | | -/* eslint-disable react-internal/invariant-args */ | 11 | | - | 12 | | -import invariant from 'shared/invariant'; | | 10 | +/* eslint-disable react-internal/prod-error-codes */ | 13 | 11 |
| 14 | 12 | // We expect that our Rollup, Jest, and Flow configurations | 15 | 13 | // always shim this module with the corresponding host config | @@ -19,4 +17,4 @@ import invariant from 'shared/invariant'; | 19 | 17 | // sure that if we *do* accidentally break the configuration, | 20 | 18 | // the failure isn't silent. | 21 | 19 |
| 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.'); |
Collapse file Copy file name to clipboardExpand all lines: packages/react-client/src/ReactFlightClientHostConfigNoStream.js +3Lines changed: 3 additions & 0 deletions | Original file line number | Diff line number | Diff line change |
|---|
@@ -12,19 +12,22 @@ export type StringDecoder = void; | 12 | 12 | export const supportsBinaryStreams = false; | 13 | 13 |
| 14 | 14 | export function createStringDecoder(): void { | | 15 | + // eslint-disable-next-line react-internal/prod-error-codes | 15 | 16 | throw new Error('Should never be called'); | 16 | 17 | } | 17 | 18 |
| 18 | 19 | export function readPartialStringChunk( | 19 | 20 | decoder: StringDecoder, | 20 | 21 | buffer: Uint8Array, | 21 | 22 | ): string { | | 23 | + // eslint-disable-next-line react-internal/prod-error-codes | 22 | 24 | throw new Error('Should never be called'); | 23 | 25 | } | 24 | 26 |
| 25 | 27 | export function readFinalStringChunk( | 26 | 28 | decoder: StringDecoder, | 27 | 29 | buffer: Uint8Array, | 28 | 30 | ): string { | | 31 | + // eslint-disable-next-line react-internal/prod-error-codes | 29 | 32 | throw new Error('Should never be called'); | 30 | 33 | } |
Collapse file Copy file name to clipboardExpand all lines: packages/react-debug-tools/src/ReactDebugHooks.js +1-2Lines changed: 1 addition & 2 deletions | Original file line number | Diff line number | Diff line change |
|---|
@@ -23,7 +23,6 @@ import type {OpaqueIDType} from 'react-reconciler/src/ReactFiberHostConfig'; | 23 | 23 | import {NoMode} from 'react-reconciler/src/ReactTypeOfMode'; | 24 | 24 |
| 25 | 25 | import ErrorStackParser from 'error-stack-parser'; | 26 | | -import invariant from 'shared/invariant'; | 27 | 26 | import ReactSharedInternals from 'shared/ReactSharedInternals'; | 28 | 27 | import {REACT_OPAQUE_ID_TYPE} from 'shared/ReactSymbols'; | 29 | 28 | import { | @@ -107,7 +106,7 @@ function nextHook(): null | Hook { | 107 | 106 | } | 108 | 107 |
| 109 | 108 | function getCacheForType<T>(resourceType: () => T): T { | 110 | | - invariant(false, 'Not implemented.'); | | 109 | + throw new Error('Not implemented.'); | 111 | 110 | } | 112 | 111 |
| 113 | 112 | function readContext<T>(context: ReactContext<T>): T { | |
Collapse file Copy file name to clipboardExpand all lines: packages/react-dom/src/client/ReactDOM.js +4-5Lines changed: 4 additions & 5 deletions | Original file line number | Diff line number | Diff line change |
|---|
@@ -39,7 +39,6 @@ import { | 39 | 39 | import {createPortal as createPortalImpl} from 'react-reconciler/src/ReactPortal'; | 40 | 40 | import {canUseDOM} from 'shared/ExecutionEnvironment'; | 41 | 41 | import ReactVersion from 'shared/ReactVersion'; | 42 | | -import invariant from 'shared/invariant'; | 43 | 42 | import { | 44 | 43 | warnUnstableRenderSubtreeIntoContainer, | 45 | 44 | enableNewReconciler, | @@ -108,10 +107,10 @@ function createPortal( | 108 | 107 | container: Container, | 109 | 108 | key: ?string = null, | 110 | 109 | ): 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 | + | 115 | 114 | // TODO: pass ReactDOM portal implementation as third argument | 116 | 115 | // $FlowFixMe The Flow type is opaque but there's no way to actually create it. | 117 | 116 | return createPortalImpl(children, container, null, key); | |
|