Skip to content

Commit 083adf8

Browse files
committed
jsx-child-element-spacing: support shorthand fragments
1 parent dd00d8a commit 083adf8

File tree

2 files changed

+54
-29
lines changed

2 files changed

+54
-29
lines changed

lib/rules/jsx-child-element-spacing.js

Lines changed: 32 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -68,39 +68,42 @@ module.exports = {
6868
INLINE_ELEMENTS.has(elementName(node))
6969
);
7070

71+
const handleJSX = node => {
72+
let lastChild = null;
73+
let child = null;
74+
(node.children.concat([null])).forEach(nextChild => {
75+
if (
76+
(lastChild || nextChild) &&
77+
(!lastChild || isInlineElement(lastChild)) &&
78+
(child && (child.type === 'Literal' || child.type === 'JSXText')) &&
79+
(!nextChild || isInlineElement(nextChild)) &&
80+
true
81+
) {
82+
if (lastChild && child.value.match(TEXT_FOLLOWING_ELEMENT_PATTERN)) {
83+
context.report({
84+
node: lastChild,
85+
loc: lastChild.loc.end,
86+
message: `Ambiguous spacing after previous element ${elementName(lastChild)}`
87+
});
88+
} else if (nextChild && child.value.match(TEXT_PRECEDING_ELEMENT_PATTERN)) {
89+
context.report({
90+
node: nextChild,
91+
loc: nextChild.loc.start,
92+
message: `Ambiguous spacing before next element ${elementName(nextChild)}`
93+
});
94+
}
95+
}
96+
lastChild = child;
97+
child = nextChild;
98+
});
99+
};
100+
71101
const TEXT_FOLLOWING_ELEMENT_PATTERN = /^\s*\n\s*\S/;
72102
const TEXT_PRECEDING_ELEMENT_PATTERN = /\S\s*\n\s*$/;
73103

74104
return {
75-
JSXElement: function(node) {
76-
let lastChild = null;
77-
let child = null;
78-
(node.children.concat([null])).forEach(nextChild => {
79-
if (
80-
(lastChild || nextChild) &&
81-
(!lastChild || isInlineElement(lastChild)) &&
82-
(child && (child.type === 'Literal' || child.type === 'JSXText')) &&
83-
(!nextChild || isInlineElement(nextChild)) &&
84-
true
85-
) {
86-
if (lastChild && child.value.match(TEXT_FOLLOWING_ELEMENT_PATTERN)) {
87-
context.report({
88-
node: lastChild,
89-
loc: lastChild.loc.end,
90-
message: `Ambiguous spacing after previous element ${elementName(lastChild)}`
91-
});
92-
} else if (nextChild && child.value.match(TEXT_PRECEDING_ELEMENT_PATTERN)) {
93-
context.report({
94-
node: nextChild,
95-
loc: nextChild.loc.start,
96-
message: `Ambiguous spacing before next element ${elementName(nextChild)}`
97-
});
98-
}
99-
}
100-
lastChild = child;
101-
child = nextChild;
102-
});
103-
}
105+
JSXElement: handleJSX,
106+
JSXFragment: handleJSX
104107
};
105108
}
106109
};

tests/lib/rules/jsx-child-element-spacing.js

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,13 @@ ruleTester.run('jsx-child-element-spacing', rule, {
1717
foo
1818
</App>
1919
`
20+
}, {
21+
code: `
22+
<>
23+
foo
24+
</>
25+
`,
26+
parser: 'babel-eslint'
2027
}, {
2128
code: `
2229
<App>
@@ -146,6 +153,21 @@ ruleTester.run('jsx-child-element-spacing', rule, {
146153
]
147154
}, {
148155
code: `
156+
<>
157+
foo
158+
<a>bar</a>
159+
</>
160+
`,
161+
parser: 'babel-eslint',
162+
errors: [
163+
{
164+
message: 'Ambiguous spacing before next element a',
165+
line: 4,
166+
column: 3
167+
}
168+
]
169+
}, {
170+
code: `
149171
<App>
150172
<a>bar</a>
151173
baz

0 commit comments

Comments
 (0)