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
  • Loading branch information
golopot committed Dec 15, 2020
1 parent d873bdb commit 1556a55
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 19 deletions.
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 1556a55

Please sign in to comment.