Skip to content

Commit

Permalink
Fix: lines-between-class-memebers (fixes #9665)
Browse files Browse the repository at this point in the history
`lines-between-class-memebers` if a comment occurs between class members
  • Loading branch information
sakabar committed Dec 3, 2017
1 parent cdb1488 commit af7a7f3
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 3 deletions.
18 changes: 15 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,20 @@ 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);

let num = 0; // the numbers of lines of comments
const len = comments.length;

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

if (len > 0 && comments[len - 1].loc.end.line === second.loc.start.line) {
num -= 1;
}

return (second.loc.start.line - first.loc.end.line - 1) - num >= 1;
}

return {
Expand All @@ -65,8 +78,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
1 change: 1 addition & 0 deletions tests/lib/rules/lines-between-class-members.js
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@ 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 foo{ bar(){}\n\n;;baz(){}}",
"class foo{ bar(){};\n\nbaz(){}}",
Expand Down

0 comments on commit af7a7f3

Please sign in to comment.