Less verbose ReactPropTypeLocations logging
Since we use keyMirror() and invariant() messages are only shown in __DEV__, we don't have to do manual constant->string translation. Also fixes a few undefined keys that just happened to work before.
Pete Hunt committed
Nov 27, 2013 at 11:57 UTC
fcfe516a2e08322b184ae82db37f38645f131337
2 files changed
+15
-19
src/core/ReactPropTypeLocations.js
+1
-1
@@ -21,7 +21,7 @@
21
var keyMirror = require('keyMirror');
22
23
var ReactPropTypeLocations = keyMirror({
24
- props: null,
24
+ prop: null,
25
context: null,
26
childContext: null
27
});
src/core/ReactPropTypes.js
+14
-18
@@ -18,11 +18,19 @@
18
19
"use strict";
20
21
-var ReactPropTypeLocations = require('ReactPropTypeLocations');
22
-
21
var createObjectFrom = require('createObjectFrom');
22
var invariant = require('invariant');
23
24
+var ReactPropTypeLocationNames = {};
25
+
26
+if (__DEV__) {
27
+ ReactPropTypeLocationNames = {
28
+ prop: 'prop',
29
+ context: 'context',
30
+ childContext: 'child context'
31
+ };
32
+}
33
+
34
/**
35
* Collection of methods that allow declaration and validation of props that are
36
* supplied to React components. Example usage:
@@ -97,10 +105,7 @@ function createPrimitiveTypeChecker(expectedType) {
105
invariant(
106
propType === expectedType,
107
'Invalid %s `%s` of type `%s` supplied to `%s`, expected `%s`.',
100
- (location === ReactPropTypeLocations.prop ? 'prop' :
101
- (location === ReactPropTypeLocations.context ? 'context' :
102
- (location === ReactPropTypeLocations.childContext ? 'child context' :
103
- '<unknown location>'))),
108
+ ReactPropTypeLocationNames[location],
109
propName,
110
propType,
111
componentName,
@@ -116,10 +121,7 @@ function createEnumTypeChecker(expectedValues) {
121
invariant(
122
expectedEnum[propValue],
123
'Invalid %s `%s` supplied to `%s`, expected one of %s.',
119
- (location === ReactPropTypeLocations.prop ? 'prop' :
120
- (location === ReactPropTypeLocations.context ? 'context' :
121
- (location === ReactPropTypeLocations.childContext ? 'child context' :
122
- '<unknown location>'))),
124
+ ReactPropTypeLocationNames[location],
125
propName,
126
componentName,
127
JSON.stringify(Object.keys(expectedEnum))
@@ -133,10 +135,7 @@ function createInstanceTypeChecker(expectedClass) {
135
invariant(
136
propValue instanceof expectedClass,
137
'Invalid %s `%s` supplied to `%s`, expected instance of `%s`.',
136
- (location === ReactPropTypeLocations.prop ? 'prop' :
137
- (location === ReactPropTypeLocations.context ? 'context' :
138
- (location === ReactPropTypeLocations.childContext ? 'child context' :
139
- '<unknown location>'))),
138
+ ReactPropTypeLocationNames[location],
139
propName,
140
componentName,
141
expectedClass.name || ANONYMOUS
@@ -156,10 +155,7 @@ function createChainableTypeChecker(validate) {
155
invariant(
156
!isRequired,
157
'Required %s `%s` was not specified in `%s`.',
159
- (location === ReactPropTypeLocations.prop ?
160
- 'prop' : (location === ReactPropTypeLocations.context ?
161
- 'context' : (location === ReactPropTypeLocations.childContext ?
162
- 'child context' : '<unknown location>'))),
158
+ ReactPropTypeLocationNames[location],
159
propName,
160
componentName || ANONYMOUS
161
);