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 | 'use strict';
|
---|
9 |
|
---|
10 | var printWarning = function() {};
|
---|
11 |
|
---|
12 | if (process.env.NODE_ENV !== 'production') {
|
---|
13 | var ReactPropTypesSecret = require('./lib/ReactPropTypesSecret');
|
---|
14 | var loggedTypeFailures = {};
|
---|
15 | var has = require('./lib/has');
|
---|
16 |
|
---|
17 | printWarning = function(text) {
|
---|
18 | var message = 'Warning: ' + text;
|
---|
19 | if (typeof console !== 'undefined') {
|
---|
20 | console.error(message);
|
---|
21 | }
|
---|
22 | try {
|
---|
23 | // --- Welcome to debugging React ---
|
---|
24 | // This error was thrown as a convenience so that you can use this stack
|
---|
25 | // to find the callsite that caused this warning to fire.
|
---|
26 | throw new Error(message);
|
---|
27 | } catch (x) { /**/ }
|
---|
28 | };
|
---|
29 | }
|
---|
30 |
|
---|
31 | /**
|
---|
32 | * Assert that the values match with the type specs.
|
---|
33 | * Error messages are memorized and will only be shown once.
|
---|
34 | *
|
---|
35 | * @param {object} typeSpecs Map of name to a ReactPropType
|
---|
36 | * @param {object} values Runtime values that need to be type-checked
|
---|
37 | * @param {string} location e.g. "prop", "context", "child context"
|
---|
38 | * @param {string} componentName Name of the component for error messages.
|
---|
39 | * @param {?Function} getStack Returns the component stack.
|
---|
40 | * @private
|
---|
41 | */
|
---|
42 | function checkPropTypes(typeSpecs, values, location, componentName, getStack) {
|
---|
43 | if (process.env.NODE_ENV !== 'production') {
|
---|
44 | for (var typeSpecName in typeSpecs) {
|
---|
45 | if (has(typeSpecs, typeSpecName)) {
|
---|
46 | var error;
|
---|
47 | // Prop type validation may throw. In case they do, we don't want to
|
---|
48 | // fail the render phase where it didn't fail before. So we log it.
|
---|
49 | // After these have been cleaned up, we'll let them throw.
|
---|
50 | try {
|
---|
51 | // This is intentionally an invariant that gets caught. It's the same
|
---|
52 | // behavior as without this statement except with a better message.
|
---|
53 | if (typeof typeSpecs[typeSpecName] !== 'function') {
|
---|
54 | var err = Error(
|
---|
55 | (componentName || 'React class') + ': ' + location + ' type `' + typeSpecName + '` is invalid; ' +
|
---|
56 | 'it must be a function, usually from the `prop-types` package, but received `' + typeof typeSpecs[typeSpecName] + '`.' +
|
---|
57 | 'This often happens because of typos such as `PropTypes.function` instead of `PropTypes.func`.'
|
---|
58 | );
|
---|
59 | err.name = 'Invariant Violation';
|
---|
60 | throw err;
|
---|
61 | }
|
---|
62 | error = typeSpecs[typeSpecName](values, typeSpecName, componentName, location, null, ReactPropTypesSecret);
|
---|
63 | } catch (ex) {
|
---|
64 | error = ex;
|
---|
65 | }
|
---|
66 | if (error && !(error instanceof Error)) {
|
---|
67 | printWarning(
|
---|
68 | (componentName || 'React class') + ': type specification of ' +
|
---|
69 | location + ' `' + typeSpecName + '` is invalid; the type checker ' +
|
---|
70 | 'function must return `null` or an `Error` but returned a ' + typeof error + '. ' +
|
---|
71 | 'You may have forgotten to pass an argument to the type checker ' +
|
---|
72 | 'creator (arrayOf, instanceOf, objectOf, oneOf, oneOfType, and ' +
|
---|
73 | 'shape all require an argument).'
|
---|
74 | );
|
---|
75 | }
|
---|
76 | if (error instanceof Error && !(error.message in loggedTypeFailures)) {
|
---|
77 | // Only monitor this failure once because there tends to be a lot of the
|
---|
78 | // same error.
|
---|
79 | loggedTypeFailures[error.message] = true;
|
---|
80 |
|
---|
81 | var stack = getStack ? getStack() : '';
|
---|
82 |
|
---|
83 | printWarning(
|
---|
84 | 'Failed ' + location + ' type: ' + error.message + (stack != null ? stack : '')
|
---|
85 | );
|
---|
86 | }
|
---|
87 | }
|
---|
88 | }
|
---|
89 | }
|
---|
90 | }
|
---|
91 |
|
---|
92 | /**
|
---|
93 | * Resets warning cache when testing.
|
---|
94 | *
|
---|
95 | * @private
|
---|
96 | */
|
---|
97 | checkPropTypes.resetWarningCache = function() {
|
---|
98 | if (process.env.NODE_ENV !== 'production') {
|
---|
99 | loggedTypeFailures = {};
|
---|
100 | }
|
---|
101 | }
|
---|
102 |
|
---|
103 | module.exports = checkPropTypes;
|
---|