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
55 lines (48 loc) • 1.66 kB
JavaScript
/**
* @fileoverview Rule to disallow hard coded font style
* @author Inbal Tish
*/
// ------------------------------------------------------------------------------
// Rule Definition
// ------------------------------------------------------------------------------
const utils = require('../utils')
const { findAndReportHardCodedValues } = utils
module.exports = {
meta: {
docs: {
description: 'disallow hard coded font style',
category: 'Best Practices',
recommended: true
},
messages: {
avoidName: 'Please do not use hard coded fontSize prop in style objects, instead use Typography presets'
},
fixable: 'code',
schema: [] // no options
},
create (context) {
function reportAndFixHardCodedFont (node) {
context.report({
node,
message: 'Please do not use hard coded fontSize prop in style objects, instead use Typography presets'
})
}
function isPropFont (propName) {
return (['fontSize', 'fontWeight', 'lineHeight', 'fontFamily'].indexOf(propName) !== -1)
}
function noHardCodedFont (node) {
node.properties.forEach((property) => {
if (property.key) {
const propName = property.key.name
if (isPropFont(propName)) {
findAndReportHardCodedValues(property.value, reportAndFixHardCodedFont, context.getScope())
}
}
})
}
return {
'CallExpression[callee.object.name=StyleSheet][callee.property.name=create] ObjectExpression': node => noHardCodedFont(node),
'JSXAttribute[name.name = style] ObjectExpression': node => noHardCodedFont(node)
}
}
}