Skip to content

Commit

Permalink
Move isCreateContext to seperate file
Browse files Browse the repository at this point in the history
  • Loading branch information
JulesBlm committed Feb 16, 2023
1 parent 6373941 commit 725d4c7
Show file tree
Hide file tree
Showing 2 changed files with 54 additions and 52 deletions.
53 changes: 1 addition & 52 deletions lib/rules/display-name.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
const values = require('object.values');

const Components = require('../util/Components');
const isCreateContext = require('../util/isCreateContext');
const astUtil = require('../util/ast');
const componentUtil = require('../util/componentUtil');
const docsUrl = require('../util/docsUrl');
Expand Down Expand Up @@ -77,58 +78,6 @@ module.exports = {
return node.type === 'CallExpression' && argumentIsCallExpression && utils.isPragmaComponentWrapper(node);
}

/**
* Checks if the node is a React.createContext call
* @param {ASTNode} node - The AST node being checked.
* @returns {Boolean} - True if node is a React.createContext call, false if not.
*/
function isCreateContext(node) {
if (
node.init
&& node.init.type === 'CallExpression'
&& node.init.callee
&& node.init.callee.name === 'createContext'
) {
return true;
}

if (
node.init
&& node.init.callee
&& node.init.callee.type === 'MemberExpression'
&& node.init.callee.property
&& node.init.callee.property.name === 'createContext'
) {
return true;
}

if (
node.expression
&& node.expression.type === 'AssignmentExpression'
&& node.expression.operator === '='
&& node.expression.right.type === 'CallExpression'
&& node.expression.right.callee
&& node.expression.right.callee.name === 'createContext'
) {
return true;
}

if (
node.expression
&& node.expression.type === 'AssignmentExpression'
&& node.expression.operator === '='
&& node.expression.right.type === 'CallExpression'
&& node.expression.right.callee
&& node.expression.right.callee.type === 'MemberExpression'
&& node.expression.right.callee.property
&& node.expression.right.callee.property.name === 'createContext'
) {
return true;
}

return false;
}

/**
* Reports missing display name for a given component
* @param {Object} component The component to process
Expand Down
53 changes: 53 additions & 0 deletions lib/util/isCreateContext.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
'use strict';

/**
* Checks if the node is a React.createContext call
* @param {ASTNode} node - The AST node being checked.
* @returns {Boolean} - True if node is a React.createContext call, false if not.
*/
module.exports = function isCreateContext(node) {
if (
node.init
&& node.init.type === 'CallExpression'
&& node.init.callee
&& node.init.callee.name === 'createContext'
) {
return true;
}

if (
node.init
&& node.init.callee
&& node.init.callee.type === 'MemberExpression'
&& node.init.callee.property
&& node.init.callee.property.name === 'createContext'
) {
return true;
}

if (
node.expression
&& node.expression.type === 'AssignmentExpression'
&& node.expression.operator === '='
&& node.expression.right.type === 'CallExpression'
&& node.expression.right.callee
&& node.expression.right.callee.name === 'createContext'
) {
return true;
}

if (
node.expression
&& node.expression.type === 'AssignmentExpression'
&& node.expression.operator === '='
&& node.expression.right.type === 'CallExpression'
&& node.expression.right.callee
&& node.expression.right.callee.type === 'MemberExpression'
&& node.expression.right.callee.property
&& node.expression.right.callee.property.name === 'createContext'
) {
return true;
}

return false;
};

0 comments on commit 725d4c7

Please sign in to comment.