no-typos.js 8.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257
  1. /**
  2. * @fileoverview Prevent common casing typos
  3. */
  4. 'use strict';
  5. const PROP_TYPES = Object.keys(require('prop-types'));
  6. const Components = require('../util/Components');
  7. const docsUrl = require('../util/docsUrl');
  8. const componentUtil = require('../util/componentUtil');
  9. const report = require('../util/report');
  10. const lifecycleMethods = require('../util/lifecycleMethods');
  11. // ------------------------------------------------------------------------------
  12. // Rule Definition
  13. // ------------------------------------------------------------------------------
  14. const STATIC_CLASS_PROPERTIES = ['propTypes', 'contextTypes', 'childContextTypes', 'defaultProps'];
  15. const messages = {
  16. typoPropTypeChain: 'Typo in prop type chain qualifier: {{name}}',
  17. typoPropType: 'Typo in declared prop type: {{name}}',
  18. typoStaticClassProp: 'Typo in static class property declaration',
  19. typoPropDeclaration: 'Typo in property declaration',
  20. typoLifecycleMethod: 'Typo in component lifecycle method declaration: {{actual}} should be {{expected}}',
  21. staticLifecycleMethod: 'Lifecycle method should be static: {{method}}',
  22. noPropTypesBinding: '`\'prop-types\'` imported without a local `PropTypes` binding.',
  23. noReactBinding: '`\'react\'` imported without a local `React` binding.',
  24. };
  25. module.exports = {
  26. meta: {
  27. docs: {
  28. description: 'Disallow common typos',
  29. category: 'Stylistic Issues',
  30. recommended: false,
  31. url: docsUrl('no-typos'),
  32. },
  33. messages,
  34. schema: [],
  35. },
  36. create: Components.detect((context, components, utils) => {
  37. let propTypesPackageName = null;
  38. let reactPackageName = null;
  39. function checkValidPropTypeQualifier(node) {
  40. if (node.name !== 'isRequired') {
  41. report(context, messages.typoPropTypeChain, 'typoPropTypeChain', {
  42. node,
  43. data: { name: node.name },
  44. });
  45. }
  46. }
  47. function checkValidPropType(node) {
  48. if (node.name && !PROP_TYPES.some((propTypeName) => propTypeName === node.name)) {
  49. report(context, messages.typoPropType, 'typoPropType', {
  50. node,
  51. data: { name: node.name },
  52. });
  53. }
  54. }
  55. function isPropTypesPackage(node) {
  56. return (
  57. node.type === 'Identifier'
  58. && node.name === propTypesPackageName
  59. ) || (
  60. node.type === 'MemberExpression'
  61. && node.property.name === 'PropTypes'
  62. && node.object.name === reactPackageName
  63. );
  64. }
  65. /* eslint-disable no-use-before-define */
  66. function checkValidCallExpression(node) {
  67. const callee = node.callee;
  68. if (callee.type === 'MemberExpression' && callee.property.name === 'shape') {
  69. checkValidPropObject(node.arguments[0]);
  70. } else if (callee.type === 'MemberExpression' && callee.property.name === 'oneOfType') {
  71. const args = node.arguments[0];
  72. if (args && args.type === 'ArrayExpression') {
  73. args.elements.forEach((el) => {
  74. checkValidProp(el);
  75. });
  76. }
  77. }
  78. }
  79. function checkValidProp(node) {
  80. if ((!propTypesPackageName && !reactPackageName) || !node) {
  81. return;
  82. }
  83. if (node.type === 'MemberExpression') {
  84. if (
  85. node.object.type === 'MemberExpression'
  86. && isPropTypesPackage(node.object.object)
  87. ) { // PropTypes.myProp.isRequired
  88. checkValidPropType(node.object.property);
  89. checkValidPropTypeQualifier(node.property);
  90. } else if (
  91. isPropTypesPackage(node.object)
  92. && node.property.name !== 'isRequired'
  93. ) { // PropTypes.myProp
  94. checkValidPropType(node.property);
  95. } else if (node.object.type === 'CallExpression') {
  96. checkValidPropTypeQualifier(node.property);
  97. checkValidCallExpression(node.object);
  98. }
  99. } else if (node.type === 'CallExpression') {
  100. checkValidCallExpression(node);
  101. }
  102. }
  103. /* eslint-enable no-use-before-define */
  104. function checkValidPropObject(node) {
  105. if (node && node.type === 'ObjectExpression') {
  106. node.properties.forEach((prop) => checkValidProp(prop.value));
  107. }
  108. }
  109. function reportErrorIfPropertyCasingTypo(propertyValue, propertyKey, isClassProperty) {
  110. const propertyName = propertyKey.name;
  111. if (propertyName === 'propTypes' || propertyName === 'contextTypes' || propertyName === 'childContextTypes') {
  112. checkValidPropObject(propertyValue);
  113. }
  114. STATIC_CLASS_PROPERTIES.forEach((CLASS_PROP) => {
  115. if (propertyName && CLASS_PROP.toLowerCase() === propertyName.toLowerCase() && CLASS_PROP !== propertyName) {
  116. const messageId = isClassProperty
  117. ? 'typoStaticClassProp'
  118. : 'typoPropDeclaration';
  119. report(context, messages[messageId], messageId, {
  120. node: propertyKey,
  121. });
  122. }
  123. });
  124. }
  125. function reportErrorIfLifecycleMethodCasingTypo(node) {
  126. const key = node.key;
  127. let nodeKeyName = key.name;
  128. if (key.type === 'Literal') {
  129. nodeKeyName = key.value;
  130. }
  131. if (key.type === 'PrivateName' || (node.computed && typeof nodeKeyName !== 'string')) {
  132. return;
  133. }
  134. lifecycleMethods.static.forEach((method) => {
  135. if (!node.static && nodeKeyName && nodeKeyName.toLowerCase() === method.toLowerCase()) {
  136. report(context, messages.staticLifecycleMethod, 'staticLifecycleMethod', {
  137. node,
  138. data: {
  139. method: nodeKeyName,
  140. },
  141. });
  142. }
  143. });
  144. lifecycleMethods.instance.concat(lifecycleMethods.static).forEach((method) => {
  145. if (nodeKeyName && method.toLowerCase() === nodeKeyName.toLowerCase() && method !== nodeKeyName) {
  146. report(context, messages.typoLifecycleMethod, 'typoLifecycleMethod', {
  147. node,
  148. data: { actual: nodeKeyName, expected: method },
  149. });
  150. }
  151. });
  152. }
  153. return {
  154. ImportDeclaration(node) {
  155. if (node.source && node.source.value === 'prop-types') { // import PropType from "prop-types"
  156. if (node.specifiers.length > 0) {
  157. propTypesPackageName = node.specifiers[0].local.name;
  158. } else {
  159. report(context, messages.noPropTypesBinding, 'noPropTypesBinding', {
  160. node,
  161. });
  162. }
  163. } else if (node.source && node.source.value === 'react') { // import { PropTypes } from "react"
  164. if (node.specifiers.length > 0) {
  165. reactPackageName = node.specifiers[0].local.name; // guard against accidental anonymous `import "react"`
  166. } else {
  167. report(context, messages.noReactBinding, 'noReactBinding', {
  168. node,
  169. });
  170. }
  171. if (node.specifiers.length >= 1) {
  172. const propTypesSpecifier = node.specifiers.find((specifier) => (
  173. specifier.imported && specifier.imported.name === 'PropTypes'
  174. ));
  175. if (propTypesSpecifier) {
  176. propTypesPackageName = propTypesSpecifier.local.name;
  177. }
  178. }
  179. }
  180. },
  181. 'ClassProperty, PropertyDefinition'(node) {
  182. if (!node.static || !componentUtil.isES6Component(node.parent.parent, context)) {
  183. return;
  184. }
  185. reportErrorIfPropertyCasingTypo(node.value, node.key, true);
  186. },
  187. MemberExpression(node) {
  188. const propertyName = node.property.name;
  189. if (
  190. !propertyName
  191. || STATIC_CLASS_PROPERTIES.map((prop) => prop.toLocaleLowerCase()).indexOf(propertyName.toLowerCase()) === -1
  192. ) {
  193. return;
  194. }
  195. const relatedComponent = utils.getRelatedComponent(node);
  196. if (
  197. relatedComponent
  198. && (componentUtil.isES6Component(relatedComponent.node, context) || (
  199. relatedComponent.node.type !== 'ClassDeclaration' && utils.isReturningJSX(relatedComponent.node)))
  200. && (node.parent && node.parent.type === 'AssignmentExpression' && node.parent.right)
  201. ) {
  202. reportErrorIfPropertyCasingTypo(node.parent.right, node.property, true);
  203. }
  204. },
  205. MethodDefinition(node) {
  206. if (!componentUtil.isES6Component(node.parent.parent, context)) {
  207. return;
  208. }
  209. reportErrorIfLifecycleMethodCasingTypo(node);
  210. },
  211. ObjectExpression(node) {
  212. const component = componentUtil.isES5Component(node, context) && components.get(node);
  213. if (!component) {
  214. return;
  215. }
  216. node.properties.filter((property) => property.type !== 'SpreadElement').forEach((property) => {
  217. reportErrorIfPropertyCasingTypo(property.value, property.key, false);
  218. reportErrorIfLifecycleMethodCasingTypo(property);
  219. });
  220. },
  221. };
  222. }),
  223. };