Skip to content

Commit 17cc2c7

Browse files
committed
Refactor: move codes
1 parent 4bdeb5c commit 17cc2c7

File tree

1 file changed

+49
-46
lines changed

1 file changed

+49
-46
lines changed

lib/rules/jsx-no-useless-fragment.js

Lines changed: 49 additions & 46 deletions
Original file line numberDiff line numberDiff line change
@@ -6,51 +6,6 @@
66
const pragmaUtil = require('../util/pragma');
77
const docsUrl = require('../util/docsUrl');
88

9-
/**
10-
* Test whether a JSXElement is a fragment
11-
* @param {JSXElement} node
12-
* @param {object} context
13-
* @returns {boolean}
14-
*/
15-
function isFragment(node, context) {
16-
const name = node.openingElement.name;
17-
const reactPragma = pragmaUtil.getFromContext(context);
18-
const fragmentPragma = pragmaUtil.getFragmentFromContext(context);
19-
20-
// <Fragment>
21-
if (
22-
name.type === 'JSXIdentifier'
23-
&& name.name === fragmentPragma
24-
) {
25-
return true;
26-
}
27-
28-
// <React.Fragment>
29-
if (
30-
name.type === 'JSXMemberExpression'
31-
&& name.object.type === 'JSXIdentifier'
32-
&& name.object.name === reactPragma
33-
&& name.property.type === 'JSXIdentifier'
34-
&& name.property.name === fragmentPragma
35-
) {
36-
return true;
37-
}
38-
39-
return false;
40-
}
41-
42-
43-
/**
44-
* Test whether a node is an padding spaces trimmed by react runtime.
45-
* @param {ASTNode} node
46-
* @returns {boolean}
47-
*/
48-
function isPaddingSpaces(node) {
49-
return (node.type === 'JSXText' || node.type === 'Literal')
50-
&& /^\s*$/.test(node.raw)
51-
&& node.raw.includes('\n');
52-
}
53-
549

5510
module.exports = {
5611
meta: {
@@ -68,6 +23,54 @@ module.exports = {
6823
},
6924

7025
create(context) {
26+
const reactPragma = pragmaUtil.getFromContext(context);
27+
const fragmentPragma = pragmaUtil.getFragmentFromContext(context);
28+
29+
/**
30+
* Test whether a JSXElement is a fragment
31+
* @param {JSXElement} node
32+
* @returns {boolean}
33+
*/
34+
function isFragment(node) {
35+
const name = node.openingElement.name;
36+
37+
// <Fragment>
38+
if (
39+
name.type === 'JSXIdentifier'
40+
&& name.name === fragmentPragma
41+
) {
42+
return true;
43+
}
44+
45+
// <React.Fragment>
46+
if (
47+
name.type === 'JSXMemberExpression'
48+
&& name.object.type === 'JSXIdentifier'
49+
&& name.object.name === reactPragma
50+
&& name.property.type === 'JSXIdentifier'
51+
&& name.property.name === fragmentPragma
52+
) {
53+
return true;
54+
}
55+
56+
return false;
57+
}
58+
59+
/**
60+
* Test whether a node is an padding spaces trimmed by react runtime.
61+
* @param {ASTNode} node
62+
* @returns {boolean}
63+
*/
64+
function isPaddingSpaces(node) {
65+
return (node.type === 'JSXText' || node.type === 'Literal')
66+
&& /^\s*$/.test(node.raw)
67+
&& node.raw.includes('\n');
68+
}
69+
70+
/**
71+
* Test whether a JSXElement has less than two children, excluding paddings spaces.
72+
* @param {JSXElement|JSXFragment} node
73+
*/
7174
function hasLessThanTwoChildren(node) {
7275
if (node.children.length < 2) {
7376
return true;
@@ -108,7 +111,7 @@ module.exports = {
108111

109112
return {
110113
JSXElement(node) {
111-
if (isFragment(node, context)) {
114+
if (isFragment(node)) {
112115
checkNode(node);
113116
}
114117
},

0 commit comments

Comments
 (0)