Skip to content

Commit

Permalink
[Fix] jsx-props-no-multi-spaces: fix a false positive for beside co…
Browse files Browse the repository at this point in the history
…mments

Fixes #2874
  • Loading branch information
golopot authored and ljharb committed Dec 15, 2020
1 parent 9ec87e5 commit 796b609
Show file tree
Hide file tree
Showing 3 changed files with 23 additions and 19 deletions.
2 changes: 2 additions & 0 deletions CHANGELOG.md
Expand Up @@ -20,8 +20,10 @@ This change log adheres to standards from [Keep a CHANGELOG](http://keepachangel
* [`jsx-max-depth`]: avoid crash with childless jsx child ([#2869][] @ljharb, @AriPerkkio)
* [`no-unknown-property`]: avoid crash with prop named with Object.prototype key ([#2879][] @ljharb, @AriPerkkio)
* [`prop-types`]: default argument does not count as props-types declaration ([#2877][] @golopot)
* [`jsx-props-no-multi-spaces`]: fix a false positive for beside comments ([#2878][] @golopot)

[#2879]: https://github.com/yannickcr/eslint-plugin-react/issues/2879
[#2878]: https://github.com/yannickcr/eslint-plugin-react/pull/2878
[#2877]: https://github.com/yannickcr/eslint-plugin-react/pull/2877
[#2875]: https://github.com/yannickcr/eslint-plugin-react/issues/2875
[#2871]: https://github.com/yannickcr/eslint-plugin-react/issues/2871
Expand Down
32 changes: 13 additions & 19 deletions lib/rules/jsx-props-no-multi-spaces.js
Expand Up @@ -39,30 +39,24 @@ module.exports = {
}
}

function hasEmptyLines(node, leadingLineCount) {
const allComments = sourceCode.getCommentsBefore(node);
let commentLines = 0;

if (allComments.length > 0) {
allComments.forEach((comment) => {
const start = comment.loc.start.line;
const end = comment.loc.end.line;

commentLines += (end - start + 1);
});

return commentLines !== leadingLineCount;
// First and second must be adjacent nodes
function hasEmptyLines(first, second) {
const comments = sourceCode.getCommentsBefore(second);
const nodes = [].concat(first, comments, second);

for (let i = 1; i < nodes.length; i += 1) {
const prev = nodes[i - 1];
const curr = nodes[i];
if (curr.loc.start.line - prev.loc.end.line >= 2) {
return true;
}
}

return true;
return false;
}

function checkSpacing(prev, node) {
const prevNodeEndLine = prev.loc.end.line;
const currNodeStartLine = node.loc.start.line;
const leadingLineCount = currNodeStartLine - prevNodeEndLine - 1;

if (leadingLineCount > 0 && hasEmptyLines(node, leadingLineCount)) {
if (hasEmptyLines(prev, node)) {
context.report({
node,
message: `Expected no line gap between “${getPropName(prev)}” and “${getPropName(node)}”`
Expand Down
8 changes: 8 additions & 0 deletions tests/lib/rules/jsx-props-no-multi-spaces.js
Expand Up @@ -108,6 +108,14 @@ ruleTester.run('jsx-props-no-multi-spaces', rule, {
type="button"
/>
`
}, {
code: `
<App
foo="Some button" // comment
// comment
bar=""
/>
`
}, {
code: `
<button
Expand Down

0 comments on commit 796b609

Please sign in to comment.