UNPKG

1.37 kBJavaScriptView Raw
1const ReactPropTypesSecret = 'SECRET_DO_NOT_PASS_THIS_OR_YOU_WILL_BE_FIRED';
2
3let loggedTypeFailures = {};
4
5/**
6 * Reset the history of which prop type warnings have been logged.
7 */
8export function resetPropWarnings() {
9 loggedTypeFailures = {};
10}
11
12/**
13 * Assert that the values match with the type specs.
14 * Error messages are memorized and will only be shown once.
15 *
16 * Adapted from https://github.com/facebook/prop-types/blob/master/checkPropTypes.js
17 *
18 * @param {object} typeSpecs Map of name to a ReactPropType
19 * @param {object} values Runtime values that need to be type-checked
20 * @param {string} location e.g. "prop", "context", "child context"
21 * @param {string} componentName Name of the component for error messages.
22 * @param {?Function} getStack Returns the component stack.
23 */
24export function checkPropTypes(
25 typeSpecs,
26 values,
27 location,
28 componentName,
29 getStack
30) {
31 Object.keys(typeSpecs).forEach(typeSpecName => {
32 let error;
33 try {
34 error = typeSpecs[typeSpecName](
35 values,
36 typeSpecName,
37 componentName,
38 location,
39 null,
40 ReactPropTypesSecret
41 );
42 } catch (e) {
43 error = e;
44 }
45 if (error && !(error.message in loggedTypeFailures)) {
46 loggedTypeFailures[error.message] = true;
47 console.error(
48 `Failed ${location} type: ${error.message}${(getStack &&
49 `\n${getStack()}`) ||
50 ''}`
51 );
52 }
53 });
54}