Skip to content

Commit d5adb6d

Browse files
committed
Move react version check for context displayname
1 parent 1f557cb commit d5adb6d

File tree

1 file changed

+17
-19
lines changed

1 file changed

+17
-19
lines changed

lib/rules/display-name.js

Lines changed: 17 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -52,7 +52,7 @@ module.exports = {
5252
create: Components.detect((context, components, utils) => {
5353
const config = context.options[0] || {};
5454
const ignoreTranspilerName = config.ignoreTranspilerName || false;
55-
const checkContextObjects = config.checkContextObjects || false;
55+
const checkContextObjects = (config.checkContextObjects || false) && testReactVersion(context, '>= 16.3.0');
5656

5757
const contextObjects = new Map();
5858

@@ -78,28 +78,28 @@ module.exports = {
7878
}
7979

8080
/**
81-
* Checks if the node is a createContext call
81+
* Checks if the node is a React.createContext call
8282
* @param {ASTNode} node - The AST node being checked.
83-
* @returns {Boolean} - True if node is a createContext call object literal, False if not.
83+
* @returns {Boolean} - True if node is a React.createContext call object literal, false if not.
8484
*/
8585
function isCreateContext(node) {
8686
if (!node.init) {
8787
return false;
8888
}
8989

9090
if (
91-
node.init.type === 'CallExpression' &&
92-
node.init.callee &&
93-
node.init.callee.name === 'createContext'
91+
node.init.type === 'CallExpression'
92+
&& node.init.callee
93+
&& node.init.callee.name === 'createContext'
9494
) {
9595
return true;
9696
}
9797

9898
if (
99-
node.init.callee &&
100-
node.init.callee.type === 'MemberExpression' &&
101-
node.init.callee.property &&
102-
node.init.callee.property.name === 'createContext'
99+
node.init.callee
100+
&& node.init.callee.type === 'MemberExpression'
101+
&& node.init.callee.property
102+
&& node.init.callee.property.name === 'createContext'
103103
) {
104104
return true;
105105
}
@@ -129,11 +129,9 @@ module.exports = {
129129
* @param {Object} contextObj The context object to process
130130
*/
131131
function reportMissingContextDisplayName(contextObj) {
132-
if (testReactVersion(context, '>= 16.3.0')) {
133-
report(context, messages.noContextDisplayName, 'noContextDisplayName', {
134-
node: contextObj.node,
135-
});
136-
}
132+
report(context, messages.noContextDisplayName, 'noContextDisplayName', {
133+
node: contextObj.node,
134+
});
137135
}
138136

139137
/**
@@ -210,10 +208,10 @@ module.exports = {
210208
return;
211209
}
212210
if (
213-
checkContextObjects &&
214-
node.object &&
215-
node.object.name &&
216-
contextObjects.has(node.object.name)
211+
checkContextObjects
212+
&& node.object
213+
&& node.object.name
214+
&& contextObjects.has(node.object.name)
217215
) {
218216
contextObjects.get(node.object.name).hasDisplayName = true;
219217
}

0 commit comments

Comments
 (0)