From 973b027df64511b0123389ac9e87c350916d76b7 Mon Sep 17 00:00:00 2001 From: Chiawen Chen Date: Sun, 8 Sep 2019 01:25:54 +0800 Subject: [PATCH] Move a function to jsxUtils --- lib/rules/jsx-no-useless-fragment.js | 7 +------ lib/util/jsx.js | 14 +++++++++++++- 2 files changed, 14 insertions(+), 7 deletions(-) diff --git a/lib/rules/jsx-no-useless-fragment.js b/lib/rules/jsx-no-useless-fragment.js index fb05be1c4c..22ae071e29 100644 --- a/lib/rules/jsx-no-useless-fragment.js +++ b/lib/rules/jsx-no-useless-fragment.js @@ -60,12 +60,7 @@ function trimLikeReact(text) { function isKeyedElement(node) { return node.type === 'JSXElement' && node.openingElement.attributes && - node.openingElement.attributes.some(attribute => ( - attribute.type === 'JSXAttribute' && - attribute.name && - attribute.name.type === 'JSXIdentifier' && - attribute.name.name === 'key' - )); + node.openingElement.attributes.some(jsxUtil.isJSXAttributeKey); } module.exports = { diff --git a/lib/util/jsx.js b/lib/util/jsx.js index d5f69dba69..d694becc61 100644 --- a/lib/util/jsx.js +++ b/lib/util/jsx.js @@ -64,8 +64,20 @@ function isJSX(node) { return node && ['JSXElement', 'JSXFragment'].indexOf(node.type) >= 0; } +/** + * Check if node is like `key={...}` as in `` + * @param {ASTNode} node + */ +function isJSXAttributeKey(node) { + return node.type === 'JSXAttribute' && + node.name && + node.name.type === 'JSXIdentifier' && + node.name.name === 'key'; +} + module.exports = { isDOMComponent, isFragment, - isJSX + isJSX, + isJSXAttributeKey };