react-native/Libraries/StyleSheet/StyleSheetValidation.js

97 lines
2.8 KiB
JavaScript
Raw Normal View History

/**
* Copyright (c) Facebook, Inc. and its affiliates.
*
* This source code is licensed under the MIT license found in the
* LICENSE file in the root directory of this source tree.
*
* @format
2015-03-25 02:34:12 +00:00
* @flow
*/
'use strict';
const DeprecatedImageStylePropTypes = require('DeprecatedImageStylePropTypes');
const TextStylePropTypes = require('TextStylePropTypes');
const DeprecatedViewStylePropTypes = require('DeprecatedViewStylePropTypes');
const invariant = require('fbjs/lib/invariant');
// Hardcoded because this is a legit case but we don't want to load it from
// a private API. We might likely want to unify style sheet creation with how it
// is done in the DOM so this might move into React. I know what I'm doing so
// plz don't fire me.
const ReactPropTypesSecret = 'SECRET_DO_NOT_PASS_THIS_OR_YOU_WILL_BE_FIRED';
class StyleSheetValidation {
Fix some flow errors that appear Summary: <!-- Thank you for sending the PR! We appreciate you spending the time to work on these changes. Help us understand your motivation by explaining why you decided to make this change. You can learn more about contributing to React Native here: http://facebook.github.io/react-native/docs/contributing.html Happy contributing! --> There are some errors I noticed after upgrading my flow version. I had them in my project, and they were also reported in #11655. These errors were ignored on master, but I went through and fixed them so the static analysis will work still. After these changes, I receive no errors using flow `0.60.1` on latest master, or using `0.59` in my local project (which does not have the requisite ignores included). - init a new project `react-native init --version <path-to-repo> helloworld` - add the flow binary `yarn add -D flow-bin` - run flow `yarn flow` - make sure there are no errors in the project. ``` No errors! ✨ Done in 23.60s. ``` <!-- Help reviewers and the release process by writing your own release notes **INTERNAL and MINOR tagged notes will not be included in the next version's final release notes.** CATEGORY [----------] TYPE [ CLI ] [-------------] LOCATION [ DOCS ] [ BREAKING ] [-------------] [ GENERAL ] [ BUGFIX ] [-{Component}-] [ INTERNAL ] [ ENHANCEMENT ] [ {File} ] [ IOS ] [ FEATURE ] [ {Directory} ] |-----------| [ ANDROID ] [ MINOR ] [ {Framework} ] - | {Message} | [----------] [-------------] [-------------] |-----------| [CATEGORY] [TYPE] [LOCATION] - MESSAGE EXAMPLES: [IOS] [BREAKING] [FlatList] - Change a thing that breaks other things [ANDROID] [BUGFIX] [TextInput] - Did a thing to TextInput [CLI] [FEATURE] [local-cli/info/info.js] - CLI easier to do things with [DOCS] [BUGFIX] [GettingStarted.md] - Accidentally a thing/word [GENERAL] [ENHANCEMENT] [Yoga] - Added new yoga thing/position [INTERNAL] [FEATURE] [./scripts] - Added thing to script that nobody will see --> [GENERAL][BUGFIX][./Libraries] - Fix up some flow definitions Closes https://github.com/facebook/react-native/pull/17086 Differential Revision: D6509112 Pulled By: hramos fbshipit-source-id: a61145b5306c666ab6510ccb9eea02d96f3decb3
2017-12-07 16:22:47 +00:00
static validateStyleProp(prop: string, style: Object, caller: string) {
if (!__DEV__ || global.__RCTProfileIsProfiling) {
return;
}
if (allStylePropTypes[prop] === undefined) {
const message1 = '"' + prop + '" is not a valid style property.';
const message2 =
'\nValid style props: ' +
JSON.stringify(Object.keys(allStylePropTypes).sort(), null, ' ');
styleError(message1, style, caller, message2);
}
const error = allStylePropTypes[prop](
style,
prop,
caller,
'prop',
null,
ReactPropTypesSecret,
);
if (error) {
styleError(error.message, style, caller);
}
}
Fix some flow errors that appear Summary: <!-- Thank you for sending the PR! We appreciate you spending the time to work on these changes. Help us understand your motivation by explaining why you decided to make this change. You can learn more about contributing to React Native here: http://facebook.github.io/react-native/docs/contributing.html Happy contributing! --> There are some errors I noticed after upgrading my flow version. I had them in my project, and they were also reported in #11655. These errors were ignored on master, but I went through and fixed them so the static analysis will work still. After these changes, I receive no errors using flow `0.60.1` on latest master, or using `0.59` in my local project (which does not have the requisite ignores included). - init a new project `react-native init --version <path-to-repo> helloworld` - add the flow binary `yarn add -D flow-bin` - run flow `yarn flow` - make sure there are no errors in the project. ``` No errors! ✨ Done in 23.60s. ``` <!-- Help reviewers and the release process by writing your own release notes **INTERNAL and MINOR tagged notes will not be included in the next version's final release notes.** CATEGORY [----------] TYPE [ CLI ] [-------------] LOCATION [ DOCS ] [ BREAKING ] [-------------] [ GENERAL ] [ BUGFIX ] [-{Component}-] [ INTERNAL ] [ ENHANCEMENT ] [ {File} ] [ IOS ] [ FEATURE ] [ {Directory} ] |-----------| [ ANDROID ] [ MINOR ] [ {Framework} ] - | {Message} | [----------] [-------------] [-------------] |-----------| [CATEGORY] [TYPE] [LOCATION] - MESSAGE EXAMPLES: [IOS] [BREAKING] [FlatList] - Change a thing that breaks other things [ANDROID] [BUGFIX] [TextInput] - Did a thing to TextInput [CLI] [FEATURE] [local-cli/info/info.js] - CLI easier to do things with [DOCS] [BUGFIX] [GettingStarted.md] - Accidentally a thing/word [GENERAL] [ENHANCEMENT] [Yoga] - Added new yoga thing/position [INTERNAL] [FEATURE] [./scripts] - Added thing to script that nobody will see --> [GENERAL][BUGFIX][./Libraries] - Fix up some flow definitions Closes https://github.com/facebook/react-native/pull/17086 Differential Revision: D6509112 Pulled By: hramos fbshipit-source-id: a61145b5306c666ab6510ccb9eea02d96f3decb3
2017-12-07 16:22:47 +00:00
static validateStyle(name: string, styles: Object) {
if (!__DEV__ || global.__RCTProfileIsProfiling) {
return;
}
for (const prop in styles[name]) {
StyleSheetValidation.validateStyleProp(
prop,
styles[name],
'StyleSheet ' + name,
);
}
}
/* $FlowFixMe(>=0.85.0 site=react_native_fb) This comment suppresses an error
* found when Flow v0.85 was deployed. To see the error, delete this comment
* and run Flow. */
static addValidStylePropTypes(stylePropTypes) {
if (!__DEV__ || global.__RCTProfileIsProfiling) {
return;
}
for (const key in stylePropTypes) {
allStylePropTypes[key] = stylePropTypes[key];
}
}
}
const styleError = function(message1, style, caller?, message2?) {
invariant(
false,
message1 +
'\n' +
(caller || '<<unknown>>') +
': ' +
JSON.stringify(style, null, ' ') +
(message2 || ''),
);
};
const allStylePropTypes = {};
if (__DEV__ && !global.__RCTProfileIsProfiling) {
StyleSheetValidation.addValidStylePropTypes(DeprecatedImageStylePropTypes);
StyleSheetValidation.addValidStylePropTypes(TextStylePropTypes);
StyleSheetValidation.addValidStylePropTypes(DeprecatedViewStylePropTypes);
}
module.exports = StyleSheetValidation;