Skip to content

Commit b43eec7

Browse files
Laura bunsgaearon
Laura buns
authored andcommitted
Replace wrap-warning-with-env-check with an eslint plugin (#17540)
* Replace Babel plugin with an ESLint plugin * Fix ESLint rule violations * Move shared conditions higher * Test formatting nits * Tweak ESLint rule * Bugfix: inside else branch, 'if' tests are not satisfactory * Use a stricter check for exactly if (__DEV__) This makes it easier to see what's going on and matches dominant style in the codebase. * Fix remaining files after stricter check
1 parent acfe4b2 commit b43eec7

35 files changed

+928
-545
lines changed

.eslintrc.js

+1
Original file line numberDiff line numberDiff line change
@@ -93,6 +93,7 @@ module.exports = {
9393
'react-internal/no-primitive-constructors': ERROR,
9494
'react-internal/no-to-warn-dev-within-to-throw': ERROR,
9595
'react-internal/warning-and-invariant-args': ERROR,
96+
'react-internal/no-production-logging': ERROR,
9697
},
9798

9899
overrides: [

packages/create-subscription/src/createSubscription.js

+10-8
Original file line numberDiff line numberDiff line change
@@ -36,14 +36,16 @@ export function createSubscription<Property, Value>(
3636
}> {
3737
const {getCurrentValue, subscribe} = config;
3838

39-
warningWithoutStack(
40-
typeof getCurrentValue === 'function',
41-
'Subscription must specify a getCurrentValue function',
42-
);
43-
warningWithoutStack(
44-
typeof subscribe === 'function',
45-
'Subscription must specify a subscribe function',
46-
);
39+
if (__DEV__) {
40+
warningWithoutStack(
41+
typeof getCurrentValue === 'function',
42+
'Subscription must specify a getCurrentValue function',
43+
);
44+
warningWithoutStack(
45+
typeof subscribe === 'function',
46+
'Subscription must specify a subscribe function',
47+
);
48+
}
4749

4850
type Props = {
4951
children: (value: Value) => React$Element<any>,

packages/legacy-events/SyntheticEvent.js

+12-11
Original file line numberDiff line numberDiff line change
@@ -283,17 +283,18 @@ function getPooledWarningPropertyDefinition(propName, getVal) {
283283
}
284284

285285
function warn(action, result) {
286-
const warningCondition = false;
287-
warningWithoutStack(
288-
warningCondition,
289-
"This synthetic event is reused for performance reasons. If you're seeing this, " +
290-
"you're %s `%s` on a released/nullified synthetic event. %s. " +
291-
'If you must keep the original synthetic event around, use event.persist(). ' +
292-
'See https://fb.me/react-event-pooling for more information.',
293-
action,
294-
propName,
295-
result,
296-
);
286+
if (__DEV__) {
287+
warningWithoutStack(
288+
false,
289+
"This synthetic event is reused for performance reasons. If you're seeing this, " +
290+
"you're %s `%s` on a released/nullified synthetic event. %s. " +
291+
'If you must keep the original synthetic event around, use event.persist(). ' +
292+
'See https://fb.me/react-event-pooling for more information.',
293+
action,
294+
propName,
295+
result,
296+
);
297+
}
297298
}
298299
}
299300

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

+11-9
Original file line numberDiff line numberDiff line change
@@ -142,15 +142,17 @@ const ReactDOM: Object = {
142142
// Temporary alias since we already shipped React 16 RC with it.
143143
// TODO: remove in React 17.
144144
unstable_createPortal(...args) {
145-
if (!didWarnAboutUnstableCreatePortal) {
146-
didWarnAboutUnstableCreatePortal = true;
147-
lowPriorityWarningWithoutStack(
148-
false,
149-
'The ReactDOM.unstable_createPortal() alias has been deprecated, ' +
150-
'and will be removed in React 17+. Update your code to use ' +
151-
'ReactDOM.createPortal() instead. It has the exact same API, ' +
152-
'but without the "unstable_" prefix.',
153-
);
145+
if (__DEV__) {
146+
if (!didWarnAboutUnstableCreatePortal) {
147+
didWarnAboutUnstableCreatePortal = true;
148+
lowPriorityWarningWithoutStack(
149+
false,
150+
'The ReactDOM.unstable_createPortal() alias has been deprecated, ' +
151+
'and will be removed in React 17+. Update your code to use ' +
152+
'ReactDOM.createPortal() instead. It has the exact same API, ' +
153+
'but without the "unstable_" prefix.',
154+
);
155+
}
154156
}
155157
return createPortal(...args);
156158
},

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

+25-23
Original file line numberDiff line numberDiff line change
@@ -40,30 +40,32 @@ const valuePropNames = ['value', 'defaultValue'];
4040
* Validation function for `value` and `defaultValue`.
4141
*/
4242
function checkSelectPropTypes(props) {
43-
ReactControlledValuePropTypes.checkPropTypes('select', props);
43+
if (__DEV__) {
44+
ReactControlledValuePropTypes.checkPropTypes('select', props);
4445

45-
for (let i = 0; i < valuePropNames.length; i++) {
46-
const propName = valuePropNames[i];
47-
if (props[propName] == null) {
48-
continue;
49-
}
50-
const isArray = Array.isArray(props[propName]);
51-
if (props.multiple && !isArray) {
52-
warning(
53-
false,
54-
'The `%s` prop supplied to <select> must be an array if ' +
55-
'`multiple` is true.%s',
56-
propName,
57-
getDeclarationErrorAddendum(),
58-
);
59-
} else if (!props.multiple && isArray) {
60-
warning(
61-
false,
62-
'The `%s` prop supplied to <select> must be a scalar ' +
63-
'value if `multiple` is false.%s',
64-
propName,
65-
getDeclarationErrorAddendum(),
66-
);
46+
for (let i = 0; i < valuePropNames.length; i++) {
47+
const propName = valuePropNames[i];
48+
if (props[propName] == null) {
49+
continue;
50+
}
51+
const isArray = Array.isArray(props[propName]);
52+
if (props.multiple && !isArray) {
53+
warning(
54+
false,
55+
'The `%s` prop supplied to <select> must be an array if ' +
56+
'`multiple` is true.%s',
57+
propName,
58+
getDeclarationErrorAddendum(),
59+
);
60+
} else if (!props.multiple && isArray) {
61+
warning(
62+
false,
63+
'The `%s` prop supplied to <select> must be a scalar ' +
64+
'value if `multiple` is false.%s',
65+
propName,
66+
getDeclarationErrorAddendum(),
67+
);
68+
}
6769
}
6870
}
6971
}

packages/react-dom/src/server/ReactPartialRenderer.js

+8-6
Original file line numberDiff line numberDiff line change
@@ -687,12 +687,14 @@ function resolve(
687687
);
688688
}
689689
} else {
690-
warningWithoutStack(
691-
false,
692-
'%s.getChildContext(): childContextTypes must be defined in order to ' +
693-
'use getChildContext().',
694-
getComponentName(Component) || 'Unknown',
695-
);
690+
if (__DEV__) {
691+
warningWithoutStack(
692+
false,
693+
'%s.getChildContext(): childContextTypes must be defined in order to ' +
694+
'use getChildContext().',
695+
getComponentName(Component) || 'Unknown',
696+
);
697+
}
696698
}
697699
}
698700
if (childContext) {

packages/react-dom/src/server/ReactPartialRendererHooks.js

+9-9
Original file line numberDiff line numberDiff line change
@@ -392,16 +392,16 @@ export function useLayoutEffect(
392392
) {
393393
if (__DEV__) {
394394
currentHookNameInDev = 'useLayoutEffect';
395+
warning(
396+
false,
397+
'useLayoutEffect does nothing on the server, because its effect cannot ' +
398+
"be encoded into the server renderer's output format. This will lead " +
399+
'to a mismatch between the initial, non-hydrated UI and the intended ' +
400+
'UI. To avoid this, useLayoutEffect should only be used in ' +
401+
'components that render exclusively on the client. ' +
402+
'See https://fb.me/react-uselayouteffect-ssr for common fixes.',
403+
);
395404
}
396-
warning(
397-
false,
398-
'useLayoutEffect does nothing on the server, because its effect cannot ' +
399-
"be encoded into the server renderer's output format. This will lead " +
400-
'to a mismatch between the initial, non-hydrated UI and the intended ' +
401-
'UI. To avoid this, useLayoutEffect should only be used in ' +
402-
'components that render exclusively on the client. ' +
403-
'See https://fb.me/react-uselayouteffect-ssr for common fixes.',
404-
);
405405
}
406406

407407
function dispatchAction<A>(

packages/react-dom/src/shared/CSSPropertyOperations.js

+30-28
Original file line numberDiff line numberDiff line change
@@ -128,37 +128,39 @@ export function validateShorthandPropertyCollisionInDev(
128128
styleUpdates,
129129
nextStyles,
130130
) {
131-
if (!warnAboutShorthandPropertyCollision) {
132-
return;
133-
}
131+
if (__DEV__) {
132+
if (!warnAboutShorthandPropertyCollision) {
133+
return;
134+
}
134135

135-
if (!nextStyles) {
136-
return;
137-
}
136+
if (!nextStyles) {
137+
return;
138+
}
138139

139-
const expandedUpdates = expandShorthandMap(styleUpdates);
140-
const expandedStyles = expandShorthandMap(nextStyles);
141-
const warnedAbout = {};
142-
for (const key in expandedUpdates) {
143-
const originalKey = expandedUpdates[key];
144-
const correctOriginalKey = expandedStyles[key];
145-
if (correctOriginalKey && originalKey !== correctOriginalKey) {
146-
const warningKey = originalKey + ',' + correctOriginalKey;
147-
if (warnedAbout[warningKey]) {
148-
continue;
140+
const expandedUpdates = expandShorthandMap(styleUpdates);
141+
const expandedStyles = expandShorthandMap(nextStyles);
142+
const warnedAbout = {};
143+
for (const key in expandedUpdates) {
144+
const originalKey = expandedUpdates[key];
145+
const correctOriginalKey = expandedStyles[key];
146+
if (correctOriginalKey && originalKey !== correctOriginalKey) {
147+
const warningKey = originalKey + ',' + correctOriginalKey;
148+
if (warnedAbout[warningKey]) {
149+
continue;
150+
}
151+
warnedAbout[warningKey] = true;
152+
warning(
153+
false,
154+
'%s a style property during rerender (%s) when a ' +
155+
'conflicting property is set (%s) can lead to styling bugs. To ' +
156+
"avoid this, don't mix shorthand and non-shorthand properties " +
157+
'for the same value; instead, replace the shorthand with ' +
158+
'separate values.',
159+
isValueEmpty(styleUpdates[originalKey]) ? 'Removing' : 'Updating',
160+
originalKey,
161+
correctOriginalKey,
162+
);
149163
}
150-
warnedAbout[warningKey] = true;
151-
warning(
152-
false,
153-
'%s a style property during rerender (%s) when a ' +
154-
'conflicting property is set (%s) can lead to styling bugs. To ' +
155-
"avoid this, don't mix shorthand and non-shorthand properties " +
156-
'for the same value; instead, replace the shorthand with ' +
157-
'separate values.',
158-
isValueEmpty(styleUpdates[originalKey]) ? 'Removing' : 'Updating',
159-
originalKey,
160-
correctOriginalKey,
161-
);
162164
}
163165
}
164166
}

0 commit comments

Comments
 (0)