Skip to content

Commit

Permalink
Fix: false positive on rulelines-between-class-members (fixes #9665) (
Browse files Browse the repository at this point in the history
#9680)

* Fix: `lines-between-class-memebers` (fixes #9665)

`lines-between-class-memebers` if a comment occurs between class members

* Add tests suggested by reviewers.

* Add comments

* Refactor: remove a nest.

* Refactor: declare variables just before they are needed.
  • Loading branch information
sakabar authored and aladdin-add committed Dec 15, 2017
1 parent 234cd26 commit 43d4ba8
Show file tree
Hide file tree
Showing 2 changed files with 58 additions and 3 deletions.
54 changes: 51 additions & 3 deletions lib/rules/lines-between-class-members.js
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,56 @@ module.exports = {
* @returns {boolean} True if there is at least a line between the tokens
*/
function isPaddingBetweenTokens(first, second) {
return second.loc.start.line - first.loc.end.line >= 2;
const comments = sourceCode.getCommentsBefore(second);
const len = comments.length;

// If there is no comments
if (len === 0) {
const linesBetweenFstAndSnd = second.loc.start.line - first.loc.end.line - 1;

return linesBetweenFstAndSnd >= 1;
}


// If there are comments
let sumOfCommentLines = 0; // the numbers of lines of comments
let prevCommentLineNum = -1; // line number of the end of the previous comment

for (let i = 0; i < len; i++) {
const commentLinesOfThisComment = comments[i].loc.end.line - comments[i].loc.start.line + 1;

sumOfCommentLines += commentLinesOfThisComment;

/*
* If this comment and the previous comment are in the same line,
* the count of comment lines is duplicated. So decrement sumOfCommentLines.
*/
if (prevCommentLineNum === comments[i].loc.start.line) {
sumOfCommentLines -= 1;
}

prevCommentLineNum = comments[i].loc.end.line;
}

/*
* If the first block and the first comment are in the same line,
* the count of comment lines is duplicated. So decrement sumOfCommentLines.
*/
if (first.loc.end.line === comments[0].loc.start.line) {
sumOfCommentLines -= 1;
}

/*
* If the last comment and the second block are in the same line,
* the count of comment lines is duplicated. So decrement sumOfCommentLines.
*/
if (comments[len - 1].loc.end.line === second.loc.start.line) {
sumOfCommentLines -= 1;
}

const linesBetweenFstAndSnd = second.loc.start.line - first.loc.end.line - 1;

return linesBetweenFstAndSnd - sumOfCommentLines >= 1;
}

return {
Expand All @@ -65,8 +114,7 @@ module.exports = {
for (let i = 0; i < body.length - 1; i++) {
const curFirst = sourceCode.getFirstToken(body[i]);
const curLast = sourceCode.getLastToken(body[i]);
const comments = sourceCode.getCommentsBefore(body[i + 1]);
const nextFirst = comments.length ? comments[0] : sourceCode.getFirstToken(body[i + 1]);
const nextFirst = sourceCode.getFirstToken(body[i + 1]);
const isPadded = isPaddingBetweenTokens(curLast, nextFirst);
const isMulti = !astUtils.isTokenOnSameLine(curFirst, curLast);
const skip = !isMulti && options[1].exceptAfterSingleLine;
Expand Down
7 changes: 7 additions & 0 deletions tests/lib/rules/lines-between-class-members.js
Original file line number Diff line number Diff line change
Expand Up @@ -36,13 +36,20 @@ ruleTester.run("lines-between-class-members", rule, {
"class foo{ bar(){}\n\nbaz(){}}",
"class foo{ bar(){}\n\n/*comments*/baz(){}}",
"class foo{ bar(){}\n\n//comments\nbaz(){}}",
"class foo{ bar(){}\n//comments\n\nbaz(){}}",
"class A{ foo() {} // a comment\n\nbar() {}}",
"class A{ foo() {}\n/* a */ /* b */\n\nbar() {}}",
"class A{ foo() {}/* a */ \n\n /* b */bar() {}}",

"class foo{ bar(){}\n\n;;baz(){}}",
"class foo{ bar(){};\n\nbaz(){}}",

{ code: "class foo{ bar(){}\nbaz(){}}", options: ["never"] },
{ code: "class foo{ bar(){}\n/*comments*/baz(){}}", options: ["never"] },
{ code: "class foo{ bar(){}\n//comments\nbaz(){}}", options: ["never"] },
{ code: "class foo{ bar(){}/* comments\n\n*/baz(){}}", options: ["never"] },
{ code: "class foo{ bar(){}/* \ncomments\n*/baz(){}}", options: ["never"] },
{ code: "class foo{ bar(){}\n/* \ncomments\n*/\nbaz(){}}", options: ["never"] },

{ code: "class foo{ bar(){}\n\nbaz(){}}", options: ["always"] },
{ code: "class foo{ bar(){}\n\n/*comments*/baz(){}}", options: ["always"] },
Expand Down

0 comments on commit 43d4ba8

Please sign in to comment.