From 4689be2d11557f0c9001025f460351a34a204a23 Mon Sep 17 00:00:00 2001 From: Johnny Zabala Date: Mon, 6 Jul 2020 18:52:06 -0400 Subject: [PATCH] [Fix]: Add check for first letter capitalization in functional component detection --- lib/util/Components.js | 24 ++++++++++++++++++++---- 1 file changed, 20 insertions(+), 4 deletions(-) diff --git a/lib/util/Components.js b/lib/util/Components.js index 3ded3e39f4..b5abdd36f2 100644 --- a/lib/util/Components.js +++ b/lib/util/Components.js @@ -70,6 +70,14 @@ function isReturnsLogicalJSX(node, property, strict) { : (returnsLogicalJSXLeft || returnsLogicalJSXRight); } +function isFirstLetterCapitalized(word) { + if (!word) { + return false; + } + const firstLetter = word.charAt(0); + return firstLetter.toUpperCase() === firstLetter; +} + const Lists = new WeakMap(); /** @@ -624,13 +632,21 @@ function componentRule(rule, context) { * @returns {ASTNode | undefined} */ getStatelessComponent(node) { - if (node.type === 'FunctionDeclaration') { - if (utils.isReturningJSXOrNull(node)) { - return node; - } + if ( + node.type === 'FunctionDeclaration' + && isFirstLetterCapitalized(node.id.name) + && utils.isReturningJSXOrNull(node) + ) { + return node; } if (node.type === 'FunctionExpression' || node.type === 'ArrowFunctionExpression') { + if (node.parent.type === 'VariableDeclarator' && utils.isReturningJSXOrNull(node)) { + if (isFirstLetterCapitalized(node.parent.id.name)) { + return node; + } + return undefined; + } if (utils.isInAllowedPositionForComponent(node) && utils.isReturningJSXOrNull(node)) { return node; }