Skip to content

Commit

Permalink
Update: Allow JSX exception in no-inline-comments (fixes #11270) (#12388
Browse files Browse the repository at this point in the history
)
  • Loading branch information
mdjermanovic authored and kaicataldo committed Nov 1, 2019
1 parent e17fb90 commit c78f4a7
Show file tree
Hide file tree
Showing 3 changed files with 369 additions and 14 deletions.
52 changes: 52 additions & 0 deletions docs/rules/no-inline-comments.md
Original file line number Diff line number Diff line change
Expand Up @@ -35,3 +35,55 @@ var foo = 5;
var bar = 5;
//This is a comment below a line of code
```

### JSX exception

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.

Examples of **incorrect** code for this rule:

```js
/*eslint no-inline-comments: "error"*/

var foo = <div>{ /* On the same line with other code */ }<h1>Some heading</h1></div>;

var bar = (
<div>
{ // These braces are not just for the comment, so it can't be on the same line
baz
}
</div>
);
```

Examples of **correct** code for this rule:

```js
/*eslint no-inline-comments: "error"*/

var foo = (
<div>
{/* These braces are just for this comment and there is nothing else on this line */}
<h1>Some heading</h1>
</div>
)

var bar = (
<div>
{
// There is nothing else on this line
baz
}
</div>
);

var quux = (
<div>
{/*
Multiline
comment
*/}
<h1>Some heading</h1>
</div>
)
```
36 changes: 25 additions & 11 deletions lib/rules/no-inline-comments.js
Original file line number Diff line number Diff line change
Expand Up @@ -35,22 +35,36 @@ module.exports = {
*/
function testCodeAroundComment(node) {

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

const preamble = startLine.slice(0, node.loc.start.column).trim();
// Nothing on both sides
if (isPreambleEmpty && isPostambleEmpty) {
return;
}

// Also check after the comment
const postamble = endLine.slice(node.loc.end.column).trim();
// JSX Exception
if (
(isPreambleEmpty || preamble === "{") &&
(isPostambleEmpty || postamble === "}")
) {
const enclosingNode = sourceCode.getNodeByRangeIndex(node.range[0]);

// Check that this comment isn't an ESLint directive
const isDirective = astUtils.isDirectiveComment(node);
if (enclosingNode && enclosingNode.type === "JSXEmptyExpression") {
return;
}
}

// Should be empty if there was only whitespace around the comment
if (!isDirective && (preamble || postamble)) {
context.report({ node, message: "Unexpected comment inline with code." });
// Don't report ESLint directive comments
if (astUtils.isDirectiveComment(node)) {
return;
}

context.report({ node, message: "Unexpected comment inline with code." });
}

//--------------------------------------------------------------------------
Expand Down
Loading

0 comments on commit c78f4a7

Please sign in to comment.