display-name.js 7.8 KB

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