no-typos.js 8.6 KB

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