UNPKG

4.07 kBJavaScriptView Raw
1/**
2 * Copyright (c) 2013-present, Facebook, Inc.
3 *
4 * This source code is licensed under the MIT license found in the
5 * LICENSE file in the root directory of this source tree.
6 *
7 */
8
9'use strict';
10
11var _prodInvariant = require('./reactProdInvariant');
12
13var ReactPropTypeLocationNames = require('./ReactPropTypeLocationNames');
14var ReactPropTypesSecret = require('./ReactPropTypesSecret');
15
16var invariant = require('fbjs/lib/invariant');
17var warning = require('fbjs/lib/warning');
18
19var ReactComponentTreeHook;
20
21if (typeof process !== 'undefined' && process.env && process.env.NODE_ENV === 'test') {
22 // Temporary hack.
23 // Inline requires don't work well with Jest:
24 // https://github.com/facebook/react/issues/7240
25 // Remove the inline requires when we don't need them anymore:
26 // https://github.com/facebook/react/pull/7178
27 ReactComponentTreeHook = require('./ReactComponentTreeHook');
28}
29
30var loggedTypeFailures = {};
31
32/**
33 * Assert that the values match with the type specs.
34 * Error messages are memorized and will only be shown once.
35 *
36 * @param {object} typeSpecs Map of name to a ReactPropType
37 * @param {object} values Runtime values that need to be type-checked
38 * @param {string} location e.g. "prop", "context", "child context"
39 * @param {string} componentName Name of the component for error messages.
40 * @param {?object} element The React element that is being type-checked
41 * @param {?number} debugID The React component instance that is being type-checked
42 * @private
43 */
44function checkReactTypeSpec(typeSpecs, values, location, componentName, element, debugID) {
45 for (var typeSpecName in typeSpecs) {
46 if (typeSpecs.hasOwnProperty(typeSpecName)) {
47 var error;
48 // Prop type validation may throw. In case they do, we don't want to
49 // fail the render phase where it didn't fail before. So we log it.
50 // After these have been cleaned up, we'll let them throw.
51 try {
52 // This is intentionally an invariant that gets caught. It's the same
53 // behavior as without this statement except with a better message.
54 !(typeof typeSpecs[typeSpecName] === 'function') ? process.env.NODE_ENV !== 'production' ? invariant(false, '%s: %s type `%s` is invalid; it must be a function, usually from React.PropTypes.', componentName || 'React class', ReactPropTypeLocationNames[location], typeSpecName) : _prodInvariant('84', componentName || 'React class', ReactPropTypeLocationNames[location], typeSpecName) : void 0;
55 error = typeSpecs[typeSpecName](values, typeSpecName, componentName, location, null, ReactPropTypesSecret);
56 } catch (ex) {
57 error = ex;
58 }
59 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', ReactPropTypeLocationNames[location], typeSpecName, typeof error) : void 0;
60 if (error instanceof Error && !(error.message in loggedTypeFailures)) {
61 // Only monitor this failure once because there tends to be a lot of the
62 // same error.
63 loggedTypeFailures[error.message] = true;
64
65 var componentStackInfo = '';
66
67 if (process.env.NODE_ENV !== 'production') {
68 if (!ReactComponentTreeHook) {
69 ReactComponentTreeHook = require('./ReactComponentTreeHook');
70 }
71 if (debugID !== null) {
72 componentStackInfo = ReactComponentTreeHook.getStackAddendumByID(debugID);
73 } else if (element !== null) {
74 componentStackInfo = ReactComponentTreeHook.getCurrentStackAddendum(element);
75 }
76 }
77
78 process.env.NODE_ENV !== 'production' ? warning(false, 'Failed %s type: %s%s', location, error.message, componentStackInfo) : void 0;
79 }
80 }
81 }
82}
83
84module.exports = checkReactTypeSpec;
\No newline at end of file