6
6
const pragmaUtil = require ( '../util/pragma' ) ;
7
7
const docsUrl = require ( '../util/docsUrl' ) ;
8
8
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
-
54
9
55
10
module . exports = {
56
11
meta : {
@@ -68,6 +23,54 @@ module.exports = {
68
23
} ,
69
24
70
25
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
+ */
71
74
function hasLessThanTwoChildren ( node ) {
72
75
if ( node . children . length < 2 ) {
73
76
return true ;
@@ -108,7 +111,7 @@ module.exports = {
108
111
109
112
return {
110
113
JSXElement ( node ) {
111
- if ( isFragment ( node , context ) ) {
114
+ if ( isFragment ( node ) ) {
112
115
checkNode ( node ) ;
113
116
}
114
117
} ,
0 commit comments