Skip to content

Commit 043845c

Browse files
committed
Extract ReactPropTypes to separate module
1 parent 3a7eef2 commit 043845c

File tree

15 files changed

+2754
-527
lines changed

15 files changed

+2754
-527
lines changed

addons/prop-types/README.md

+5
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
# prop-types
2+
3+
Runtime type checking for React props and similar objects.
4+
5+
Refer to the [React documentation](https://facebook.github.io/react/docs/typechecking-with-proptypes.html) for more information.

addons/prop-types/checkPropTypes.js

+61
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,61 @@
1+
/**
2+
* Copyright 2013-present, Facebook, Inc.
3+
* All rights reserved.
4+
*
5+
* This source code is licensed under the BSD-style license found in the
6+
* LICENSE file in the root directory of this source tree. An additional grant
7+
* of patent rights can be found in the PATENTS file in the same directory.
8+
*/
9+
10+
'use strict';
11+
12+
var invariant = require('fbjs/lib/invariant');
13+
var warning = require('fbjs/lib/warning');
14+
15+
var ReactPropTypesSecret = require('./lib/ReactPropTypesSecret');
16+
17+
var loggedTypeFailures = {};
18+
19+
/**
20+
* Assert that the values match with the type specs.
21+
* Error messages are memorized and will only be shown once.
22+
*
23+
* @param {object} typeSpecs Map of name to a ReactPropType
24+
* @param {object} values Runtime values that need to be type-checked
25+
* @param {string} location e.g. "prop", "context", "child context"
26+
* @param {string} componentName Name of the component for error messages.
27+
* @param {?Function} getStack Returns the component stack.
28+
* @private
29+
*/
30+
function checkPropTypes(typeSpecs, values, location, componentName, getStack) {
31+
if (process.env.NODE_ENV !== 'production') {
32+
for (var typeSpecName in typeSpecs) {
33+
if (typeSpecs.hasOwnProperty(typeSpecName)) {
34+
var error;
35+
// Prop type validation may throw. In case they do, we don't want to
36+
// fail the render phase where it didn't fail before. So we log it.
37+
// After these have been cleaned up, we'll let them throw.
38+
try {
39+
// This is intentionally an invariant that gets caught. It's the same
40+
// behavior as without this statement except with a better message.
41+
invariant(typeof typeSpecs[typeSpecName] === 'function', '%s: %s type `%s` is invalid; it must be a function, usually from ' + 'React.PropTypes.', componentName || 'React class', location, typeSpecName);
42+
error = typeSpecs[typeSpecName](values, typeSpecName, componentName, location, null, ReactPropTypesSecret);
43+
} catch (ex) {
44+
error = ex;
45+
}
46+
process.env.NODE_ENV !== 'production' ? warning(!error || error instanceof Error, '%s: type specification of %s `%s` is invalid; the type checker ' + 'function must return `null` or an `Error` but returned a %s. ' + 'You may have forgotten to pass an argument to the type checker ' + 'creator (arrayOf, instanceOf, objectOf, oneOf, oneOfType, and ' + 'shape all require an argument).', componentName || 'React class', location, typeSpecName, typeof error) : void 0;
47+
if (error instanceof Error && !(error.message in loggedTypeFailures)) {
48+
// Only monitor this failure once because there tends to be a lot of the
49+
// same error.
50+
loggedTypeFailures[error.message] = true;
51+
52+
var stack = getStack ? getStack() : '';
53+
54+
process.env.NODE_ENV !== 'production' ? warning(false, 'Failed %s type: %s%s', location, error.message, stack != null ? stack : '') : void 0;
55+
}
56+
}
57+
}
58+
}
59+
}
60+
61+
module.exports = checkPropTypes;

0 commit comments

Comments
 (0)