Skip to content

Commit 796b609

Browse files
chiawendtljharb
authored andcommitted
[Fix] jsx-props-no-multi-spaces: fix a false positive for beside comments
Fixes #2874
1 parent 9ec87e5 commit 796b609

File tree

3 files changed

+23
-19
lines changed

3 files changed

+23
-19
lines changed

CHANGELOG.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,8 +20,10 @@ This change log adheres to standards from [Keep a CHANGELOG](http://keepachangel
2020
* [`jsx-max-depth`]: avoid crash with childless jsx child ([#2869][] @ljharb, @AriPerkkio)
2121
* [`no-unknown-property`]: avoid crash with prop named with Object.prototype key ([#2879][] @ljharb, @AriPerkkio)
2222
* [`prop-types`]: default argument does not count as props-types declaration ([#2877][] @golopot)
23+
* [`jsx-props-no-multi-spaces`]: fix a false positive for beside comments ([#2878][] @golopot)
2324

2425
[#2879]: https://github.com/yannickcr/eslint-plugin-react/issues/2879
26+
[#2878]: https://github.com/yannickcr/eslint-plugin-react/pull/2878
2527
[#2877]: https://github.com/yannickcr/eslint-plugin-react/pull/2877
2628
[#2875]: https://github.com/yannickcr/eslint-plugin-react/issues/2875
2729
[#2871]: https://github.com/yannickcr/eslint-plugin-react/issues/2871

lib/rules/jsx-props-no-multi-spaces.js

Lines changed: 13 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -39,30 +39,24 @@ module.exports = {
3939
}
4040
}
4141

42-
function hasEmptyLines(node, leadingLineCount) {
43-
const allComments = sourceCode.getCommentsBefore(node);
44-
let commentLines = 0;
45-
46-
if (allComments.length > 0) {
47-
allComments.forEach((comment) => {
48-
const start = comment.loc.start.line;
49-
const end = comment.loc.end.line;
50-
51-
commentLines += (end - start + 1);
52-
});
53-
54-
return commentLines !== leadingLineCount;
42+
// First and second must be adjacent nodes
43+
function hasEmptyLines(first, second) {
44+
const comments = sourceCode.getCommentsBefore(second);
45+
const nodes = [].concat(first, comments, second);
46+
47+
for (let i = 1; i < nodes.length; i += 1) {
48+
const prev = nodes[i - 1];
49+
const curr = nodes[i];
50+
if (curr.loc.start.line - prev.loc.end.line >= 2) {
51+
return true;
52+
}
5553
}
5654

57-
return true;
55+
return false;
5856
}
5957

6058
function checkSpacing(prev, node) {
61-
const prevNodeEndLine = prev.loc.end.line;
62-
const currNodeStartLine = node.loc.start.line;
63-
const leadingLineCount = currNodeStartLine - prevNodeEndLine - 1;
64-
65-
if (leadingLineCount > 0 && hasEmptyLines(node, leadingLineCount)) {
59+
if (hasEmptyLines(prev, node)) {
6660
context.report({
6761
node,
6862
message: `Expected no line gap between “${getPropName(prev)}” and “${getPropName(node)}”`

tests/lib/rules/jsx-props-no-multi-spaces.js

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -108,6 +108,14 @@ ruleTester.run('jsx-props-no-multi-spaces', rule, {
108108
type="button"
109109
/>
110110
`
111+
}, {
112+
code: `
113+
<App
114+
foo="Some button" // comment
115+
// comment
116+
bar=""
117+
/>
118+
`
111119
}, {
112120
code: `
113121
<button

0 commit comments

Comments
 (0)