-
Notifications
You must be signed in to change notification settings - Fork 47.2k
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
Strip React.PropTypes type checking code in production #7651
Closed
Closed
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -13,10 +13,10 @@ | |
|
||
var ReactElement = require('ReactElement'); | ||
var ReactPropTypeLocationNames = require('ReactPropTypeLocationNames'); | ||
var ReactPropTypesSecret = require('ReactPropTypesSecret'); | ||
|
||
var emptyFunction = require('emptyFunction'); | ||
var getIteratorFn = require('getIteratorFn'); | ||
var invariant = require('invariant'); | ||
var warning = require('warning'); | ||
|
||
/** | ||
|
@@ -68,25 +68,57 @@ var warning = require('warning'); | |
|
||
var ANONYMOUS = '<<anonymous>>'; | ||
|
||
var ReactPropTypes = { | ||
array: createPrimitiveTypeChecker('array'), | ||
bool: createPrimitiveTypeChecker('boolean'), | ||
func: createPrimitiveTypeChecker('function'), | ||
number: createPrimitiveTypeChecker('number'), | ||
object: createPrimitiveTypeChecker('object'), | ||
string: createPrimitiveTypeChecker('string'), | ||
symbol: createPrimitiveTypeChecker('symbol'), | ||
|
||
any: createAnyTypeChecker(), | ||
arrayOf: createArrayOfTypeChecker, | ||
element: createElementTypeChecker(), | ||
instanceOf: createInstanceTypeChecker, | ||
node: createNodeChecker(), | ||
objectOf: createObjectOfTypeChecker, | ||
oneOf: createEnumTypeChecker, | ||
oneOfType: createUnionTypeChecker, | ||
shape: createShapeTypeChecker, | ||
}; | ||
if (__DEV__) { | ||
// Keep in sync with production version below | ||
var ReactPropTypes = { | ||
array: createPrimitiveTypeChecker('array'), | ||
bool: createPrimitiveTypeChecker('boolean'), | ||
func: createPrimitiveTypeChecker('function'), | ||
number: createPrimitiveTypeChecker('number'), | ||
object: createPrimitiveTypeChecker('object'), | ||
string: createPrimitiveTypeChecker('string'), | ||
symbol: createPrimitiveTypeChecker('symbol'), | ||
|
||
any: createAnyTypeChecker(), | ||
arrayOf: createArrayOfTypeChecker, | ||
element: createElementTypeChecker(), | ||
instanceOf: createInstanceTypeChecker, | ||
node: createNodeChecker(), | ||
objectOf: createObjectOfTypeChecker, | ||
oneOf: createEnumTypeChecker, | ||
oneOfType: createUnionTypeChecker, | ||
shape: createShapeTypeChecker, | ||
}; | ||
} else { | ||
var productionTypeChecker = function() { | ||
invariant( | ||
false, | ||
'React.PropTypes type checking code is stripped in production.' | ||
); | ||
}; | ||
productionTypeChecker.isRequired = productionTypeChecker; | ||
var getProductionTypeChecker = () => productionTypeChecker; | ||
// Keep in sync with development version above | ||
var ReactPropTypes = { | ||
array: getProductionTypeChecker(), | ||
bool: getProductionTypeChecker(), | ||
func: getProductionTypeChecker(), | ||
number: getProductionTypeChecker(), | ||
object: getProductionTypeChecker(), | ||
string: getProductionTypeChecker(), | ||
symbol: getProductionTypeChecker(), | ||
|
||
any: getProductionTypeChecker(), | ||
arrayOf: getProductionTypeChecker, | ||
element: getProductionTypeChecker(), | ||
instanceOf: getProductionTypeChecker, | ||
node: getProductionTypeChecker(), | ||
objectOf: getProductionTypeChecker, | ||
oneOf: getProductionTypeChecker, | ||
oneOfType: getProductionTypeChecker, | ||
shape: getProductionTypeChecker, | ||
}; | ||
} | ||
|
||
/** | ||
* inlined Object.is polyfill to avoid requiring consumers ship their own | ||
|
@@ -120,41 +152,16 @@ function PropTypeError(message) { | |
PropTypeError.prototype = Error.prototype; | ||
|
||
function createChainableTypeChecker(validate) { | ||
if (__DEV__) { | ||
var manualPropTypeCallCache = {}; | ||
} | ||
function checkType( | ||
isRequired, | ||
props, | ||
propName, | ||
componentName, | ||
location, | ||
propFullName, | ||
secret | ||
propFullName | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. you can leave the trailing comma :) |
||
) { | ||
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 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) { | ||
|
@@ -171,13 +178,7 @@ function createChainableTypeChecker(validate) { | |
} | ||
return null; | ||
} else { | ||
return validate( | ||
props, | ||
propName, | ||
componentName, | ||
location, | ||
propFullName, | ||
); | ||
return validate(props, propName, componentName, location, propFullName); | ||
} | ||
} | ||
|
||
|
@@ -188,14 +189,7 @@ function createChainableTypeChecker(validate) { | |
} | ||
|
||
function createPrimitiveTypeChecker(expectedType) { | ||
function validate( | ||
props, | ||
propName, | ||
componentName, | ||
location, | ||
propFullName, | ||
secret | ||
) { | ||
function validate(props, propName, componentName, location, propFullName) { | ||
var propValue = props[propName]; | ||
var propType = getPropType(propValue); | ||
if (propType !== expectedType) { | ||
|
@@ -242,8 +236,7 @@ function createArrayOfTypeChecker(typeChecker) { | |
i, | ||
componentName, | ||
location, | ||
`${propFullName}[${i}]`, | ||
ReactPropTypesSecret | ||
`${propFullName}[${i}]` | ||
); | ||
if (error instanceof Error) { | ||
return error; | ||
|
@@ -334,8 +327,7 @@ function createObjectOfTypeChecker(typeChecker) { | |
key, | ||
componentName, | ||
location, | ||
`${propFullName}.${key}`, | ||
ReactPropTypesSecret | ||
`${propFullName}.${key}` | ||
); | ||
if (error instanceof Error) { | ||
return error; | ||
|
@@ -357,14 +349,7 @@ function createUnionTypeChecker(arrayOfTypeCheckers) { | |
for (var i = 0; i < arrayOfTypeCheckers.length; i++) { | ||
var checker = arrayOfTypeCheckers[i]; | ||
if ( | ||
checker( | ||
props, | ||
propName, | ||
componentName, | ||
location, | ||
propFullName, | ||
ReactPropTypesSecret | ||
) == null | ||
checker(props, propName, componentName, location, propFullName) == null | ||
) { | ||
return null; | ||
} | ||
|
@@ -414,8 +399,7 @@ function createShapeTypeChecker(shapeTypes) { | |
key, | ||
componentName, | ||
location, | ||
`${propFullName}.${key}`, | ||
ReactPropTypesSecret | ||
`${propFullName}.${key}` | ||
); | ||
if (error) { | ||
return error; | ||
|
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
What's the point of calling it here? Why not just put
productionTypeChecker
?There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I'm trying to mirror the structure above exactly so it's easy to keep them in sync.