|
| 1 | +/** |
| 2 | + * @fileoverview Disallow useless fragments |
| 3 | + */ |
| 4 | + |
| 5 | +'use strict'; |
| 6 | + |
| 7 | +const pragmaUtil = require('../util/pragma'); |
| 8 | +const jsxUtil = require('../util/jsx'); |
| 9 | +const docsUrl = require('../util/docsUrl'); |
| 10 | + |
| 11 | +function isJSXText(node) { |
| 12 | + return !!node && (node.type === 'JSXText' || node.type === 'Literal'); |
| 13 | +} |
| 14 | + |
| 15 | +/** |
| 16 | + * @param {string} text |
| 17 | + * @returns {boolean} |
| 18 | + */ |
| 19 | +function isOnlyWhitespace(text) { |
| 20 | + return text.trim().length === 0; |
| 21 | +} |
| 22 | + |
| 23 | +/** |
| 24 | + * @param {ASTNode} node |
| 25 | + * @returns {boolean} |
| 26 | + */ |
| 27 | +function isNonspaceJSXTextOrJSXCurly(node) { |
| 28 | + return (isJSXText(node) && !isOnlyWhitespace(node.raw)) || node.type === 'JSXExpressionContainer'; |
| 29 | +} |
| 30 | + |
| 31 | +/** |
| 32 | + * Somehow fragment like this is useful: <Foo content={<>ee eeee eeee ...</>} /> |
| 33 | + * @param {ASTNode} node |
| 34 | + * @returns {boolean} |
| 35 | + */ |
| 36 | +function isFragmentWithOnlyTextAndIsNotChild(node) { |
| 37 | + return node.children.length === 1 && |
| 38 | + isJSXText(node.children[0]) && |
| 39 | + !(node.parent.type === 'JSXElement' || node.parent.type === 'JSXFragment'); |
| 40 | +} |
| 41 | + |
| 42 | +/** |
| 43 | + * @param {string} text |
| 44 | + * @returns {string} |
| 45 | + */ |
| 46 | +function trimLikeReact(text) { |
| 47 | + const leadingSpaces = /^\s*/.exec(text)[0]; |
| 48 | + const trailingSpaces = /\s*$/.exec(text)[0]; |
| 49 | + |
| 50 | + const start = leadingSpaces.includes('\n') ? leadingSpaces.length : 0; |
| 51 | + const end = trailingSpaces.includes('\n') ? text.length - trailingSpaces.length : text.length; |
| 52 | + |
| 53 | + return text.slice(start, end); |
| 54 | +} |
| 55 | + |
| 56 | +/** |
| 57 | + * Test if node is like `<Fragment key={_}>_</Fragment>` |
| 58 | + * @param {JSXElement} node |
| 59 | + * @returns {boolean} |
| 60 | + */ |
| 61 | +function isKeyedElement(node) { |
| 62 | + return node.type === 'JSXElement' && |
| 63 | + node.openingElement.attributes && |
| 64 | + node.openingElement.attributes.some(jsxUtil.isJSXAttributeKey); |
| 65 | +} |
| 66 | + |
| 67 | +module.exports = { |
| 68 | + meta: { |
| 69 | + type: 'suggestion', |
| 70 | + fixable: 'code', |
| 71 | + docs: { |
| 72 | + description: 'Disallow unnecessary fragments', |
| 73 | + category: 'Possible Errors', |
| 74 | + recommended: false, |
| 75 | + url: docsUrl('jsx-no-useless-fragment') |
| 76 | + }, |
| 77 | + messages: { |
| 78 | + NeedsMoreChidren: 'Fragments should contain more than one child - otherwise, there‘s no need for a Fragment at all.', |
| 79 | + ChildOfHtmlElement: 'Passing a fragment to an HTML element is useless.' |
| 80 | + } |
| 81 | + }, |
| 82 | + |
| 83 | + create(context) { |
| 84 | + const reactPragma = pragmaUtil.getFromContext(context); |
| 85 | + const fragmentPragma = pragmaUtil.getFragmentFromContext(context); |
| 86 | + |
| 87 | + /** |
| 88 | + * Test whether a node is an padding spaces trimmed by react runtime. |
| 89 | + * @param {ASTNode} node |
| 90 | + * @returns {boolean} |
| 91 | + */ |
| 92 | + function isPaddingSpaces(node) { |
| 93 | + return isJSXText(node) && |
| 94 | + isOnlyWhitespace(node.raw) && |
| 95 | + node.raw.includes('\n'); |
| 96 | + } |
| 97 | + |
| 98 | + /** |
| 99 | + * Test whether a JSXElement has less than two children, excluding paddings spaces. |
| 100 | + * @param {JSXElement|JSXFragment} node |
| 101 | + * @returns {boolean} |
| 102 | + */ |
| 103 | + function hasLessThanTwoChildren(node) { |
| 104 | + if (!node || !node.children || node.children.length < 2) { |
| 105 | + return true; |
| 106 | + } |
| 107 | + |
| 108 | + return ( |
| 109 | + node.children.length - |
| 110 | + (+isPaddingSpaces(node.children[0])) - |
| 111 | + (+isPaddingSpaces(node.children[node.children.length - 1])) |
| 112 | + ) < 2; |
| 113 | + } |
| 114 | + |
| 115 | + /** |
| 116 | + * @param {JSXElement|JSXFragment} node |
| 117 | + * @returns {boolean} |
| 118 | + */ |
| 119 | + function isChildOfHtmlElement(node) { |
| 120 | + return node.parent.type === 'JSXElement' && |
| 121 | + node.parent.openingElement.name.type === 'JSXIdentifier' && |
| 122 | + /^[a-z]+$/.test(node.parent.openingElement.name.name); |
| 123 | + } |
| 124 | + |
| 125 | + /** |
| 126 | + * @param {JSXElement|JSXFragment} node |
| 127 | + * @return {boolean} |
| 128 | + */ |
| 129 | + function isChildOfComponentElement(node) { |
| 130 | + return node.parent.type === 'JSXElement' && |
| 131 | + !isChildOfHtmlElement(node) && |
| 132 | + !jsxUtil.isFragment(node.parent, reactPragma, fragmentPragma); |
| 133 | + } |
| 134 | + |
| 135 | + /** |
| 136 | + * @param {ASTNode} node |
| 137 | + * @returns {boolean} |
| 138 | + */ |
| 139 | + function canFix(node) { |
| 140 | + // Not safe to fix fragments without a jsx parent. |
| 141 | + if (!(node.parent.type === 'JSXElement' || node.parent.type === 'JSXFragment')) { |
| 142 | + // const a = <></> |
| 143 | + if (node.children.length === 0) { |
| 144 | + return false; |
| 145 | + } |
| 146 | + |
| 147 | + // const a = <>cat {meow}</> |
| 148 | + if (node.children.some(isNonspaceJSXTextOrJSXCurly)) { |
| 149 | + return false; |
| 150 | + } |
| 151 | + } |
| 152 | + |
| 153 | + // Not safe to fix `<Eeee><>foo</></Eeee>` because `Eeee` might require its children be a ReactElement. |
| 154 | + if (isChildOfComponentElement(node)) { |
| 155 | + return false; |
| 156 | + } |
| 157 | + |
| 158 | + return true; |
| 159 | + } |
| 160 | + |
| 161 | + /** |
| 162 | + * @param {ASTNode} node |
| 163 | + * @returns {Function | undefined} |
| 164 | + */ |
| 165 | + function getFix(node) { |
| 166 | + if (!canFix(node)) { |
| 167 | + return undefined; |
| 168 | + } |
| 169 | + |
| 170 | + return function fix(fixer) { |
| 171 | + const opener = node.type === 'JSXFragment' ? node.openingFragment : node.openingElement; |
| 172 | + const closer = node.type === 'JSXFragment' ? node.closingFragment : node.closingElement; |
| 173 | + const childrenText = context.getSourceCode().getText().slice(opener.range[1], closer.range[0]); |
| 174 | + |
| 175 | + return fixer.replaceText(node, trimLikeReact(childrenText)); |
| 176 | + }; |
| 177 | + } |
| 178 | + |
| 179 | + function checkNode(node) { |
| 180 | + if (isKeyedElement(node)) { |
| 181 | + return; |
| 182 | + } |
| 183 | + |
| 184 | + if (hasLessThanTwoChildren(node) && !isFragmentWithOnlyTextAndIsNotChild(node)) { |
| 185 | + context.report({ |
| 186 | + node, |
| 187 | + messageId: 'NeedsMoreChidren', |
| 188 | + fix: getFix(node) |
| 189 | + }); |
| 190 | + } |
| 191 | + |
| 192 | + if (isChildOfHtmlElement(node)) { |
| 193 | + context.report({ |
| 194 | + node, |
| 195 | + messageId: 'ChildOfHtmlElement', |
| 196 | + fix: getFix(node) |
| 197 | + }); |
| 198 | + } |
| 199 | + } |
| 200 | + |
| 201 | + return { |
| 202 | + JSXElement(node) { |
| 203 | + if (jsxUtil.isFragment(node, reactPragma, fragmentPragma)) { |
| 204 | + checkNode(node); |
| 205 | + } |
| 206 | + }, |
| 207 | + JSXFragment: checkNode |
| 208 | + }; |
| 209 | + } |
| 210 | +}; |
0 commit comments