Skip to content

refactoring: warning revamp part 2 #17081

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

Closed
wants to merge 3 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
23 changes: 11 additions & 12 deletions packages/create-subscription/src/createSubscription.js
Original file line number Diff line number Diff line change
Expand Up @@ -36,14 +36,13 @@ export function createSubscription<Property, Value>(
}> {
const {getCurrentValue, subscribe} = config;

warningWithoutStack(
typeof getCurrentValue === 'function',
'Subscription must specify a getCurrentValue function',
);
warningWithoutStack(
typeof subscribe === 'function',
'Subscription must specify a subscribe function',
);
if (!(typeof getCurrentValue === 'function')) {
warningWithoutStack('Subscription must specify a getCurrentValue function');
}

if (!(typeof subscribe === 'function')) {
warningWithoutStack('Subscription must specify a subscribe function');
}

type Props = {
children: (value: Value) => React$Element<any>,
Expand Down Expand Up @@ -129,10 +128,10 @@ export function createSubscription<Property, Value>(

// Store the unsubscribe method for later (in case the subscribable prop changes).
const unsubscribe = subscribe(source, callback);
invariant(
typeof unsubscribe === 'function',
'A subscription must return an unsubscribe function.',
);

if (!(typeof unsubscribe === 'function')) {
invariant('A subscription must return an unsubscribe function.');
}

// It's safe to store unsubscribe on the instance because
// We only read or write that property during the "commit" phase.
Expand Down
12 changes: 7 additions & 5 deletions packages/jest-react/src/JestReact.js
Original file line number Diff line number Diff line change
Expand Up @@ -28,11 +28,13 @@ function captureAssertion(fn) {
function assertYieldsWereCleared(root) {
const Scheduler = root._Scheduler;
const actualYields = Scheduler.unstable_clearYields();
invariant(
actualYields.length === 0,
'Log of yielded values is not empty. ' +
'Call expect(ReactTestRenderer).unstable_toHaveYielded(...) first.',
);

if (!(actualYields.length === 0)) {
invariant(
'Log of yielded values is not empty. ' +
'Call expect(ReactTestRenderer).unstable_toHaveYielded(...) first.',
);
}
}

export function unstable_toMatchRenderedOutput(root, expectedJSX) {
Expand Down
13 changes: 8 additions & 5 deletions packages/legacy-events/EventBatching.js
Original file line number Diff line number Diff line change
Expand Up @@ -56,11 +56,14 @@ export function runEventsInBatch(
}

forEachAccumulated(processingEventQueue, executeDispatchesAndReleaseTopLevel);
invariant(
!eventQueue,
'processEventQueue(): Additional events were enqueued while processing ' +
'an event queue. Support for this has not yet been implemented.',
);

if (eventQueue) {
invariant(
'processEventQueue(): Additional events were enqueued while processing ' +
'an event queue. Support for this has not yet been implemented.',
);
}

// This would be a good time to rethrow if any of the event handlers threw.
rethrowCaughtError();
}
15 changes: 9 additions & 6 deletions packages/legacy-events/EventPluginHub.js
Original file line number Diff line number Diff line change
Expand Up @@ -114,12 +114,15 @@ export function getListener(inst: Fiber, registrationName: string) {
if (shouldPreventMouseEvent(registrationName, inst.type, props)) {
return null;
}
invariant(
!listener || typeof listener === 'function',
'Expected `%s` listener to be a function, instead got a value of `%s` type.',
registrationName,
typeof listener,
);

if (!(!listener || typeof listener === 'function')) {
invariant(
'Expected `%s` listener to be a function, instead got a value of `%s` type.',
registrationName,
typeof listener,
);
}

return listener;
}

Expand Down
101 changes: 59 additions & 42 deletions packages/legacy-events/EventPluginRegistry.js
Original file line number Diff line number Diff line change
Expand Up @@ -42,34 +42,43 @@ function recomputePluginOrdering(): void {
for (const pluginName in namesToPlugins) {
const pluginModule = namesToPlugins[pluginName];
const pluginIndex = eventPluginOrder.indexOf(pluginName);
invariant(
pluginIndex > -1,
'EventPluginRegistry: Cannot inject event plugins that do not exist in ' +
'the plugin ordering, `%s`.',
pluginName,
);

if (!(pluginIndex > -1)) {
invariant(
'EventPluginRegistry: Cannot inject event plugins that do not exist in ' +
'the plugin ordering, `%s`.',
pluginName,
);
}

if (plugins[pluginIndex]) {
continue;
}
invariant(
pluginModule.extractEvents,
'EventPluginRegistry: Event plugins must implement an `extractEvents` ' +
'method, but `%s` does not.',
pluginName,
);

if (!pluginModule.extractEvents) {
invariant(
'EventPluginRegistry: Event plugins must implement an `extractEvents` ' +
'method, but `%s` does not.',
pluginName,
);
}

plugins[pluginIndex] = pluginModule;
const publishedEvents = pluginModule.eventTypes;
for (const eventName in publishedEvents) {
invariant(
publishEventForPlugin(
if (
!publishEventForPlugin(
publishedEvents[eventName],
pluginModule,
eventName,
),
'EventPluginRegistry: Failed to publish event `%s` for plugin `%s`.',
eventName,
pluginName,
);
)
) {
invariant(
'EventPluginRegistry: Failed to publish event `%s` for plugin `%s`.',
eventName,
pluginName,
);
}
}
}
}
Expand All @@ -87,12 +96,14 @@ function publishEventForPlugin(
pluginModule: PluginModule<AnyNativeEvent>,
eventName: string,
): boolean {
invariant(
!eventNameDispatchConfigs.hasOwnProperty(eventName),
'EventPluginHub: More than one plugin attempted to publish the same ' +
'event name, `%s`.',
eventName,
);
if (eventNameDispatchConfigs.hasOwnProperty(eventName)) {
invariant(
'EventPluginHub: More than one plugin attempted to publish the same ' +
'event name, `%s`.',
eventName,
);
}

eventNameDispatchConfigs[eventName] = dispatchConfig;

const phasedRegistrationNames = dispatchConfig.phasedRegistrationNames;
Expand Down Expand Up @@ -131,12 +142,14 @@ function publishRegistrationName(
pluginModule: PluginModule<AnyNativeEvent>,
eventName: string,
): void {
invariant(
!registrationNameModules[registrationName],
'EventPluginHub: More than one plugin attempted to publish the same ' +
'registration name, `%s`.',
registrationName,
);
if (registrationNameModules[registrationName]) {
invariant(
'EventPluginHub: More than one plugin attempted to publish the same ' +
'registration name, `%s`.',
registrationName,
);
}

registrationNameModules[registrationName] = pluginModule;
registrationNameDependencies[registrationName] =
pluginModule.eventTypes[eventName].dependencies;
Expand Down Expand Up @@ -198,11 +211,13 @@ export const possibleRegistrationNames = __DEV__ ? {} : (null: any);
export function injectEventPluginOrder(
injectedEventPluginOrder: EventPluginOrder,
): void {
invariant(
!eventPluginOrder,
'EventPluginRegistry: Cannot inject event plugin ordering more than ' +
'once. You are likely trying to load more than one copy of React.',
);
if (eventPluginOrder) {
invariant(
'EventPluginRegistry: Cannot inject event plugin ordering more than ' +
'once. You are likely trying to load more than one copy of React.',
);
}

// Clone the ordering so it cannot be dynamically mutated.
eventPluginOrder = Array.prototype.slice.call(injectedEventPluginOrder);
recomputePluginOrdering();
Expand Down Expand Up @@ -231,12 +246,14 @@ export function injectEventPluginsByName(
!namesToPlugins.hasOwnProperty(pluginName) ||
namesToPlugins[pluginName] !== pluginModule
) {
invariant(
!namesToPlugins[pluginName],
'EventPluginRegistry: Cannot inject two different event plugins ' +
'using the same name, `%s`.',
pluginName,
);
if (namesToPlugins[pluginName]) {
invariant(
'EventPluginRegistry: Cannot inject two different event plugins ' +
'using the same name, `%s`.',
pluginName,
);
}

namesToPlugins[pluginName] = pluginModule;
isOrderingDirty = true;
}
Expand Down
27 changes: 14 additions & 13 deletions packages/legacy-events/EventPluginUtils.js
Original file line number Diff line number Diff line change
Expand Up @@ -22,11 +22,12 @@ export function setComponentTree(
getInstanceFromNode = getInstanceFromNodeImpl;
getNodeFromInstance = getNodeFromInstanceImpl;
if (__DEV__) {
warningWithoutStack(
getNodeFromInstance && getInstanceFromNode,
'EventPluginUtils.setComponentTree(...): Injected ' +
'module is missing getNodeFromInstance or getInstanceFromNode.',
);
if (!(getNodeFromInstance && getInstanceFromNode)) {
warningWithoutStack(
'EventPluginUtils.setComponentTree(...): Injected ' +
'module is missing getNodeFromInstance or getInstanceFromNode.',
);
}
}
}

Expand All @@ -50,10 +51,9 @@ if (__DEV__) {
? 1
: 0;

warningWithoutStack(
instancesIsArr === listenersIsArr && instancesLen === listenersLen,
'EventPluginUtils: Invalid `event`.',
);
if (!(instancesIsArr === listenersIsArr && instancesLen === listenersLen)) {
warningWithoutStack('EventPluginUtils: Invalid `event`.');
}
};
}

Expand Down Expand Up @@ -150,10 +150,11 @@ export function executeDirectDispatch(event) {
}
const dispatchListener = event._dispatchListeners;
const dispatchInstance = event._dispatchInstances;
invariant(
!Array.isArray(dispatchListener),
'executeDirectDispatch(...): Invalid `event`.',
);

if (Array.isArray(dispatchListener)) {
invariant('executeDirectDispatch(...): Invalid `event`.');
}

event.currentTarget = dispatchListener
? getNodeFromInstance(dispatchInstance)
: null;
Expand Down
4 changes: 3 additions & 1 deletion packages/legacy-events/EventPropagators.js
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,9 @@ function listenerAtPhase(inst, event, propagationPhase: PropagationPhases) {
*/
function accumulateDirectionalDispatches(inst, phase, event) {
if (__DEV__) {
warningWithoutStack(inst, 'Dispatching inst must not be null');
if (!inst) {
warningWithoutStack('Dispatching inst must not be null');
}
}
const listener = listenerAtPhase(inst, event, phase);
if (listener) {
Expand Down
13 changes: 8 additions & 5 deletions packages/legacy-events/ReactControlledComponent.js
Original file line number Diff line number Diff line change
Expand Up @@ -28,11 +28,14 @@ function restoreStateOfTarget(target) {
// Unmounted
return;
}
invariant(
typeof restoreImpl === 'function',
'setRestoreImplementation() needs to be called to handle a target for controlled ' +
'events. This error is likely caused by a bug in React. Please file an issue.',
);

if (!(typeof restoreImpl === 'function')) {
invariant(
'setRestoreImplementation() needs to be called to handle a target for controlled ' +
'events. This error is likely caused by a bug in React. Please file an issue.',
);
}

const props = getFiberCurrentPropsFromNode(internalInstance.stateNode);
restoreImpl(internalInstance.stateNode, internalInstance.type, props);
}
Expand Down
28 changes: 16 additions & 12 deletions packages/legacy-events/ResponderTouchHistoryStore.js
Original file line number Diff line number Diff line change
Expand Up @@ -93,15 +93,19 @@ function resetTouchRecord(touchRecord: TouchRecord, touch: Touch): void {
}

function getTouchIdentifier({identifier}: Touch): number {
invariant(identifier != null, 'Touch object is missing identifier.');
if (!(identifier != null)) {
invariant('Touch object is missing identifier.');
}

if (__DEV__) {
warningWithoutStack(
identifier <= MAX_TOUCH_BANK,
'Touch identifier %s is greater than maximum supported %s which causes ' +
'performance issues backfilling array locations for all of the indices.',
identifier,
MAX_TOUCH_BANK,
);
if (!(identifier <= MAX_TOUCH_BANK)) {
warningWithoutStack(
'Touch identifier %s is greater than maximum supported %s which causes ' +
'performance issues backfilling array locations for all of the indices.',
identifier,
MAX_TOUCH_BANK,
);
}
}
return identifier;
}
Expand Down Expand Up @@ -200,10 +204,10 @@ const ResponderTouchHistoryStore = {
}
if (__DEV__) {
const activeRecord = touchBank[touchHistory.indexOfSingleActiveTouch];
warningWithoutStack(
activeRecord != null && activeRecord.touchActive,
'Cannot find single active touch.',
);

if (!(activeRecord != null && activeRecord.touchActive)) {
warningWithoutStack('Cannot find single active touch.');
}
}
}
}
Expand Down
Loading