From 1185b3799798d5b2b31ef39abe55231ce7e0e875 Mon Sep 17 00:00:00 2001 From: Chiawen Chen Date: Thu, 29 Apr 2021 01:16:00 +0800 Subject: [PATCH] [Refactor] `void-dom-elements-no-children`: improve performance --- CHANGELOG.md | 2 ++ lib/util/Components.js | 20 ++++++++++++-------- 2 files changed, 14 insertions(+), 8 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index fc9aa80b18..96a82a18b8 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -14,7 +14,9 @@ This change log adheres to standards from [Keep a CHANGELOG](http://keepachangel ### Changed * [Docs] [`jsx-newline`]: Fix minor spelling error on rule name ([#2974][] @DennisSkoko) +* [Refactor] [`void-dom-elements-no-children`]: improve performance +[#2977]: https://github.com/yannickcr/eslint-plugin-react/pull/2977 [#2975]: https://github.com/yannickcr/eslint-plugin-react/pull/2975 [#2974]: https://github.com/yannickcr/eslint-plugin-react/pull/2974 [#2972]: https://github.com/yannickcr/eslint-plugin-react/pull/2972 diff --git a/lib/util/Components.js b/lib/util/Components.js index 4cbd62e750..27dd09c0a6 100644 --- a/lib/util/Components.js +++ b/lib/util/Components.js @@ -393,25 +393,29 @@ function componentRule(rule, context) { * @returns {Boolean} True if createElement called from pragma */ isCreateElement(node) { - const calledOnPragma = ( + // match `React.createElement()` + if ( node && node.callee && node.callee.object && node.callee.object.name === pragma && node.callee.property && node.callee.property.name === 'createElement' - ); + ) { + return true; + } - const calledDirectly = ( + // match `createElement()` + if ( node && node.callee && node.callee.name === 'createElement' - ); - - if (this.isDestructuredFromPragmaImport('createElement')) { - return calledDirectly || calledOnPragma; + && this.isDestructuredFromPragmaImport('createElement') + ) { + return true; } - return calledOnPragma; + + return false; }, /**