Skip to content

Commit 6e05536

Browse files
committed
style: fix lint,prettier errors
1 parent 30f0022 commit 6e05536

File tree

2 files changed

+68
-79
lines changed

2 files changed

+68
-79
lines changed

lib/rules/display-name.js

Lines changed: 54 additions & 65 deletions
Original file line numberDiff line numberDiff line change
@@ -31,36 +31,32 @@ const messages = {
3131
module.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

tests/lib/rules/display-name.js

Lines changed: 14 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -970,8 +970,7 @@ ruleTester.run('display-name', rule, {
970970
errors: [
971971
{
972972
messageId: 'noDisplayName',
973-
},
974-
],
973+
}],
975974
},
976975
{
977976
code: `
@@ -1178,9 +1177,9 @@ ruleTester.run('display-name', rule, {
11781177
errors: [{ messageId: 'noDisplayName' }],
11791178
},
11801179
{
1181-
// Only trigger an error for the outer React.memo,
1182-
// if the React version is not in the following range:
1183-
// ^0.14.10 || ^15.7.0 || >= 16.12.0
1180+
// Only trigger an error for the outer React.memo,
1181+
// if the React version is not in the following range:
1182+
// ^0.14.10 || ^15.7.0 || >= 16.12.0
11841183
code: `
11851184
import React from 'react'
11861185
@@ -1193,18 +1192,17 @@ ruleTester.run('display-name', rule, {
11931192
errors: [
11941193
{
11951194
messageId: 'noDisplayName',
1196-
},
1197-
],
1195+
}],
11981196
settings: {
11991197
react: {
12001198
version: '15.6.0',
12011199
},
12021200
},
12031201
},
12041202
{
1205-
// Only trigger an error for the outer React.memo,
1206-
// if the React version is not in the following range:
1207-
// ^0.14.10 || ^15.7.0 || >= ^16.12.0
1203+
// Only trigger an error for the outer React.memo,
1204+
// if the React version is not in the following range:
1205+
// ^0.14.10 || ^15.7.0 || >= ^16.12.0
12081206
code: `
12091207
import React from 'react'
12101208
@@ -1222,9 +1220,9 @@ ruleTester.run('display-name', rule, {
12221220
},
12231221
},
12241222
{
1225-
// React does not handle the result of forwardRef being passed into memo
1226-
// ComponentWithMemoAndForwardRef gets shown as Memo(Anonymous)
1227-
// See https://github.com/facebook/react/issues/16722
1223+
// React does not handle the result of forwardRef being passed into memo
1224+
// ComponentWithMemoAndForwardRef gets shown as Memo(Anonymous)
1225+
// See https://github.com/facebook/react/issues/16722
12281226
code: `
12291227
import React from 'react'
12301228
@@ -1351,7 +1349,9 @@ ruleTester.run('display-name', rule, {
13511349
<div>{a} {listItem}</div>
13521350
);
13531351
`,
1354-
errors: [{ message: 'Component definition is missing display name' }],
1352+
errors: [
1353+
{ message: 'Component definition is missing display name' },
1354+
],
13551355
},
13561356
{
13571357
code: `

0 commit comments

Comments
 (0)