Add clearer invariants to composite spec mixing
Ben Alpert committed
Feb 26, 2014 at 21:09 UTC
59d2420b05dad657d232fc5ba9d7b1e8cc9f2949
1 file changed
+22
-8
src/core/ReactCompositeComponent.js
+22
-8
@@ -465,14 +465,15 @@ function mixSpecIntoComponent(ConvenienceConstructor, spec) {
465
// The following member methods should not be automatically bound:
466
// 1. Expected ReactCompositeComponent methods (in the "interface").
467
// 2. Overridden methods (that were mixed in).
468
- var isCompositeComponentMethod = name in ReactCompositeComponentInterface;
469
- var isInherited = name in proto;
468
+ var isCompositeComponentMethod =
469
+ ReactCompositeComponentInterface.hasOwnProperty(name);
470
+ var isAlreadyDefined = proto.hasOwnProperty(name);
471
var markedDontBind = property && property.__reactDontBind;
472
var isFunction = typeof property === 'function';
473
var shouldAutoBind =
474
isFunction &&
475
!isCompositeComponentMethod &&
475
- !isInherited &&
476
+ !isAlreadyDefined &&
477
!markedDontBind;
478
479
if (shouldAutoBind) {
@@ -482,13 +483,26 @@ function mixSpecIntoComponent(ConvenienceConstructor, spec) {
483
proto.__reactAutoBindMap[name] = property;
484
proto[name] = property;
485
} else {
485
- if (isInherited) {
486
+ if (isAlreadyDefined) {
487
+ var specPolicy = ReactCompositeComponentInterface[name];
488
+
489
+ // These cases should already be caught by validateMethodOverride
490
+ invariant(
491
+ isCompositeComponentMethod && (
492
+ specPolicy === SpecPolicy.DEFINE_MANY_MERGED ||
493
+ specPolicy === SpecPolicy.DEFINE_MANY
494
+ ),
495
+ 'ReactCompositeComponent: Unexpected spec policy %s for key %s ' +
496
+ 'when mixing in component specs.',
497
+ specPolicy,
498
+ name
499
+ );
500
+
501
// For methods which are defined more than once, call the existing
487
- // methods before calling the new property.
488
- if (ReactCompositeComponentInterface[name] ===
489
- SpecPolicy.DEFINE_MANY_MERGED) {
502
+ // methods before calling the new property, merging if appropriate.
503
+ if (specPolicy === SpecPolicy.DEFINE_MANY_MERGED) {
504
proto[name] = createMergedResultFunction(proto[name], property);
491
- } else {
505
+ } else if (specPolicy === SpecPolicy.DEFINE_MANY) {
506
proto[name] = createChainedFunction(proto[name], property);
507
}
508
} else {