2015-03-17 20:42:44 +00:00
|
|
|
/**
|
2018-09-11 22:27:47 +00:00
|
|
|
* Copyright (c) Facebook, Inc. and its affiliates.
|
2015-03-23 22:07:33 +00:00
|
|
|
*
|
2018-02-17 02:24:55 +00:00
|
|
|
* This source code is licensed under the MIT license found in the
|
|
|
|
* LICENSE file in the root directory of this source tree.
|
2015-03-17 20:42:44 +00:00
|
|
|
*
|
2018-05-11 02:06:46 +00:00
|
|
|
* @format
|
2015-03-25 02:34:12 +00:00
|
|
|
* @flow
|
2015-03-17 20:42:44 +00:00
|
|
|
*/
|
2018-05-11 02:06:46 +00:00
|
|
|
|
2015-03-17 20:42:44 +00:00
|
|
|
'use strict';
|
|
|
|
|
2018-09-29 06:13:03 +00:00
|
|
|
const DeprecatedImageStylePropTypes = require('DeprecatedImageStylePropTypes');
|
2018-05-10 22:44:52 +00:00
|
|
|
const TextStylePropTypes = require('TextStylePropTypes');
|
2018-10-01 19:13:06 +00:00
|
|
|
const DeprecatedViewStylePropTypes = require('DeprecatedViewStylePropTypes');
|
2015-03-17 20:42:44 +00:00
|
|
|
|
2018-05-10 22:44:52 +00:00
|
|
|
const invariant = require('fbjs/lib/invariant');
|
2015-03-17 20:42:44 +00:00
|
|
|
|
2017-02-08 22:38:37 +00:00
|
|
|
// 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';
|
|
|
|
|
2015-03-17 20:42:44 +00:00
|
|
|
class StyleSheetValidation {
|
2017-12-07 16:22:47 +00:00
|
|
|
static validateStyleProp(prop: string, style: Object, caller: string) {
|
2018-09-19 23:17:48 +00:00
|
|
|
if (!__DEV__ || global.__RCTProfileIsProfiling) {
|
2015-03-17 20:42:44 +00:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
if (allStylePropTypes[prop] === undefined) {
|
2018-05-10 22:44:52 +00:00
|
|
|
const message1 = '"' + prop + '" is not a valid style property.';
|
2018-05-11 02:06:46 +00:00
|
|
|
const message2 =
|
|
|
|
'\nValid style props: ' +
|
2015-06-15 20:27:20 +00:00
|
|
|
JSON.stringify(Object.keys(allStylePropTypes).sort(), null, ' ');
|
2015-03-17 20:42:44 +00:00
|
|
|
styleError(message1, style, caller, message2);
|
|
|
|
}
|
2018-05-10 22:44:52 +00:00
|
|
|
const error = allStylePropTypes[prop](
|
2015-03-17 20:42:44 +00:00
|
|
|
style,
|
|
|
|
prop,
|
|
|
|
caller,
|
2017-02-08 22:38:37 +00:00
|
|
|
'prop',
|
2016-07-21 16:01:07 +00:00
|
|
|
null,
|
2017-02-08 22:38:37 +00:00
|
|
|
ReactPropTypesSecret,
|
2015-03-17 20:42:44 +00:00
|
|
|
);
|
|
|
|
if (error) {
|
|
|
|
styleError(error.message, style, caller);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-12-07 16:22:47 +00:00
|
|
|
static validateStyle(name: string, styles: Object) {
|
2018-09-19 23:17:48 +00:00
|
|
|
if (!__DEV__ || global.__RCTProfileIsProfiling) {
|
2015-03-17 20:42:44 +00:00
|
|
|
return;
|
|
|
|
}
|
2018-05-10 22:44:52 +00:00
|
|
|
for (const prop in styles[name]) {
|
2018-05-11 02:06:46 +00:00
|
|
|
StyleSheetValidation.validateStyleProp(
|
|
|
|
prop,
|
|
|
|
styles[name],
|
|
|
|
'StyleSheet ' + name,
|
|
|
|
);
|
2015-03-17 20:42:44 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-11-02 07:26:18 +00:00
|
|
|
/* $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. */
|
2015-03-17 20:42:44 +00:00
|
|
|
static addValidStylePropTypes(stylePropTypes) {
|
2018-09-19 23:17:48 +00:00
|
|
|
if (!__DEV__ || global.__RCTProfileIsProfiling) {
|
|
|
|
return;
|
|
|
|
}
|
2018-05-10 22:44:52 +00:00
|
|
|
for (const key in stylePropTypes) {
|
2015-03-17 20:42:44 +00:00
|
|
|
allStylePropTypes[key] = stylePropTypes[key];
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-05-10 22:44:52 +00:00
|
|
|
const styleError = function(message1, style, caller?, message2?) {
|
2015-03-17 20:42:44 +00:00
|
|
|
invariant(
|
|
|
|
false,
|
2018-05-11 02:06:46 +00:00
|
|
|
message1 +
|
|
|
|
'\n' +
|
|
|
|
(caller || '<<unknown>>') +
|
|
|
|
': ' +
|
|
|
|
JSON.stringify(style, null, ' ') +
|
|
|
|
(message2 || ''),
|
2015-03-17 20:42:44 +00:00
|
|
|
);
|
|
|
|
};
|
|
|
|
|
2018-05-10 22:44:52 +00:00
|
|
|
const allStylePropTypes = {};
|
2015-03-17 20:42:44 +00:00
|
|
|
|
2018-09-19 23:17:48 +00:00
|
|
|
if (__DEV__ && !global.__RCTProfileIsProfiling) {
|
2018-09-29 06:13:03 +00:00
|
|
|
StyleSheetValidation.addValidStylePropTypes(DeprecatedImageStylePropTypes);
|
2018-09-19 23:17:48 +00:00
|
|
|
StyleSheetValidation.addValidStylePropTypes(TextStylePropTypes);
|
2018-10-01 19:13:06 +00:00
|
|
|
StyleSheetValidation.addValidStylePropTypes(DeprecatedViewStylePropTypes);
|
2018-09-19 23:17:48 +00:00
|
|
|
}
|
2015-03-17 20:42:44 +00:00
|
|
|
|
|
|
|
module.exports = StyleSheetValidation;
|