Skip to content

Commit 128b38f

Browse files
committed
jsx-closing-tag-location: support shorthand fragments
1 parent 0a68c28 commit 128b38f

File tree

2 files changed

+74
-34
lines changed

2 files changed

+74
-34
lines changed

lib/rules/jsx-closing-tag-location.js

Lines changed: 37 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -22,45 +22,48 @@ module.exports = {
2222
},
2323

2424
create: function(context) {
25-
return {
26-
JSXClosingElement: function(node) {
27-
if (!node.parent) {
28-
return;
29-
}
30-
31-
const opening = node.parent.openingElement;
32-
if (opening.loc.start.line === node.loc.start.line) {
33-
return;
34-
}
25+
function handleClosingElement(node) {
26+
if (!node.parent) {
27+
return;
28+
}
3529

36-
if (opening.loc.start.column === node.loc.start.column) {
37-
return;
38-
}
30+
const opening = node.parent.openingElement || node.parent.openingFragment;
31+
if (opening.loc.start.line === node.loc.start.line) {
32+
return;
33+
}
3934

40-
let message;
41-
if (!astUtil.isNodeFirstInLine(context, node)) {
42-
message = 'Closing tag of a multiline JSX expression must be on its own line.';
43-
} else {
44-
message = 'Expected closing tag to match indentation of opening.';
45-
}
35+
if (opening.loc.start.column === node.loc.start.column) {
36+
return;
37+
}
4638

47-
context.report({
48-
node: node,
49-
loc: node.loc,
50-
message,
51-
fix: function(fixer) {
52-
const indent = Array(opening.loc.start.column + 1).join(' ');
53-
if (astUtil.isNodeFirstInLine(context, node)) {
54-
return fixer.replaceTextRange(
55-
[node.range[0] - node.loc.start.column, node.range[0]],
56-
indent
57-
);
58-
}
39+
let message;
40+
if (!astUtil.isNodeFirstInLine(context, node)) {
41+
message = 'Closing tag of a multiline JSX expression must be on its own line.';
42+
} else {
43+
message = 'Expected closing tag to match indentation of opening.';
44+
}
5945

60-
return fixer.insertTextBefore(node, `\n${indent}`);
46+
context.report({
47+
node: node,
48+
loc: node.loc,
49+
message,
50+
fix: function(fixer) {
51+
const indent = Array(opening.loc.start.column + 1).join(' ');
52+
if (astUtil.isNodeFirstInLine(context, node)) {
53+
return fixer.replaceTextRange(
54+
[node.range[0] - node.loc.start.column, node.range[0]],
55+
indent
56+
);
6157
}
62-
});
63-
}
58+
59+
return fixer.insertTextBefore(node, `\n${indent}`);
60+
}
61+
});
62+
}
63+
64+
return {
65+
JSXClosingElement: handleClosingElement,
66+
JSXClosingFragment: handleClosingElement
6467
};
6568
}
6669
};

tests/lib/rules/jsx-closing-tag-location.js

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,18 @@ ruleTester.run('jsx-closing-tag-location', rule, {
3636
code: `
3737
<App>foo</App>
3838
`
39+
}, {
40+
code: `
41+
<>
42+
foo
43+
</>
44+
`,
45+
parser: 'babel-eslint'
46+
}, {
47+
code: `
48+
<>foo</>
49+
`,
50+
parser: 'babel-eslint'
3951
}],
4052

4153
invalid: [{
@@ -61,5 +73,30 @@ ruleTester.run('jsx-closing-tag-location', rule, {
6173
</App>
6274
`,
6375
errors: MESSAGE_OWN_LINE
76+
}, {
77+
code: `
78+
<>
79+
foo
80+
</>
81+
`,
82+
parser: 'babel-eslint',
83+
output: `
84+
<>
85+
foo
86+
</>
87+
`,
88+
errors: MESSAGE_MATCH_INDENTATION
89+
}, {
90+
code: `
91+
<>
92+
foo</>
93+
`,
94+
parser: 'babel-eslint',
95+
output: `
96+
<>
97+
foo
98+
</>
99+
`,
100+
errors: MESSAGE_OWN_LINE
64101
}]
65102
});

0 commit comments

Comments
 (0)