Skip to content

Commit 006f52b

Browse files
committed
Multiline Block comments are sticky to Multiline components. Add newline before first
comment.
1 parent 4f05c5b commit 006f52b

File tree

2 files changed

+106
-3
lines changed

2 files changed

+106
-3
lines changed

lib/rules/jsx-newline.js

+18-3
Original file line numberDiff line numberDiff line change
@@ -77,6 +77,17 @@ module.exports = {
7777
return /^\s*{\/\*/.test(elementRawValue);
7878
}
7979

80+
function findFirstNonBlockComment(index, elements) {
81+
if (index >= elements.length) return;
82+
83+
const element = elements[index];
84+
const isElementJSX = element.type === 'JSXElement' || element.type === 'JSXExpressionContainer';
85+
86+
if (!isBlockCommentInCurlyBraces(element) && isElementJSX) return element;
87+
88+
return findFirstNonBlockComment(index + 1, elements);
89+
}
90+
8091
return {
8192
'Program:exit'() {
8293
jsxElementParents.forEach((parent) => {
@@ -98,7 +109,13 @@ module.exports = {
98109
// Check adjacent sibling has the proper amount of newlines
99110
const isWithoutNewLine = !/\n\s*\n/.test(firstAdjacentSibling.value);
100111

101-
if (allowMultilines && (isMultilined(element) || isMultilined(secondAdjacentSibling))) {
112+
if (isBlockCommentInCurlyBraces(element)) return;
113+
114+
if (
115+
allowMultilines
116+
&& (isMultilined(element)
117+
|| isMultilined(findFirstNonBlockComment(index + 2, elements)))
118+
) {
102119
if (!isWithoutNewLine) return;
103120

104121
const regex = /(\n)(?!.*\1)/g;
@@ -121,8 +138,6 @@ module.exports = {
121138

122139
if (isWithoutNewLine === prevent) return;
123140

124-
if (isBlockCommentInCurlyBraces(element)) return;
125-
126141
const messageId = prevent
127142
? 'prevent'
128143
: 'require';

tests/lib/rules/jsx-newline.js

+88
Original file line numberDiff line numberDiff line change
@@ -159,6 +159,24 @@ new RuleTester({ parserOptions }).run('jsx-newline', rule, {
159159
features: ['fragment'],
160160
options: [{ prevent: true, allowMultilines: true }],
161161
},
162+
{
163+
code: `
164+
<div>
165+
{/* this does not have a newline */}
166+
<Icon f7='gear' />
167+
{/* neither does this */}
168+
<OneLineComponent />
169+
170+
{/* but this one needs one */}
171+
<Button>
172+
<IconPreview />
173+
Button 2
174+
<span></span>
175+
</Button>
176+
</div>
177+
`,
178+
options: [{ prevent: true, allowMultilines: true }],
179+
},
162180
{
163181
code: `
164182
<div>
@@ -284,6 +302,76 @@ new RuleTester({ parserOptions }).run('jsx-newline', rule, {
284302
`,
285303
errors: [{ messageId: 'require' }],
286304
},
305+
{
306+
code: `
307+
<div>
308+
{/* this does not have a newline */}
309+
<Icon f7='gear' />
310+
{/* neither does this */}
311+
<OneLineComponent />
312+
{/* but this one needs one */}
313+
<Button>
314+
<IconPreview />
315+
Button 2
316+
<span></span>
317+
</Button>
318+
</div>
319+
`,
320+
output: `
321+
<div>
322+
{/* this does not have a newline */}
323+
<Icon f7='gear' />
324+
{/* neither does this */}
325+
<OneLineComponent />
326+
327+
{/* but this one needs one */}
328+
<Button>
329+
<IconPreview />
330+
Button 2
331+
<span></span>
332+
</Button>
333+
</div>
334+
`,
335+
options: [{ prevent: true, allowMultilines: true }],
336+
errors: [{ messageId: 'allowMultilines' }],
337+
},
338+
{
339+
code: `
340+
<div>
341+
{/* this does not have a newline */}
342+
<Icon f7='gear' />
343+
{/* neither does this */}
344+
<OneLineComponent />
345+
{/* Multiline */}
346+
{/* Block comments */}
347+
{/* Stick to MultilineComponent */}
348+
<Button>
349+
<IconPreview />
350+
Button 2
351+
<span></span>
352+
</Button>
353+
</div>
354+
`,
355+
output: `
356+
<div>
357+
{/* this does not have a newline */}
358+
<Icon f7='gear' />
359+
{/* neither does this */}
360+
<OneLineComponent />
361+
362+
{/* Multiline */}
363+
{/* Block comments */}
364+
{/* Stick to MultilineComponent */}
365+
<Button>
366+
<IconPreview />
367+
Button 2
368+
<span></span>
369+
</Button>
370+
</div>
371+
`,
372+
options: [{ prevent: true, allowMultilines: true }],
373+
errors: [{ messageId: 'allowMultilines' }],
374+
},
287375
{
288376
code: `
289377
<div>

0 commit comments

Comments
 (0)