Flowify Libraries/StyleSheet and Libraries/Text

This commit is contained in:
Marshall Roch 2015-03-24 16:37:54 -07:00
parent eb16bb4dfd
commit a343c4345e
10 changed files with 43 additions and 17 deletions

View File

@ -7,6 +7,7 @@
* of patent rights can be found in the PATENTS file in the same directory.
*
* @providesModule EdgeInsetsPropType
* @flow
*/
'use strict'

View File

@ -7,6 +7,7 @@
* of patent rights can be found in the PATENTS file in the same directory.
*
* @providesModule LayoutPropTypes
* @flow
*/
'use strict';

View File

@ -7,6 +7,7 @@
* of patent rights can be found in the PATENTS file in the same directory.
*
* @providesModule PointPropType
* @flow
*/
'use strict'

View File

@ -7,6 +7,7 @@
* of patent rights can be found in the PATENTS file in the same directory.
*
* @providesModule StyleSheet
* @flow
*/
'use strict';
@ -58,7 +59,7 @@ var StyleSheetValidation = require('StyleSheetValidation');
* subsequent uses are going to refer an id (not implemented yet).
*/
class StyleSheet {
static create(obj) {
static create(obj: {[key: string]: any}): {[key: string]: number} {
var result = {};
for (var key in obj) {
StyleSheetValidation.validateStyle(key, obj);

View File

@ -7,15 +7,18 @@
* of patent rights can be found in the PATENTS file in the same directory.
*
* @providesModule StyleSheetPropType
* @flow
*/
'use strict';
var createStrictShapeTypeChecker = require('createStrictShapeTypeChecker');
var flattenStyle = require('flattenStyle');
function StyleSheetPropType(shape) {
function StyleSheetPropType(
shape: {[key: string]: ReactPropsCheckType}
): ReactPropsCheckType {
var shapePropType = createStrictShapeTypeChecker(shape);
return function(props, propName, componentName, location) {
return function(props, propName, componentName, location?) {
var newProps = props;
if (props[propName]) {
// Just make a dummy prop object with only the flattened style

View File

@ -7,6 +7,7 @@
* of patent rights can be found in the PATENTS file in the same directory.
*
* @providesModule StyleSheetRegistry
* @flow
*/
'use strict';
@ -15,7 +16,7 @@ var uniqueID = 1;
var emptyStyle = {};
class StyleSheetRegistry {
static registerStyle(style) {
static registerStyle(style: Object): number {
var id = ++uniqueID;
if (__DEV__) {
Object.freeze(style);
@ -24,7 +25,7 @@ class StyleSheetRegistry {
return id;
}
static getStyleByID(id) {
static getStyleByID(id: number): Object {
if (!id) {
// Used in the style={[condition && id]} pattern,
// we want it to be a no-op when the value is false or null

View File

@ -7,6 +7,7 @@
* of patent rights can be found in the PATENTS file in the same directory.
*
* @providesModule StyleSheetValidation
* @flow
*/
'use strict';
@ -60,7 +61,7 @@ class StyleSheetValidation {
}
}
var styleError = function(message1, style, caller, message2) {
var styleError = function(message1, style, caller?, message2?) {
invariant(
false,
message1 + '\n' + (caller || '<<unknown>>') + ': ' +

View File

@ -7,12 +7,17 @@
* of patent rights can be found in the PATENTS file in the same directory.
*
* @providesModule flattenStyle
* @flow
*/
'use strict';
var StyleSheetRegistry = require('StyleSheetRegistry');
var invariant = require('invariant');
var mergeIntoFast = require('mergeIntoFast');
type Atom = number | bool | Object | Array<?Atom>
type StyleObj = Atom | Array<?StyleObj>
function getStyle(style) {
if (typeof style === 'number') {
return StyleSheetRegistry.getStyleByID(style);
@ -20,10 +25,14 @@ function getStyle(style) {
return style;
}
function flattenStyle(style) {
// TODO: Flow 0.7.0 doesn't refine bools properly so we have to use `any` to
// tell it that this can't be a bool anymore. Should be fixed in 0.8.0,
// after which this can take a ?StyleObj.
function flattenStyle(style: any): ?Object {
if (!style) {
return undefined;
}
invariant(style !== true, 'style may be false but not true');
if (!Array.isArray(style)) {
return getStyle(style);

View File

@ -7,16 +7,17 @@
* of patent rights can be found in the PATENTS file in the same directory.
*
* @providesModule styleDiffer
* @flow
*/
'use strict';
var deepDiffer = require('deepDiffer');
function styleDiffer(a, b) {
function styleDiffer(a: any, b: any): bool {
return !styleEqual(a, b);
}
function styleEqual(a, b) {
function styleEqual(a: any, b: any): bool {
if (!a) {
return !b;
}

View File

@ -7,7 +7,7 @@
* of patent rights can be found in the PATENTS file in the same directory.
*
* @providesModule Text
* @typechecks static-only
* @flow
*/
'use strict';
@ -102,7 +102,7 @@ var Text = React.createClass({
});
},
onStartShouldSetResponder: function() {
onStartShouldSetResponder: function(): bool {
var shouldSetFromProps = this.props.onStartShouldSetResponder &&
this.props.onStartShouldSetResponder();
return shouldSetFromProps || !!this.props.onPress;
@ -111,7 +111,7 @@ var Text = React.createClass({
/*
* Returns true to allow responder termination
*/
handleResponderTerminationRequest: function() {
handleResponderTerminationRequest: function(): bool {
// Allow touchable or props.onResponderTerminationRequest to deny
// the request
var allowTermination = this.touchableHandleResponderTerminationRequest();
@ -121,25 +121,25 @@ var Text = React.createClass({
return allowTermination;
},
handleResponderGrant: function(e, dispatchID) {
handleResponderGrant: function(e: SyntheticEvent, dispatchID: string) {
this.touchableHandleResponderGrant(e, dispatchID);
this.props.onResponderGrant &&
this.props.onResponderGrant.apply(this, arguments);
},
handleResponderMove: function(e) {
handleResponderMove: function(e: SyntheticEvent) {
this.touchableHandleResponderMove(e);
this.props.onResponderMove &&
this.props.onResponderMove.apply(this, arguments);
},
handleResponderRelease: function(e) {
handleResponderRelease: function(e: SyntheticEvent) {
this.touchableHandleResponderRelease(e);
this.props.onResponderRelease &&
this.props.onResponderRelease.apply(this, arguments);
},
handleResponderTerminate: function(e) {
handleResponderTerminate: function(e: SyntheticEvent) {
this.touchableHandleResponderTerminate(e);
this.props.onResponderTerminate &&
this.props.onResponderTerminate.apply(this, arguments);
@ -167,7 +167,7 @@ var Text = React.createClass({
this.props.onPress && this.props.onPress();
},
touchableGetPressRectOffset: function() {
touchableGetPressRectOffset: function(): RectOffset {
return PRESS_RECT_OFFSET;
},
@ -193,6 +193,13 @@ var Text = React.createClass({
},
});
type RectOffset = {
top: number;
left: number;
right: number;
bottom: number;
}
var PRESS_RECT_OFFSET = {top: 20, left: 20, right: 20, bottom: 30};
var RCTText = createReactIOSNativeComponentClass(viewConfig);