Skip to content

Commit 1d90894

Browse files
authored
Re-add the warning if PropType function is called manually (#8903)
* Revert "Revert "Warn if PropType function is called manually (#7132)"" This reverts commit be71f76. In other words, now we again have the warning if you attempt to call PropTypes manually. It was removed in #8066 but we shouldn't have done this since we still want to avoid people accidentally calling them in production (and even more so since now it would throw). Fixes #8080. * Pass secret in ReactControlledValuePropTypes * Record tests
1 parent 994a0c8 commit 1d90894

File tree

6 files changed

+276
-8
lines changed

6 files changed

+276
-8
lines changed

scripts/fiber/tests-passing.txt

+10
Original file line numberDiff line numberDiff line change
@@ -313,9 +313,11 @@ src/isomorphic/classic/types/__tests__/ReactPropTypes-test.js
313313
* should not warn for valid values
314314
* should be implicitly optional and not warn without values
315315
* should warn for missing required values
316+
* should warn if called manually in development
316317
* should should accept any value
317318
* should be implicitly optional and not warn without values
318319
* should warn for missing required values
320+
* should warn if called manually in development
319321
* should fail for invalid argument
320322
* should support the arrayOf propTypes
321323
* should support arrayOf with complex types
@@ -325,23 +327,27 @@ src/isomorphic/classic/types/__tests__/ReactPropTypes-test.js
325327
* should not warn when passing an empty array
326328
* should be implicitly optional and not warn without values
327329
* should warn for missing required values
330+
* should warn if called manually in development
328331
* should support components
329332
* should not support multiple components or scalar values
330333
* should be able to define a single child as label
331334
* should warn when passing no label and isRequired is set
332335
* should be implicitly optional and not warn without values
333336
* should warn for missing required values
337+
* should warn if called manually in development
334338
* should warn for invalid instances
335339
* should not warn for valid values
336340
* should be implicitly optional and not warn without values
337341
* should warn for missing required values
342+
* should warn if called manually in development
338343
* should warn for invalid values
339344
* should not warn for valid values
340345
* should not warn for iterables
341346
* should not warn for entry iterables
342347
* should not warn for null/undefined if not required
343348
* should warn for missing required values
344349
* should accept empty array for required props
350+
* should warn if called manually in development
345351
* should fail for invalid argument
346352
* should support the objectOf propTypes
347353
* should support objectOf with complex types
@@ -351,16 +357,19 @@ src/isomorphic/classic/types/__tests__/ReactPropTypes-test.js
351357
* should not warn when passing an empty object
352358
* should be implicitly optional and not warn without values
353359
* should warn for missing required values
360+
* should warn if called manually in development
354361
* should warn but not error for invalid argument
355362
* should warn for invalid values
356363
* should not warn for valid values
357364
* should be implicitly optional and not warn without values
358365
* should warn for missing required values
366+
* should warn if called manually in development
359367
* should warn but not error for invalid argument
360368
* should warn if none of the types are valid
361369
* should not warn if one of the types are valid
362370
* should be implicitly optional and not warn without values
363371
* should warn for missing required values
372+
* should warn if called manually in development
364373
* should warn for non objects
365374
* should not warn for empty values
366375
* should not warn for an empty object
@@ -371,6 +380,7 @@ src/isomorphic/classic/types/__tests__/ReactPropTypes-test.js
371380
* should warn for invalid key types
372381
* should be implicitly optional and not warn without values
373382
* should warn for missing required values
383+
* should warn if called manually in development
374384
* should warn for non-symbol
375385
* should not warn for a polyfilled Symbol
376386
* should have been called with the right params

src/isomorphic/classic/types/ReactPropTypes.js

+57-7
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@
1313

1414
var ReactElement = require('ReactElement');
1515
var ReactPropTypeLocationNames = require('ReactPropTypeLocationNames');
16+
var ReactPropTypesSecret = require('ReactPropTypesSecret');
1617

1718
var emptyFunction = require('emptyFunction');
1819
var getIteratorFn = require('getIteratorFn');
@@ -154,16 +155,42 @@ function PropTypeError(message) {
154155
PropTypeError.prototype = Error.prototype;
155156

156157
function createChainableTypeChecker(validate) {
158+
if (__DEV__) {
159+
var manualPropTypeCallCache = {};
160+
}
157161
function checkType(
158162
isRequired,
159163
props,
160164
propName,
161165
componentName,
162166
location,
163-
propFullName
167+
propFullName,
168+
secret
164169
) {
165170
componentName = componentName || ANONYMOUS;
166171
propFullName = propFullName || propName;
172+
if (__DEV__) {
173+
if (
174+
secret !== ReactPropTypesSecret &&
175+
typeof console !== 'undefined'
176+
) {
177+
var cacheKey = `${componentName}:${propName}`;
178+
if (!manualPropTypeCallCache[cacheKey]) {
179+
warning(
180+
false,
181+
'You are manually calling a React.PropTypes validation ' +
182+
'function for the `%s` prop on `%s`. This is deprecated ' +
183+
'and will not work in production with the next major version. ' +
184+
'You may be seeing this warning due to a third-party PropTypes ' +
185+
'library. See https://fb.me/react-warning-dont-call-proptypes ' +
186+
'for details.',
187+
propFullName,
188+
componentName
189+
);
190+
manualPropTypeCallCache[cacheKey] = true;
191+
}
192+
}
193+
}
167194
if (props[propName] == null) {
168195
var locationName = ReactPropTypeLocationNames[location];
169196
if (isRequired) {
@@ -180,7 +207,13 @@ function createChainableTypeChecker(validate) {
180207
}
181208
return null;
182209
} else {
183-
return validate(props, propName, componentName, location, propFullName);
210+
return validate(
211+
props,
212+
propName,
213+
componentName,
214+
location,
215+
propFullName,
216+
);
184217
}
185218
}
186219

@@ -191,7 +224,14 @@ function createChainableTypeChecker(validate) {
191224
}
192225

193226
function createPrimitiveTypeChecker(expectedType) {
194-
function validate(props, propName, componentName, location, propFullName) {
227+
function validate(
228+
props,
229+
propName,
230+
componentName,
231+
location,
232+
propFullName,
233+
secret
234+
) {
195235
var propValue = props[propName];
196236
var propType = getPropType(propValue);
197237
if (propType !== expectedType) {
@@ -238,7 +278,8 @@ function createArrayOfTypeChecker(typeChecker) {
238278
i,
239279
componentName,
240280
location,
241-
`${propFullName}[${i}]`
281+
`${propFullName}[${i}]`,
282+
ReactPropTypesSecret
242283
);
243284
if (error instanceof Error) {
244285
return error;
@@ -329,7 +370,8 @@ function createObjectOfTypeChecker(typeChecker) {
329370
key,
330371
componentName,
331372
location,
332-
`${propFullName}.${key}`
373+
`${propFullName}.${key}`,
374+
ReactPropTypesSecret
333375
);
334376
if (error instanceof Error) {
335377
return error;
@@ -351,7 +393,14 @@ function createUnionTypeChecker(arrayOfTypeCheckers) {
351393
for (var i = 0; i < arrayOfTypeCheckers.length; i++) {
352394
var checker = arrayOfTypeCheckers[i];
353395
if (
354-
checker(props, propName, componentName, location, propFullName) == null
396+
checker(
397+
props,
398+
propName,
399+
componentName,
400+
location,
401+
propFullName,
402+
ReactPropTypesSecret
403+
) == null
355404
) {
356405
return null;
357406
}
@@ -401,7 +450,8 @@ function createShapeTypeChecker(shapeTypes) {
401450
key,
402451
componentName,
403452
location,
404-
`${propFullName}.${key}`
453+
`${propFullName}.${key}`,
454+
ReactPropTypesSecret
405455
);
406456
if (error) {
407457
return error;

0 commit comments

Comments
 (0)