@@ -31,36 +31,32 @@ const messages = {
3131module . exports = {
3232 meta : {
3333 docs : {
34- description :
35- 'Disallow missing displayName in a React component definition' ,
34+ description : 'Disallow missing displayName in a React component definition' ,
3635 category : 'Best Practices' ,
3736 recommended : true ,
3837 url : docsUrl ( 'display-name' ) ,
3938 } ,
4039
4140 messages,
4241
43- schema : [
44- {
45- type : 'object' ,
46- properties : {
47- ignoreTranspilerName : {
48- type : 'boolean' ,
49- } ,
50- checkContextObjects : {
51- type : 'boolean' ,
52- } ,
42+ schema : [ {
43+ type : 'object' ,
44+ properties : {
45+ ignoreTranspilerName : {
46+ type : 'boolean' ,
47+ } ,
48+ checkContextObjects : {
49+ type : 'boolean' ,
5350 } ,
54- additionalProperties : false ,
5551 } ,
56- ] ,
52+ additionalProperties : false ,
53+ } ] ,
5754 } ,
5855
5956 create : Components . detect ( ( context , components , utils ) => {
6057 const config = context . options [ 0 ] || { } ;
6158 const ignoreTranspilerName = config . ignoreTranspilerName || false ;
62- const checkContextObjects = ( config . checkContextObjects || false )
63- && testReactVersion ( context , '>= 16.3.0' ) ;
59+ const checkContextObjects = ( config . checkContextObjects || false ) && testReactVersion ( context , '>= 16.3.0' ) ;
6460
6561 const contextObjects = new Map ( ) ;
6662
@@ -80,12 +76,10 @@ module.exports = {
8076 * @returns {boolean } True if React.forwardRef is nested inside React.memo, false if not.
8177 */
8278 function isNestedMemo ( node ) {
83- return (
84- astUtil . isCallExpression ( node )
79+ return astUtil . isCallExpression ( node )
8580 && node . arguments
8681 && astUtil . isCallExpression ( node . arguments [ 0 ] )
87- && utils . isPragmaComponentWrapper ( node )
88- ) ;
82+ && utils . isPragmaComponentWrapper ( node ) ;
8983 }
9084
9185 /**
@@ -121,40 +115,46 @@ module.exports = {
121115 * @returns {boolean } True if component has a name, false if not.
122116 */
123117 function hasTranspilerName ( node ) {
124- const namedObjectAssignment = node . type === 'ObjectExpression'
118+ const namedObjectAssignment = (
119+ node . type === 'ObjectExpression'
125120 && node . parent
126121 && node . parent . parent
127122 && node . parent . parent . type === 'AssignmentExpression'
128- && ( ! node . parent . parent . left . object
123+ && (
124+ ! node . parent . parent . left . object
129125 || node . parent . parent . left . object . name !== 'module'
130- || node . parent . parent . left . property . name !== 'exports' ) ;
131- const namedObjectDeclaration = node . type === 'ObjectExpression'
126+ || node . parent . parent . left . property . name !== 'exports'
127+ )
128+ ) ;
129+ const namedObjectDeclaration = (
130+ node . type === 'ObjectExpression'
132131 && node . parent
133132 && node . parent . parent
134- && node . parent . parent . type === 'VariableDeclarator' ;
135- const namedClass = ( node . type === 'ClassDeclaration' || node . type === 'ClassExpression' )
133+ && node . parent . parent . type === 'VariableDeclarator'
134+ ) ;
135+ const namedClass = (
136+ ( node . type === 'ClassDeclaration' || node . type === 'ClassExpression' )
136137 && node . id
137- && ! ! node . id . name ;
138+ && ! ! node . id . name
139+ ) ;
138140
139- const namedFunctionDeclaration = ( node . type === 'FunctionDeclaration'
140- || node . type === 'FunctionExpression' )
141+ const namedFunctionDeclaration = (
142+ ( node . type === 'FunctionDeclaration' || node . type === 'FunctionExpression' )
141143 && node . id
142- && ! ! node . id . name ;
144+ && ! ! node . id . name
145+ ) ;
143146
144- const namedFunctionExpression = astUtil . isFunctionLikeExpression ( node )
147+ const namedFunctionExpression = (
148+ astUtil . isFunctionLikeExpression ( node )
145149 && node . parent
146- && ( node . parent . type === 'VariableDeclarator'
147- || node . parent . type === 'Property'
148- || node . parent . method === true )
149- && ( ! node . parent . parent
150- || ! componentUtil . isES5Component ( node . parent . parent , context ) ) ;
150+ && ( node . parent . type === 'VariableDeclarator' || node . parent . type === 'Property' || node . parent . method === true )
151+ && ( ! node . parent . parent || ! componentUtil . isES5Component ( node . parent . parent , context ) )
152+ ) ;
151153
152154 if (
153- namedObjectAssignment
154- || namedObjectDeclaration
155+ namedObjectAssignment || namedObjectDeclaration
155156 || namedClass
156- || namedFunctionDeclaration
157- || namedFunctionExpression
157+ || namedFunctionDeclaration || namedFunctionExpression
158158 ) {
159159 return true ;
160160 }
@@ -199,31 +199,29 @@ module.exports = {
199199 }
200200
201201 function isIdentifierShadowed ( node , identifierName ) {
202- while ( node && node . parent ) {
203- node = node . parent ;
202+ let currentNode = node ;
203+ while ( currentNode && currentNode . parent ) {
204+ currentNode = currentNode . parent ;
204205 if (
205- node . type === 'FunctionDeclaration'
206- || node . type === 'FunctionExpression'
207- || node . type === 'ArrowFunctionExpression'
206+ currentNode . type === 'FunctionDeclaration'
207+ || currentNode . type === 'FunctionExpression'
208+ || currentNode . type === 'ArrowFunctionExpression'
208209 ) {
209210 break ;
210211 }
211212 }
212213
213- if ( ! node || ! node . body ) {
214+ if ( ! currentNode || ! currentNode . body ) {
214215 return false ;
215216 }
216217
217- return hasVariableDeclaration ( node . body , identifierName ) ;
218+ return hasVariableDeclaration ( currentNode . body , identifierName ) ;
218219 }
219-
220220 /**
221- *
222- * Check is current component shadowed
223- * @param {ASTNode } node The AST node being checked.
224- * @returns {boolean } True if component has a name, false if not.
221+ * Checks whether the component wrapper (e.g. React.memo or forwardRef) is shadowed in the current scope.
222+ * @param {ASTNode } node - The CallExpression AST node representing a potential component wrapper.
223+ * @returns {boolean } True if the wrapper identifier (e.g. 'React', 'memo', 'forwardRef') is shadowed, false otherwise.
225224 */
226-
227225 function isShadowedComponent ( node ) {
228226 if ( ! node || node . type !== 'CallExpression' ) {
229227 return false ;
@@ -253,10 +251,7 @@ module.exports = {
253251 return {
254252 ExpressionStatement ( node ) {
255253 if ( checkContextObjects && isCreateContext ( node ) ) {
256- contextObjects . set ( node . expression . left . name , {
257- node,
258- hasDisplayName : false ,
259- } ) ;
254+ contextObjects . set ( node . expression . left . name , { node, hasDisplayName : false } ) ;
260255 }
261256 } ,
262257 VariableDeclarator ( node ) {
@@ -320,10 +315,7 @@ module.exports = {
320315 if ( ignoreTranspilerName || ! hasTranspilerName ( node ) ) {
321316 // Search for the displayName declaration
322317 node . properties . forEach ( ( property ) => {
323- if (
324- ! property . key
325- || ! propsUtil . isDisplayNameDeclaration ( property . key )
326- ) {
318+ if ( ! property . key || ! propsUtil . isDisplayNameDeclaration ( property . key ) ) {
327319 return ;
328320 }
329321 markDisplayNameAsDeclared ( node ) ;
@@ -338,10 +330,7 @@ module.exports = {
338330 return ;
339331 }
340332
341- if (
342- node . arguments . length > 0
343- && astUtil . isFunctionLikeExpression ( node . arguments [ 0 ] )
344- ) {
333+ if ( node . arguments . length > 0 && astUtil . isFunctionLikeExpression ( node . arguments [ 0 ] ) ) {
345334 // Skip over React.forwardRef declarations that are embedded within
346335 // a React.memo i.e. React.memo(React.forwardRef(/* ... */))
347336 // This means that we raise a single error for the call to React.memo
0 commit comments