react-native-ui-lib
Version:
[](https://travis-ci.org/wix/react-native-ui-lib) [](https://www.npmjs.com/package/react-native-ui-lib) [![NPM Down
58 lines (50 loc) • 1.81 kB
JavaScript
const colorProps = [
'color', 'backgroundColor', 'borderColor', 'borderRightColor',
'borderBottomColor', 'borderEndColor', 'borderLeftColor', 'borderStartColor',
'borderTopColor', 'textShadowColor', 'textDecorationColor', 'tintColor',
'placeholderTextColor', 'selectionColor', 'underlineColorAndroid',
];
const colorExceptions = [
'transparent', 'rgba(0,0,0,0)',
];
function findAndReportHardCodedValues(value, reporter, scope, depthOfSearch = 4) {
if (depthOfSearch === 0) return;
if (value === undefined || value === false) return;
if (isLiteral(value.type)) {
reporter(value);
} else if (value.type === 'ConditionalExpression') {
findAndReportHardCodedValues(value.consequent, reporter, scope, depthOfSearch - 1);
findAndReportHardCodedValues(value.alternate, reporter, scope, depthOfSearch - 1);
} else if (value.type === 'Identifier') {
findAndReportHardCodedValues(findValueNodeOfIdentifier(value.name, scope), reporter, scope, depthOfSearch - 1);
}
}
function propIsColor(propName) {
return colorProps.indexOf(propName) !== -1;
}
function isLiteral(type) {
return (type === 'Literal' || type === 'TemplateLiteral');
}
function isColorException(colorString) {
const lowerCaseColorString = colorString.toLowerCase();
return colorExceptions.indexOf(lowerCaseColorString) !== -1;
}
function findValueNodeOfIdentifier(identifierName, scope) {
const varsInScope = scope.variables;
let valueNode = false;
varsInScope.forEach((variable) => {
if (variable.name === identifierName) {
if (variable.defs) {
valueNode = variable.defs[variable.defs.length - 1].node.init;
}
}
});
return valueNode;
}
module.exports = {
findAndReportHardCodedValues,
propIsColor,
isLiteral,
isColorException,
findValueNodeOfIdentifier,
};