|
| 1 | +/** |
| 2 | + * @fileoverview Prevent common casing typos |
| 3 | + */ |
| 4 | +'use strict'; |
| 5 | + |
| 6 | +const Components = require('../util/Components'); |
| 7 | + |
| 8 | +// ------------------------------------------------------------------------------ |
| 9 | +// Rule Definition |
| 10 | +// ------------------------------------------------------------------------------ |
| 11 | + |
| 12 | +const STATIC_CLASS_PROPERTIES = ['propTypes', 'contextTypes', 'childContextTypes', 'defaultProps']; |
| 13 | + |
| 14 | +module.exports = { |
| 15 | + meta: { |
| 16 | + docs: { |
| 17 | + description: 'Prevent common casing typos', |
| 18 | + category: 'Stylistic Issues', |
| 19 | + recommended: false |
| 20 | + }, |
| 21 | + schema: [] |
| 22 | + }, |
| 23 | + |
| 24 | + create: Components.detect(function(context, components, utils) { |
| 25 | + function reportErrorIfCasingTypo(node, propertyName) { |
| 26 | + STATIC_CLASS_PROPERTIES.forEach(function(CLASS_PROP) { |
| 27 | + if (propertyName && CLASS_PROP.toLowerCase() === propertyName.toLowerCase() && CLASS_PROP !== propertyName) { |
| 28 | + context.report({ |
| 29 | + node: node, |
| 30 | + message: 'Typo in static class property declaration' |
| 31 | + }); |
| 32 | + } |
| 33 | + }); |
| 34 | + } |
| 35 | + |
| 36 | + return { |
| 37 | + ClassProperty: function(node) { |
| 38 | + if (!node.static || !utils.isES6Component(node.parent.parent)) { |
| 39 | + return; |
| 40 | + } |
| 41 | + |
| 42 | + const tokens = context.getFirstTokens(node, 2); |
| 43 | + const propertyName = tokens[1].value; |
| 44 | + reportErrorIfCasingTypo(node, propertyName); |
| 45 | + }, |
| 46 | + |
| 47 | + MemberExpression: function(node) { |
| 48 | + const relatedComponent = utils.getRelatedComponent(node); |
| 49 | + |
| 50 | + if ( |
| 51 | + relatedComponent && |
| 52 | + (utils.isES6Component(relatedComponent.node) || utils.isReturningJSX(relatedComponent.node)) |
| 53 | + ) { |
| 54 | + const propertyName = node.property.name; |
| 55 | + reportErrorIfCasingTypo(node, propertyName); |
| 56 | + } |
| 57 | + } |
| 58 | + }; |
| 59 | + }) |
| 60 | +}; |
0 commit comments