Skip to content

Commit c78f4a7

Browse files
mdjermanovickaicataldo
authored andcommitted
Update: Allow JSX exception in no-inline-comments (fixes #11270) (#12388)
1 parent e17fb90 commit c78f4a7

File tree

3 files changed

+369
-14
lines changed

3 files changed

+369
-14
lines changed

docs/rules/no-inline-comments.md

Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -35,3 +35,55 @@ var foo = 5;
3535
var bar = 5;
3636
//This is a comment below a line of code
3737
```
38+
39+
### JSX exception
40+
41+
Comments inside the curly braces in JSX are allowed to be on the same line as the braces, but only if they are not on the same line with other code, and the braces do not enclose an actual expression.
42+
43+
Examples of **incorrect** code for this rule:
44+
45+
```js
46+
/*eslint no-inline-comments: "error"*/
47+
48+
var foo = <div>{ /* On the same line with other code */ }<h1>Some heading</h1></div>;
49+
50+
var bar = (
51+
<div>
52+
{ // These braces are not just for the comment, so it can't be on the same line
53+
baz
54+
}
55+
</div>
56+
);
57+
```
58+
59+
Examples of **correct** code for this rule:
60+
61+
```js
62+
/*eslint no-inline-comments: "error"*/
63+
64+
var foo = (
65+
<div>
66+
{/* These braces are just for this comment and there is nothing else on this line */}
67+
<h1>Some heading</h1>
68+
</div>
69+
)
70+
71+
var bar = (
72+
<div>
73+
{
74+
// There is nothing else on this line
75+
baz
76+
}
77+
</div>
78+
);
79+
80+
var quux = (
81+
<div>
82+
{/*
83+
Multiline
84+
comment
85+
*/}
86+
<h1>Some heading</h1>
87+
</div>
88+
)
89+
```

lib/rules/no-inline-comments.js

Lines changed: 25 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -35,22 +35,36 @@ module.exports = {
3535
*/
3636
function testCodeAroundComment(node) {
3737

38-
// Get the whole line and cut it off at the start of the comment
39-
const startLine = String(sourceCode.lines[node.loc.start.line - 1]);
40-
const endLine = String(sourceCode.lines[node.loc.end.line - 1]);
38+
const startLine = String(sourceCode.lines[node.loc.start.line - 1]),
39+
endLine = String(sourceCode.lines[node.loc.end.line - 1]),
40+
preamble = startLine.slice(0, node.loc.start.column).trim(),
41+
postamble = endLine.slice(node.loc.end.column).trim(),
42+
isPreambleEmpty = !preamble,
43+
isPostambleEmpty = !postamble;
4144

42-
const preamble = startLine.slice(0, node.loc.start.column).trim();
45+
// Nothing on both sides
46+
if (isPreambleEmpty && isPostambleEmpty) {
47+
return;
48+
}
4349

44-
// Also check after the comment
45-
const postamble = endLine.slice(node.loc.end.column).trim();
50+
// JSX Exception
51+
if (
52+
(isPreambleEmpty || preamble === "{") &&
53+
(isPostambleEmpty || postamble === "}")
54+
) {
55+
const enclosingNode = sourceCode.getNodeByRangeIndex(node.range[0]);
4656

47-
// Check that this comment isn't an ESLint directive
48-
const isDirective = astUtils.isDirectiveComment(node);
57+
if (enclosingNode && enclosingNode.type === "JSXEmptyExpression") {
58+
return;
59+
}
60+
}
4961

50-
// Should be empty if there was only whitespace around the comment
51-
if (!isDirective && (preamble || postamble)) {
52-
context.report({ node, message: "Unexpected comment inline with code." });
62+
// Don't report ESLint directive comments
63+
if (astUtils.isDirectiveComment(node)) {
64+
return;
5365
}
66+
67+
context.report({ node, message: "Unexpected comment inline with code." });
5468
}
5569

5670
//--------------------------------------------------------------------------

0 commit comments

Comments
 (0)