Skip to content

Commit

Permalink
Re-add the warning if PropType function is called manually (#8903)
Browse files Browse the repository at this point in the history
* 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
  • Loading branch information
gaearon committed Feb 8, 2017
1 parent 994a0c8 commit 1d90894
Show file tree
Hide file tree
Showing 6 changed files with 276 additions and 8 deletions.
10 changes: 10 additions & 0 deletions scripts/fiber/tests-passing.txt
Original file line number Diff line number Diff line change
Expand Up @@ -313,9 +313,11 @@ src/isomorphic/classic/types/__tests__/ReactPropTypes-test.js
* should not warn for valid values
* should be implicitly optional and not warn without values
* should warn for missing required values
* should warn if called manually in development
* should should accept any value
* should be implicitly optional and not warn without values
* should warn for missing required values
* should warn if called manually in development
* should fail for invalid argument
* should support the arrayOf propTypes
* should support arrayOf with complex types
Expand All @@ -325,23 +327,27 @@ src/isomorphic/classic/types/__tests__/ReactPropTypes-test.js
* should not warn when passing an empty array
* should be implicitly optional and not warn without values
* should warn for missing required values
* should warn if called manually in development
* should support components
* should not support multiple components or scalar values
* should be able to define a single child as label
* should warn when passing no label and isRequired is set
* should be implicitly optional and not warn without values
* should warn for missing required values
* should warn if called manually in development
* should warn for invalid instances
* should not warn for valid values
* should be implicitly optional and not warn without values
* should warn for missing required values
* should warn if called manually in development
* should warn for invalid values
* should not warn for valid values
* should not warn for iterables
* should not warn for entry iterables
* should not warn for null/undefined if not required
* should warn for missing required values
* should accept empty array for required props
* should warn if called manually in development
* should fail for invalid argument
* should support the objectOf propTypes
* should support objectOf with complex types
Expand All @@ -351,16 +357,19 @@ src/isomorphic/classic/types/__tests__/ReactPropTypes-test.js
* should not warn when passing an empty object
* should be implicitly optional and not warn without values
* should warn for missing required values
* should warn if called manually in development
* should warn but not error for invalid argument
* should warn for invalid values
* should not warn for valid values
* should be implicitly optional and not warn without values
* should warn for missing required values
* should warn if called manually in development
* should warn but not error for invalid argument
* should warn if none of the types are valid
* should not warn if one of the types are valid
* should be implicitly optional and not warn without values
* should warn for missing required values
* should warn if called manually in development
* should warn for non objects
* should not warn for empty values
* should not warn for an empty object
Expand All @@ -371,6 +380,7 @@ src/isomorphic/classic/types/__tests__/ReactPropTypes-test.js
* should warn for invalid key types
* should be implicitly optional and not warn without values
* should warn for missing required values
* should warn if called manually in development
* should warn for non-symbol
* should not warn for a polyfilled Symbol
* should have been called with the right params
Expand Down
64 changes: 57 additions & 7 deletions src/isomorphic/classic/types/ReactPropTypes.js
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@

var ReactElement = require('ReactElement');
var ReactPropTypeLocationNames = require('ReactPropTypeLocationNames');
var ReactPropTypesSecret = require('ReactPropTypesSecret');

var emptyFunction = require('emptyFunction');
var getIteratorFn = require('getIteratorFn');
Expand Down Expand Up @@ -154,16 +155,42 @@ function PropTypeError(message) {
PropTypeError.prototype = Error.prototype;

function createChainableTypeChecker(validate) {
if (__DEV__) {
var manualPropTypeCallCache = {};
}
function checkType(
isRequired,
props,
propName,
componentName,
location,
propFullName
propFullName,
secret
) {
componentName = componentName || ANONYMOUS;
propFullName = propFullName || propName;
if (__DEV__) {
if (
secret !== ReactPropTypesSecret &&
typeof console !== 'undefined'
) {
var cacheKey = `${componentName}:${propName}`;
if (!manualPropTypeCallCache[cacheKey]) {
warning(
false,
'You are manually calling a React.PropTypes validation ' +
'function for the `%s` prop on `%s`. This is deprecated ' +
'and will not work in production with the next major version. ' +
'You may be seeing this warning due to a third-party PropTypes ' +
'library. See https://fb.me/react-warning-dont-call-proptypes ' +
'for details.',
propFullName,
componentName
);
manualPropTypeCallCache[cacheKey] = true;
}
}
}
if (props[propName] == null) {
var locationName = ReactPropTypeLocationNames[location];
if (isRequired) {
Expand All @@ -180,7 +207,13 @@ function createChainableTypeChecker(validate) {
}
return null;
} else {
return validate(props, propName, componentName, location, propFullName);
return validate(
props,
propName,
componentName,
location,
propFullName,
);
}
}

Expand All @@ -191,7 +224,14 @@ function createChainableTypeChecker(validate) {
}

function createPrimitiveTypeChecker(expectedType) {
function validate(props, propName, componentName, location, propFullName) {
function validate(
props,
propName,
componentName,
location,
propFullName,
secret
) {
var propValue = props[propName];
var propType = getPropType(propValue);
if (propType !== expectedType) {
Expand Down Expand Up @@ -238,7 +278,8 @@ function createArrayOfTypeChecker(typeChecker) {
i,
componentName,
location,
`${propFullName}[${i}]`
`${propFullName}[${i}]`,
ReactPropTypesSecret
);
if (error instanceof Error) {
return error;
Expand Down Expand Up @@ -329,7 +370,8 @@ function createObjectOfTypeChecker(typeChecker) {
key,
componentName,
location,
`${propFullName}.${key}`
`${propFullName}.${key}`,
ReactPropTypesSecret
);
if (error instanceof Error) {
return error;
Expand All @@ -351,7 +393,14 @@ function createUnionTypeChecker(arrayOfTypeCheckers) {
for (var i = 0; i < arrayOfTypeCheckers.length; i++) {
var checker = arrayOfTypeCheckers[i];
if (
checker(props, propName, componentName, location, propFullName) == null
checker(
props,
propName,
componentName,
location,
propFullName,
ReactPropTypesSecret
) == null
) {
return null;
}
Expand Down Expand Up @@ -401,7 +450,8 @@ function createShapeTypeChecker(shapeTypes) {
key,
componentName,
location,
`${propFullName}.${key}`
`${propFullName}.${key}`,
ReactPropTypesSecret
);
if (error) {
return error;
Expand Down

0 comments on commit 1d90894

Please sign in to comment.