Skip to content

Commit

Permalink
Flowify Libraries/StyleSheet and Libraries/Text
Browse files Browse the repository at this point in the history
  • Loading branch information
mroch committed Mar 25, 2015
1 parent eb16bb4 commit a343c43
Show file tree
Hide file tree
Showing 10 changed files with 43 additions and 17 deletions.
1 change: 1 addition & 0 deletions Libraries/StyleSheet/EdgeInsetsPropType.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
* of patent rights can be found in the PATENTS file in the same directory.
*
* @providesModule EdgeInsetsPropType
* @flow
*/
'use strict'

Expand Down
1 change: 1 addition & 0 deletions Libraries/StyleSheet/LayoutPropTypes.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
* of patent rights can be found in the PATENTS file in the same directory.
*
* @providesModule LayoutPropTypes
* @flow
*/
'use strict';

Expand Down
1 change: 1 addition & 0 deletions Libraries/StyleSheet/PointPropType.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
* of patent rights can be found in the PATENTS file in the same directory.
*
* @providesModule PointPropType
* @flow
*/
'use strict'

Expand Down
3 changes: 2 additions & 1 deletion Libraries/StyleSheet/StyleSheet.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
* of patent rights can be found in the PATENTS file in the same directory.
*
* @providesModule StyleSheet
* @flow
*/
'use strict';

Expand Down Expand Up @@ -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);
Expand Down
7 changes: 5 additions & 2 deletions Libraries/StyleSheet/StyleSheetPropType.js
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
5 changes: 3 additions & 2 deletions Libraries/StyleSheet/StyleSheetRegistry.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
* of patent rights can be found in the PATENTS file in the same directory.
*
* @providesModule StyleSheetRegistry
* @flow
*/
'use strict';

Expand All @@ -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);
Expand All @@ -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
Expand Down
3 changes: 2 additions & 1 deletion Libraries/StyleSheet/StyleSheetValidation.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
* of patent rights can be found in the PATENTS file in the same directory.
*
* @providesModule StyleSheetValidation
* @flow
*/
'use strict';

Expand Down Expand Up @@ -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>>') + ': ' +
Expand Down
11 changes: 10 additions & 1 deletion Libraries/StyleSheet/flattenStyle.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,23 +7,32 @@
* 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);
}
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);
Expand Down
5 changes: 3 additions & 2 deletions Libraries/StyleSheet/styleDiffer.js
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}
Expand Down
23 changes: 15 additions & 8 deletions Libraries/Text/Text.js
Original file line number Diff line number Diff line change
Expand Up @@ -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';

Expand Down Expand Up @@ -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;
Expand All @@ -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();
Expand All @@ -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);
Expand Down Expand Up @@ -167,7 +167,7 @@ var Text = React.createClass({
this.props.onPress && this.props.onPress();
},

touchableGetPressRectOffset: function() {
touchableGetPressRectOffset: function(): RectOffset {
return PRESS_RECT_OFFSET;
},

Expand All @@ -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);
Expand Down

0 comments on commit a343c43

Please sign in to comment.