Remove keyMirror in SpecPolicy (#7590)
As discussed in my RFC: https://www.facebook.com/groups/2003630259862046/permalink/2097088000516271/
Christopher Chedeau committed
Aug 28, 2016 at 10:30 UTC
e3b2c6e650d80bfcfd32eb47354eb269269f77c8
1 file changed
+30
-32
src/isomorphic/classic/class/ReactClass.js
+30
-32
@@ -18,7 +18,6 @@ var ReactNoopUpdateQueue = require('ReactNoopUpdateQueue');
18
19
var emptyObject = require('emptyObject');
20
var invariant = require('invariant');
21
-var keyMirror = require('keyMirror');
21
var keyOf = require('keyOf');
22
var warning = require('warning');
23
@@ -29,27 +28,26 @@ var MIXINS_KEY = keyOf({mixins: null});
28
/**
29
* Policies that describe methods in `ReactClassInterface`.
30
*/
32
-var SpecPolicy = keyMirror({
31
+type SpecPolicy =
32
/**
33
* These methods may be defined only once by the class specification or mixin.
34
*/
36
- DEFINE_ONCE: null,
35
+ 'DEFINE_ONCE' |
36
/**
37
* These methods may be defined by both the class specification and mixins.
38
* Subsequent definitions will be chained. These methods must return void.
39
*/
41
- DEFINE_MANY: null,
40
+ 'DEFINE_MANY' |
41
/**
42
* These methods are overriding the base class.
43
*/
45
- OVERRIDE_BASE: null,
44
+ 'OVERRIDE_BASE' |
45
/**
46
* These methods are similar to DEFINE_MANY, except we assume they return
47
* objects. We try to merge the keys of the return values of all the mixed in
48
* functions. If there is a key conflict we throw.
49
*/
51
- DEFINE_MANY_MERGED: null,
52
-});
50
+ 'DEFINE_MANY_MERGED';
51
52
53
/**
@@ -74,7 +72,7 @@ var SpecPolicy = keyMirror({
72
* @interface ReactClassInterface
73
* @internal
74
*/
77
-var ReactClassInterface = {
75
+var ReactClassInterface: {[key: string]: SpecPolicy} = {
76
77
/**
78
* An array of Mixin objects to include when defining your component.
@@ -82,7 +80,7 @@ var ReactClassInterface = {
80
* @type {array}
81
* @optional
82
*/
85
- mixins: SpecPolicy.DEFINE_MANY,
83
+ mixins: 'DEFINE_MANY',
84
85
/**
86
* An object containing properties and methods that should be defined on
@@ -91,7 +89,7 @@ var ReactClassInterface = {
89
* @type {object}
90
* @optional
91
*/
94
- statics: SpecPolicy.DEFINE_MANY,
92
+ statics: 'DEFINE_MANY',
93
94
/**
95
* Definition of prop types for this component.
@@ -99,7 +97,7 @@ var ReactClassInterface = {
97
* @type {object}
98
* @optional
99
*/
102
- propTypes: SpecPolicy.DEFINE_MANY,
100
+ propTypes: 'DEFINE_MANY',
101
102
/**
103
* Definition of context types for this component.
@@ -107,7 +105,7 @@ var ReactClassInterface = {
105
* @type {object}
106
* @optional
107
*/
110
- contextTypes: SpecPolicy.DEFINE_MANY,
108
+ contextTypes: 'DEFINE_MANY',
109
110
/**
111
* Definition of context types this component sets for its children.
@@ -115,7 +113,7 @@ var ReactClassInterface = {
113
* @type {object}
114
* @optional
115
*/
118
- childContextTypes: SpecPolicy.DEFINE_MANY,
116
+ childContextTypes: 'DEFINE_MANY',
117
118
// ==== Definition methods ====
119
@@ -129,7 +127,7 @@ var ReactClassInterface = {
127
* @return {object}
128
* @optional
129
*/
132
- getDefaultProps: SpecPolicy.DEFINE_MANY_MERGED,
130
+ getDefaultProps: 'DEFINE_MANY_MERGED',
131
132
/**
133
* Invoked once before the component is mounted. The return value will be used
@@ -145,13 +143,13 @@ var ReactClassInterface = {
143
* @return {object}
144
* @optional
145
*/
148
- getInitialState: SpecPolicy.DEFINE_MANY_MERGED,
146
+ getInitialState: 'DEFINE_MANY_MERGED',
147
148
/**
149
* @return {object}
150
* @optional
151
*/
154
- getChildContext: SpecPolicy.DEFINE_MANY_MERGED,
152
+ getChildContext: 'DEFINE_MANY_MERGED',
153
154
/**
155
* Uses props from `this.props` and state from `this.state` to render the
@@ -169,7 +167,7 @@ var ReactClassInterface = {
167
* @nosideeffects
168
* @required
169
*/
172
- render: SpecPolicy.DEFINE_ONCE,
170
+ render: 'DEFINE_ONCE',
171
172
173
@@ -182,7 +180,7 @@ var ReactClassInterface = {
180
*
181
* @optional
182
*/
185
- componentWillMount: SpecPolicy.DEFINE_MANY,
183
+ componentWillMount: 'DEFINE_MANY',
184
185
/**
186
* Invoked when the component has been mounted and has a DOM representation.
@@ -194,7 +192,7 @@ var ReactClassInterface = {
192
* @param {DOMElement} rootNode DOM element representing the component.
193
* @optional
194
*/
197
- componentDidMount: SpecPolicy.DEFINE_MANY,
195
+ componentDidMount: 'DEFINE_MANY',
196
197
/**
198
* Invoked before the component receives new props.
@@ -215,7 +213,7 @@ var ReactClassInterface = {
213
* @param {object} nextProps
214
* @optional
215
*/
218
- componentWillReceiveProps: SpecPolicy.DEFINE_MANY,
216
+ componentWillReceiveProps: 'DEFINE_MANY',
217
218
/**
219
* Invoked while deciding if the component should be updated as a result of
@@ -237,7 +235,7 @@ var ReactClassInterface = {
235
* @return {boolean} True if the component should update.
236
* @optional
237
*/
240
- shouldComponentUpdate: SpecPolicy.DEFINE_ONCE,
238
+ shouldComponentUpdate: 'DEFINE_ONCE',
239
240
/**
241
* Invoked when the component is about to update due to a transition from
@@ -254,7 +252,7 @@ var ReactClassInterface = {
252
* @param {ReactReconcileTransaction} transaction
253
* @optional
254
*/
257
- componentWillUpdate: SpecPolicy.DEFINE_MANY,
255
+ componentWillUpdate: 'DEFINE_MANY',
256
257
/**
258
* Invoked when the component's DOM representation has been updated.
@@ -268,7 +266,7 @@ var ReactClassInterface = {
266
* @param {DOMElement} rootNode DOM element representing the component.
267
* @optional
268
*/
271
- componentDidUpdate: SpecPolicy.DEFINE_MANY,
269
+ componentDidUpdate: 'DEFINE_MANY',
270
271
/**
272
* Invoked when the component is about to be removed from its parent and have
@@ -281,7 +279,7 @@ var ReactClassInterface = {
279
*
280
* @optional
281
*/
284
- componentWillUnmount: SpecPolicy.DEFINE_MANY,
282
+ componentWillUnmount: 'DEFINE_MANY',
283
284
285
@@ -297,7 +295,7 @@ var ReactClassInterface = {
295
* @internal
296
* @overridable
297
*/
300
- updateComponent: SpecPolicy.OVERRIDE_BASE,
298
+ updateComponent: 'OVERRIDE_BASE',
299
300
};
301
@@ -412,7 +410,7 @@ function validateMethodOverride(isAlreadyDefined, name) {
410
// Disallow overriding of base class methods unless explicitly allowed.
411
if (ReactClassMixin.hasOwnProperty(name)) {
412
invariant(
415
- specPolicy === SpecPolicy.OVERRIDE_BASE,
413
+ specPolicy === 'OVERRIDE_BASE',
414
'ReactClassInterface: You are attempting to override ' +
415
'`%s` from your class specification. Ensure that your method names ' +
416
'do not overlap with React methods.',
@@ -423,8 +421,8 @@ function validateMethodOverride(isAlreadyDefined, name) {
421
// Disallow defining methods more than once unless explicitly allowed.
422
if (isAlreadyDefined) {
423
invariant(
426
- specPolicy === SpecPolicy.DEFINE_MANY ||
427
- specPolicy === SpecPolicy.DEFINE_MANY_MERGED,
424
+ specPolicy === 'DEFINE_MANY' ||
425
+ specPolicy === 'DEFINE_MANY_MERGED',
426
'ReactClassInterface: You are attempting to define ' +
427
'`%s` on your component more than once. This conflict may be due ' +
428
'to a mixin.',
@@ -519,8 +517,8 @@ function mixSpecIntoComponent(Constructor, spec) {
517
// These cases should already be caught by validateMethodOverride.
518
invariant(
519
isReactClassMethod && (
522
- specPolicy === SpecPolicy.DEFINE_MANY_MERGED ||
523
- specPolicy === SpecPolicy.DEFINE_MANY
520
+ specPolicy === 'DEFINE_MANY_MERGED' ||
521
+ specPolicy === 'DEFINE_MANY'
522
),
523
'ReactClass: Unexpected spec policy %s for key %s ' +
524
'when mixing in component specs.',
@@ -530,9 +528,9 @@ function mixSpecIntoComponent(Constructor, spec) {
528
529
// For methods which are defined more than once, call the existing
530
// methods before calling the new property, merging if appropriate.
533
- if (specPolicy === SpecPolicy.DEFINE_MANY_MERGED) {
531
+ if (specPolicy === 'DEFINE_MANY_MERGED') {
532
proto[name] = createMergedResultFunction(proto[name], property);
535
- } else if (specPolicy === SpecPolicy.DEFINE_MANY) {
533
+ } else if (specPolicy === 'DEFINE_MANY') {
534
proto[name] = createChainedFunction(proto[name], property);
535
}
536
} else {