From dd01e016a6145e8929c79e489fa8e82471420250 Mon Sep 17 00:00:00 2001 From: Christopher Chedeau Date: Sun, 28 Aug 2016 10:30:59 -0700 Subject: [PATCH] Remove keyMirror in SpecPolicy (#7590) As discussed in my RFC: https://www.facebook.com/groups/2003630259862046/permalink/2097088000516271/ (cherry picked from commit e3b2c6e650d80bfcfd32eb47354eb269269f77c8) --- src/isomorphic/classic/class/ReactClass.js | 62 +++++++++++----------- 1 file changed, 30 insertions(+), 32 deletions(-) diff --git a/src/isomorphic/classic/class/ReactClass.js b/src/isomorphic/classic/class/ReactClass.js index 4c5e1315f1..0cb2e7cfef 100644 --- a/src/isomorphic/classic/class/ReactClass.js +++ b/src/isomorphic/classic/class/ReactClass.js @@ -18,7 +18,6 @@ var ReactNoopUpdateQueue = require('ReactNoopUpdateQueue'); var emptyObject = require('emptyObject'); var invariant = require('invariant'); -var keyMirror = require('keyMirror'); var keyOf = require('keyOf'); var warning = require('warning'); @@ -29,27 +28,26 @@ var MIXINS_KEY = keyOf({mixins: null}); /** * Policies that describe methods in `ReactClassInterface`. */ -var SpecPolicy = keyMirror({ +type SpecPolicy = /** * These methods may be defined only once by the class specification or mixin. */ - DEFINE_ONCE: null, + 'DEFINE_ONCE' | /** * These methods may be defined by both the class specification and mixins. * Subsequent definitions will be chained. These methods must return void. */ - DEFINE_MANY: null, + 'DEFINE_MANY' | /** * These methods are overriding the base class. */ - OVERRIDE_BASE: null, + 'OVERRIDE_BASE' | /** * These methods are similar to DEFINE_MANY, except we assume they return * objects. We try to merge the keys of the return values of all the mixed in * functions. If there is a key conflict we throw. */ - DEFINE_MANY_MERGED: null, -}); + 'DEFINE_MANY_MERGED'; var injectedMixins = []; @@ -76,7 +74,7 @@ var injectedMixins = []; * @interface ReactClassInterface * @internal */ -var ReactClassInterface = { +var ReactClassInterface: {[key: string]: SpecPolicy} = { /** * An array of Mixin objects to include when defining your component. @@ -84,7 +82,7 @@ var ReactClassInterface = { * @type {array} * @optional */ - mixins: SpecPolicy.DEFINE_MANY, + mixins: 'DEFINE_MANY', /** * An object containing properties and methods that should be defined on @@ -93,7 +91,7 @@ var ReactClassInterface = { * @type {object} * @optional */ - statics: SpecPolicy.DEFINE_MANY, + statics: 'DEFINE_MANY', /** * Definition of prop types for this component. @@ -101,7 +99,7 @@ var ReactClassInterface = { * @type {object} * @optional */ - propTypes: SpecPolicy.DEFINE_MANY, + propTypes: 'DEFINE_MANY', /** * Definition of context types for this component. @@ -109,7 +107,7 @@ var ReactClassInterface = { * @type {object} * @optional */ - contextTypes: SpecPolicy.DEFINE_MANY, + contextTypes: 'DEFINE_MANY', /** * Definition of context types this component sets for its children. @@ -117,7 +115,7 @@ var ReactClassInterface = { * @type {object} * @optional */ - childContextTypes: SpecPolicy.DEFINE_MANY, + childContextTypes: 'DEFINE_MANY', // ==== Definition methods ==== @@ -131,7 +129,7 @@ var ReactClassInterface = { * @return {object} * @optional */ - getDefaultProps: SpecPolicy.DEFINE_MANY_MERGED, + getDefaultProps: 'DEFINE_MANY_MERGED', /** * Invoked once before the component is mounted. The return value will be used @@ -147,13 +145,13 @@ var ReactClassInterface = { * @return {object} * @optional */ - getInitialState: SpecPolicy.DEFINE_MANY_MERGED, + getInitialState: 'DEFINE_MANY_MERGED', /** * @return {object} * @optional */ - getChildContext: SpecPolicy.DEFINE_MANY_MERGED, + getChildContext: 'DEFINE_MANY_MERGED', /** * Uses props from `this.props` and state from `this.state` to render the @@ -171,7 +169,7 @@ var ReactClassInterface = { * @nosideeffects * @required */ - render: SpecPolicy.DEFINE_ONCE, + render: 'DEFINE_ONCE', @@ -184,7 +182,7 @@ var ReactClassInterface = { * * @optional */ - componentWillMount: SpecPolicy.DEFINE_MANY, + componentWillMount: 'DEFINE_MANY', /** * Invoked when the component has been mounted and has a DOM representation. @@ -196,7 +194,7 @@ var ReactClassInterface = { * @param {DOMElement} rootNode DOM element representing the component. * @optional */ - componentDidMount: SpecPolicy.DEFINE_MANY, + componentDidMount: 'DEFINE_MANY', /** * Invoked before the component receives new props. @@ -217,7 +215,7 @@ var ReactClassInterface = { * @param {object} nextProps * @optional */ - componentWillReceiveProps: SpecPolicy.DEFINE_MANY, + componentWillReceiveProps: 'DEFINE_MANY', /** * Invoked while deciding if the component should be updated as a result of @@ -239,7 +237,7 @@ var ReactClassInterface = { * @return {boolean} True if the component should update. * @optional */ - shouldComponentUpdate: SpecPolicy.DEFINE_ONCE, + shouldComponentUpdate: 'DEFINE_ONCE', /** * Invoked when the component is about to update due to a transition from @@ -256,7 +254,7 @@ var ReactClassInterface = { * @param {ReactReconcileTransaction} transaction * @optional */ - componentWillUpdate: SpecPolicy.DEFINE_MANY, + componentWillUpdate: 'DEFINE_MANY', /** * Invoked when the component's DOM representation has been updated. @@ -270,7 +268,7 @@ var ReactClassInterface = { * @param {DOMElement} rootNode DOM element representing the component. * @optional */ - componentDidUpdate: SpecPolicy.DEFINE_MANY, + componentDidUpdate: 'DEFINE_MANY', /** * Invoked when the component is about to be removed from its parent and have @@ -283,7 +281,7 @@ var ReactClassInterface = { * * @optional */ - componentWillUnmount: SpecPolicy.DEFINE_MANY, + componentWillUnmount: 'DEFINE_MANY', @@ -299,7 +297,7 @@ var ReactClassInterface = { * @internal * @overridable */ - updateComponent: SpecPolicy.OVERRIDE_BASE, + updateComponent: 'OVERRIDE_BASE', }; @@ -414,7 +412,7 @@ function validateMethodOverride(isAlreadyDefined, name) { // Disallow overriding of base class methods unless explicitly allowed. if (ReactClassMixin.hasOwnProperty(name)) { invariant( - specPolicy === SpecPolicy.OVERRIDE_BASE, + specPolicy === 'OVERRIDE_BASE', 'ReactClassInterface: You are attempting to override ' + '`%s` from your class specification. Ensure that your method names ' + 'do not overlap with React methods.', @@ -425,8 +423,8 @@ function validateMethodOverride(isAlreadyDefined, name) { // Disallow defining methods more than once unless explicitly allowed. if (isAlreadyDefined) { invariant( - specPolicy === SpecPolicy.DEFINE_MANY || - specPolicy === SpecPolicy.DEFINE_MANY_MERGED, + specPolicy === 'DEFINE_MANY' || + specPolicy === 'DEFINE_MANY_MERGED', 'ReactClassInterface: You are attempting to define ' + '`%s` on your component more than once. This conflict may be due ' + 'to a mixin.', @@ -521,8 +519,8 @@ function mixSpecIntoComponent(Constructor, spec) { // These cases should already be caught by validateMethodOverride. invariant( isReactClassMethod && ( - specPolicy === SpecPolicy.DEFINE_MANY_MERGED || - specPolicy === SpecPolicy.DEFINE_MANY + specPolicy === 'DEFINE_MANY_MERGED' || + specPolicy === 'DEFINE_MANY' ), 'ReactClass: Unexpected spec policy %s for key %s ' + 'when mixing in component specs.', @@ -532,9 +530,9 @@ function mixSpecIntoComponent(Constructor, spec) { // For methods which are defined more than once, call the existing // methods before calling the new property, merging if appropriate. - if (specPolicy === SpecPolicy.DEFINE_MANY_MERGED) { + if (specPolicy === 'DEFINE_MANY_MERGED') { proto[name] = createMergedResultFunction(proto[name], property); - } else if (specPolicy === SpecPolicy.DEFINE_MANY) { + } else if (specPolicy === 'DEFINE_MANY') { proto[name] = createChainedFunction(proto[name], property); } } else {