diff --git a/lib/rules/display-name.js b/lib/rules/display-name.js index 3028c61c71..3d69a13c87 100644 --- a/lib/rules/display-name.js +++ b/lib/rules/display-name.js @@ -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'); @@ -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 diff --git a/lib/util/isCreateContext.js b/lib/util/isCreateContext.js new file mode 100644 index 0000000000..d5fdfe4551 --- /dev/null +++ b/lib/util/isCreateContext.js @@ -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; +};