display-name.js 6.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238
  1. /**
  2. * @fileoverview Prevent missing displayName in a React component definition
  3. * @author Yannick Croissant
  4. */
  5. 'use strict';
  6. const Components = require('../util/Components');
  7. const astUtil = require('../util/ast');
  8. const docsUrl = require('../util/docsUrl');
  9. const propsUtil = require('../util/props');
  10. // ------------------------------------------------------------------------------
  11. // Rule Definition
  12. // ------------------------------------------------------------------------------
  13. module.exports = {
  14. meta: {
  15. docs: {
  16. description: 'Prevent missing displayName in a React component definition',
  17. category: 'Best Practices',
  18. recommended: true,
  19. url: docsUrl('display-name')
  20. },
  21. messages: {
  22. noDisplayName: 'Component definition is missing display name'
  23. },
  24. schema: [{
  25. type: 'object',
  26. properties: {
  27. ignoreTranspilerName: {
  28. type: 'boolean'
  29. }
  30. },
  31. additionalProperties: false
  32. }]
  33. },
  34. create: Components.detect((context, components, utils) => {
  35. const config = context.options[0] || {};
  36. const ignoreTranspilerName = config.ignoreTranspilerName || false;
  37. /**
  38. * Mark a prop type as declared
  39. * @param {ASTNode} node The AST node being checked.
  40. */
  41. function markDisplayNameAsDeclared(node) {
  42. components.set(node, {
  43. hasDisplayName: true
  44. });
  45. }
  46. /**
  47. * Reports missing display name for a given component
  48. * @param {Object} component The component to process
  49. */
  50. function reportMissingDisplayName(component) {
  51. context.report({
  52. node: component.node,
  53. messageId: 'noDisplayName'
  54. });
  55. }
  56. /**
  57. * Checks if the component have a name set by the transpiler
  58. * @param {ASTNode} node The AST node being checked.
  59. * @returns {Boolean} True if component has a name, false if not.
  60. */
  61. function hasTranspilerName(node) {
  62. const namedObjectAssignment = (
  63. node.type === 'ObjectExpression'
  64. && node.parent
  65. && node.parent.parent
  66. && node.parent.parent.type === 'AssignmentExpression'
  67. && (
  68. !node.parent.parent.left.object
  69. || node.parent.parent.left.object.name !== 'module'
  70. || node.parent.parent.left.property.name !== 'exports'
  71. )
  72. );
  73. const namedObjectDeclaration = (
  74. node.type === 'ObjectExpression'
  75. && node.parent
  76. && node.parent.parent
  77. && node.parent.parent.type === 'VariableDeclarator'
  78. );
  79. const namedClass = (
  80. (node.type === 'ClassDeclaration' || node.type === 'ClassExpression')
  81. && node.id
  82. && !!node.id.name
  83. );
  84. const namedFunctionDeclaration = (
  85. (node.type === 'FunctionDeclaration' || node.type === 'FunctionExpression')
  86. && node.id
  87. && !!node.id.name
  88. );
  89. const namedFunctionExpression = (
  90. astUtil.isFunctionLikeExpression(node)
  91. && node.parent
  92. && (node.parent.type === 'VariableDeclarator' || node.parent.method === true)
  93. && (!node.parent.parent || !utils.isES5Component(node.parent.parent))
  94. );
  95. if (
  96. namedObjectAssignment || namedObjectDeclaration
  97. || namedClass
  98. || namedFunctionDeclaration || namedFunctionExpression
  99. ) {
  100. return true;
  101. }
  102. return false;
  103. }
  104. // --------------------------------------------------------------------------
  105. // Public
  106. // --------------------------------------------------------------------------
  107. return {
  108. ClassProperty(node) {
  109. if (!propsUtil.isDisplayNameDeclaration(node)) {
  110. return;
  111. }
  112. markDisplayNameAsDeclared(node);
  113. },
  114. MemberExpression(node) {
  115. if (!propsUtil.isDisplayNameDeclaration(node.property)) {
  116. return;
  117. }
  118. const component = utils.getRelatedComponent(node);
  119. if (!component) {
  120. return;
  121. }
  122. markDisplayNameAsDeclared(component.node);
  123. },
  124. FunctionExpression(node) {
  125. if (ignoreTranspilerName || !hasTranspilerName(node)) {
  126. return;
  127. }
  128. if (components.get(node)) {
  129. markDisplayNameAsDeclared(node);
  130. }
  131. },
  132. FunctionDeclaration(node) {
  133. if (ignoreTranspilerName || !hasTranspilerName(node)) {
  134. return;
  135. }
  136. if (components.get(node)) {
  137. markDisplayNameAsDeclared(node);
  138. }
  139. },
  140. ArrowFunctionExpression(node) {
  141. if (ignoreTranspilerName || !hasTranspilerName(node)) {
  142. return;
  143. }
  144. if (components.get(node)) {
  145. markDisplayNameAsDeclared(node);
  146. }
  147. },
  148. MethodDefinition(node) {
  149. if (!propsUtil.isDisplayNameDeclaration(node.key)) {
  150. return;
  151. }
  152. markDisplayNameAsDeclared(node);
  153. },
  154. ClassExpression(node) {
  155. if (ignoreTranspilerName || !hasTranspilerName(node)) {
  156. return;
  157. }
  158. markDisplayNameAsDeclared(node);
  159. },
  160. ClassDeclaration(node) {
  161. if (ignoreTranspilerName || !hasTranspilerName(node)) {
  162. return;
  163. }
  164. markDisplayNameAsDeclared(node);
  165. },
  166. ObjectExpression(node) {
  167. if (ignoreTranspilerName || !hasTranspilerName(node)) {
  168. // Search for the displayName declaration
  169. node.properties.forEach((property) => {
  170. if (!property.key || !propsUtil.isDisplayNameDeclaration(property.key)) {
  171. return;
  172. }
  173. markDisplayNameAsDeclared(node);
  174. });
  175. return;
  176. }
  177. markDisplayNameAsDeclared(node);
  178. },
  179. CallExpression(node) {
  180. if (!utils.isPragmaComponentWrapper(node)) {
  181. return;
  182. }
  183. if (node.arguments.length > 0 && astUtil.isFunctionLikeExpression(node.arguments[0])) {
  184. // Skip over React.forwardRef declarations that are embeded within
  185. // a React.memo i.e. React.memo(React.forwardRef(/* ... */))
  186. // This means that we raise a single error for the call to React.memo
  187. // instead of one for React.memo and one for React.forwardRef
  188. const isWrappedInAnotherPragma = utils.getPragmaComponentWrapper(node);
  189. if (
  190. !isWrappedInAnotherPragma
  191. && (ignoreTranspilerName || !hasTranspilerName(node.arguments[0]))
  192. ) {
  193. return;
  194. }
  195. if (components.get(node)) {
  196. markDisplayNameAsDeclared(node);
  197. }
  198. }
  199. },
  200. 'Program:exit'() {
  201. const list = components.list();
  202. // Report missing display name for all components
  203. Object.keys(list).filter((component) => !list[component].hasDisplayName).forEach((component) => {
  204. reportMissingDisplayName(list[component]);
  205. });
  206. }
  207. };
  208. })
  209. };