Skip to content

Commit 9b5d6aa

Browse files
Belco90ljharb
authored andcommitted
[Fix] self-closing-comp: consider JSXMemberExpression as component too
1 parent 598277e commit 9b5d6aa

File tree

3 files changed

+82
-1
lines changed

3 files changed

+82
-1
lines changed

docs/rules/self-closing-comp.md

+10
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,8 @@ The following patterns are considered warnings:
1010

1111
```jsx
1212
var HelloJohn = <Hello name="John"></Hello>;
13+
14+
var HelloJohnCompound = <Hello.Compound name="John"></Hello.Compound>;
1315
```
1416

1517
The following patterns are **not** considered warnings:
@@ -21,8 +23,12 @@ var intentionalSpace = <div>{' '}</div>;
2123

2224
var HelloJohn = <Hello name="John" />;
2325

26+
var HelloJohnCompound = <Hello.Compound name="John" />;
27+
2428
var Profile = <Hello name="John"><img src="picture.png" /></Hello>;
2529

30+
var ProfileCompound = <Hello.Compound name="John"><img src="picture.png" /></Hello.Compound>;
31+
2632
var HelloSpace = <Hello>{' '}</Hello>;
2733
```
2834

@@ -58,7 +64,11 @@ var intentionalSpace = <div>{' '}</div>;
5864

5965
var HelloJohn = <Hello name="John" />;
6066

67+
var HelloJohnCompound = <Hello.Compound name="John" />;
68+
6169
var Profile = <Hello name="John"><img src="picture.png" /></Hello>;
70+
71+
var ProfileCompound = <Hello.Compound name="John"><img src="picture.png" /></Hello.Compound>;
6272
```
6373

6474
### `html`

lib/rules/self-closing-comp.js

+5-1
Original file line numberDiff line numberDiff line change
@@ -42,7 +42,11 @@ module.exports = {
4242

4343
create(context) {
4444
function isComponent(node) {
45-
return node.name && node.name.type === 'JSXIdentifier' && !jsxUtil.isDOMComponent(node);
45+
return (
46+
node.name &&
47+
(node.name.type === 'JSXIdentifier' || node.name.type === 'JSXMemberExpression') &&
48+
!jsxUtil.isDOMComponent(node)
49+
);
4650
}
4751

4852
function childrenIsEmpty(node) {

tests/lib/rules/self-closing-comp.js

+67
Original file line numberDiff line numberDiff line change
@@ -30,37 +30,66 @@ ruleTester.run('self-closing-comp', rule, {
3030
valid: [
3131
{
3232
code: 'var HelloJohn = <Hello name="John" />;'
33+
}, {
34+
code: 'var HelloJohn = <Hello.Compound name="John" />;'
3335
}, {
3436
code: 'var Profile = <Hello name="John"><img src="picture.png" /></Hello>;'
37+
}, {
38+
code: 'var Profile = <Hello.Compound name="John"><img src="picture.png" /></Hello.Compound>;'
3539
}, {
3640
code: `
3741
<Hello>
3842
<Hello name="John" />
3943
</Hello>
4044
`
45+
}, {
46+
code: `
47+
<Hello.Compound>
48+
<Hello.Compound name="John" />
49+
</Hello.Compound>
50+
`
4151
}, {
4252
code: 'var HelloJohn = <Hello name="John"> </Hello>;'
53+
}, {
54+
code: 'var HelloJohn = <Hello.Compound name="John"> </Hello.Compound>;'
4355
}, {
4456
code: 'var HelloJohn = <Hello name="John"> </Hello>;'
57+
}, {
58+
code: 'var HelloJohn = <Hello.Compound name="John"> </Hello.Compound>;'
4559
}, {
4660
code: 'var HelloJohn = <div>&nbsp;</div>;'
4761
}, {
4862
code: 'var HelloJohn = <div>{\' \'}</div>;'
4963
}, {
5064
code: 'var HelloJohn = <Hello name="John">&nbsp;</Hello>;'
65+
}, {
66+
code: 'var HelloJohn = <Hello.Compound name="John">&nbsp;</Hello.Compound>;'
5167
}, {
5268
code: 'var HelloJohn = <Hello name="John" />;',
5369
options: []
70+
}, {
71+
code: 'var HelloJohn = <Hello.Compound name="John" />;',
72+
options: []
5473
}, {
5574
code: 'var Profile = <Hello name="John"><img src="picture.png" /></Hello>;',
5675
options: []
76+
}, {
77+
code: 'var Profile = <Hello.Compound name="John"><img src="picture.png" /></Hello.Compound>;',
78+
options: []
5779
}, {
5880
code: `
5981
<Hello>
6082
<Hello name="John" />
6183
</Hello>
6284
`,
6385
options: []
86+
}, {
87+
code: `
88+
<Hello.Compound>
89+
<Hello.Compound name="John" />
90+
</Hello.Compound>
91+
`,
92+
options: []
6493
}, {
6594
code: 'var HelloJohn = <div> </div>;',
6695
options: []
@@ -76,15 +105,27 @@ ruleTester.run('self-closing-comp', rule, {
76105
}, {
77106
code: 'var HelloJohn = <Hello name="John">&nbsp;</Hello>;',
78107
options: []
108+
}, {
109+
code: 'var HelloJohn = <Hello.Compound name="John">&nbsp;</Hello.Compound>;',
110+
options: []
79111
}, {
80112
code: 'var HelloJohn = <Hello name="John"></Hello>;',
81113
options: [{component: false}]
114+
}, {
115+
code: 'var HelloJohn = <Hello.Compound name="John"></Hello.Compound>;',
116+
options: [{component: false}]
82117
}, {
83118
code: 'var HelloJohn = <Hello name="John">\n</Hello>;',
84119
options: [{component: false}]
120+
}, {
121+
code: 'var HelloJohn = <Hello.Compound name="John">\n</Hello.Compound>;',
122+
options: [{component: false}]
85123
}, {
86124
code: 'var HelloJohn = <Hello name="John"> </Hello>;',
87125
options: [{component: false}]
126+
}, {
127+
code: 'var HelloJohn = <Hello.Compound name="John"> </Hello.Compound>;',
128+
options: [{component: false}]
88129
}, {
89130
code: 'var contentContainer = <div className="content" />;',
90131
options: [{html: true}]
@@ -121,26 +162,52 @@ ruleTester.run('self-closing-comp', rule, {
121162
errors: [{
122163
message: 'Empty components are self-closing'
123164
}]
165+
}, {
166+
code: 'var CompoundHelloJohn = <Hello.Compound name="John"></Hello.Compound>;',
167+
output: 'var CompoundHelloJohn = <Hello.Compound name="John" />;',
168+
errors: [{
169+
message: 'Empty components are self-closing'
170+
}]
124171
}, {
125172
code: 'var HelloJohn = <Hello name="John">\n</Hello>;',
126173
output: 'var HelloJohn = <Hello name="John" />;',
127174
errors: [{
128175
message: 'Empty components are self-closing'
129176
}]
177+
}, {
178+
code: 'var HelloJohn = <Hello.Compound name="John">\n</Hello.Compound>;',
179+
output: 'var HelloJohn = <Hello.Compound name="John" />;',
180+
errors: [{
181+
message: 'Empty components are self-closing'
182+
}]
130183
}, {
131184
code: 'var HelloJohn = <Hello name="John"></Hello>;',
132185
output: 'var HelloJohn = <Hello name="John" />;',
133186
options: [],
134187
errors: [{
135188
message: 'Empty components are self-closing'
136189
}]
190+
}, {
191+
code: 'var HelloJohn = <Hello.Compound name="John"></Hello.Compound>;',
192+
output: 'var HelloJohn = <Hello.Compound name="John" />;',
193+
options: [],
194+
errors: [{
195+
message: 'Empty components are self-closing'
196+
}]
137197
}, {
138198
code: 'var HelloJohn = <Hello name="John">\n</Hello>;',
139199
output: 'var HelloJohn = <Hello name="John" />;',
140200
options: [],
141201
errors: [{
142202
message: 'Empty components are self-closing'
143203
}]
204+
}, {
205+
code: 'var HelloJohn = <Hello.Compound name="John">\n</Hello.Compound>;',
206+
output: 'var HelloJohn = <Hello.Compound name="John" />;',
207+
options: [],
208+
errors: [{
209+
message: 'Empty components are self-closing'
210+
}]
144211
}, {
145212
code: 'var contentContainer = <div className="content"></div>;',
146213
output: 'var contentContainer = <div className="content" />;',

0 commit comments

Comments
 (0)