Skip to content

Commit 4f05c5b

Browse files
committed
Don't add new line after block commments
1 parent 865ed16 commit 4f05c5b

File tree

2 files changed

+69
-0
lines changed

2 files changed

+69
-0
lines changed

lib/rules/jsx-newline.js

+8
Original file line numberDiff line numberDiff line change
@@ -72,6 +72,11 @@ module.exports = {
7272
const jsxElementParents = new Set();
7373
const sourceCode = context.getSourceCode();
7474

75+
function isBlockCommentInCurlyBraces(element) {
76+
const elementRawValue = sourceCode.getText(element);
77+
return /^\s*{\/\*/.test(elementRawValue);
78+
}
79+
7580
return {
7681
'Program:exit'() {
7782
jsxElementParents.forEach((parent) => {
@@ -115,6 +120,9 @@ module.exports = {
115120
}
116121

117122
if (isWithoutNewLine === prevent) return;
123+
124+
if (isBlockCommentInCurlyBraces(element)) return;
125+
118126
const messageId = prevent
119127
? 'prevent'
120128
: 'require';

tests/lib/rules/jsx-newline.js

+61
Original file line numberDiff line numberDiff line change
@@ -105,6 +105,43 @@ new RuleTester({ parserOptions }).run('jsx-newline', rule, {
105105
</Button>
106106
`,
107107
},
108+
{
109+
code: `
110+
<Button popoverOpen='#settings-popover' style={{ width: 'fit-content' }}>
111+
{/* fake-eslint-disable-next-line should also work insinde a component */}
112+
<Icon f7='gear' />
113+
</Button>
114+
`,
115+
},
116+
{
117+
code: `
118+
<Button popoverOpen='#settings-popover' style={{ width: 'fit-content' }}>
119+
{/* should work insinde a component */}
120+
{/* and it should work when using multiple comments */}
121+
<Icon f7='gear' />
122+
</Button>
123+
`,
124+
},
125+
{
126+
code: `
127+
<Button popoverOpen='#settings-popover' style={{ width: 'fit-content' }}>
128+
{/* this is a multiline
129+
block comment */}
130+
<Icon f7='gear' />
131+
</Button>
132+
`,
133+
},
134+
{
135+
code: `
136+
<>
137+
{/* does this */}
138+
<Icon f7='gear' />
139+
140+
{/* also work with multiple components and inside a fragment? */}
141+
<OneLineComponent />
142+
</>
143+
`,
144+
},
108145
{
109146
code: `
110147
<>
@@ -223,6 +260,30 @@ new RuleTester({ parserOptions }).run('jsx-newline', rule, {
223260
`,
224261
errors: [{ messageId: 'require' }],
225262
},
263+
{
264+
code: `
265+
<div>
266+
{/* This should however still not work*/}
267+
<Icon f7='gear' />
268+
269+
<OneLineComponent />
270+
{/* Comments between components still need a newLine */}
271+
<OneLineComponent />
272+
</div>
273+
`,
274+
output: `
275+
<div>
276+
{/* This should however still not work*/}
277+
<Icon f7='gear' />
278+
279+
<OneLineComponent />
280+
281+
{/* Comments between components still need a newLine */}
282+
<OneLineComponent />
283+
</div>
284+
`,
285+
errors: [{ messageId: 'require' }],
286+
},
226287
{
227288
code: `
228289
<div>

0 commit comments

Comments
 (0)