Skip to content

Commit 43d4ba8

Browse files
sakabaraladdin-add
authored andcommitted
Fix: false positive on rulelines-between-class-members (fixes #9665) (#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.
1 parent 234cd26 commit 43d4ba8

File tree

2 files changed

+58
-3
lines changed

2 files changed

+58
-3
lines changed

lib/rules/lines-between-class-members.js

+51-3
Original file line numberDiff line numberDiff line change
@@ -55,7 +55,56 @@ module.exports = {
5555
* @returns {boolean} True if there is at least a line between the tokens
5656
*/
5757
function isPaddingBetweenTokens(first, second) {
58-
return second.loc.start.line - first.loc.end.line >= 2;
58+
const comments = sourceCode.getCommentsBefore(second);
59+
const len = comments.length;
60+
61+
// If there is no comments
62+
if (len === 0) {
63+
const linesBetweenFstAndSnd = second.loc.start.line - first.loc.end.line - 1;
64+
65+
return linesBetweenFstAndSnd >= 1;
66+
}
67+
68+
69+
// If there are comments
70+
let sumOfCommentLines = 0; // the numbers of lines of comments
71+
let prevCommentLineNum = -1; // line number of the end of the previous comment
72+
73+
for (let i = 0; i < len; i++) {
74+
const commentLinesOfThisComment = comments[i].loc.end.line - comments[i].loc.start.line + 1;
75+
76+
sumOfCommentLines += commentLinesOfThisComment;
77+
78+
/*
79+
* If this comment and the previous comment are in the same line,
80+
* the count of comment lines is duplicated. So decrement sumOfCommentLines.
81+
*/
82+
if (prevCommentLineNum === comments[i].loc.start.line) {
83+
sumOfCommentLines -= 1;
84+
}
85+
86+
prevCommentLineNum = comments[i].loc.end.line;
87+
}
88+
89+
/*
90+
* If the first block and the first comment are in the same line,
91+
* the count of comment lines is duplicated. So decrement sumOfCommentLines.
92+
*/
93+
if (first.loc.end.line === comments[0].loc.start.line) {
94+
sumOfCommentLines -= 1;
95+
}
96+
97+
/*
98+
* If the last comment and the second block are in the same line,
99+
* the count of comment lines is duplicated. So decrement sumOfCommentLines.
100+
*/
101+
if (comments[len - 1].loc.end.line === second.loc.start.line) {
102+
sumOfCommentLines -= 1;
103+
}
104+
105+
const linesBetweenFstAndSnd = second.loc.start.line - first.loc.end.line - 1;
106+
107+
return linesBetweenFstAndSnd - sumOfCommentLines >= 1;
59108
}
60109

61110
return {
@@ -65,8 +114,7 @@ module.exports = {
65114
for (let i = 0; i < body.length - 1; i++) {
66115
const curFirst = sourceCode.getFirstToken(body[i]);
67116
const curLast = sourceCode.getLastToken(body[i]);
68-
const comments = sourceCode.getCommentsBefore(body[i + 1]);
69-
const nextFirst = comments.length ? comments[0] : sourceCode.getFirstToken(body[i + 1]);
117+
const nextFirst = sourceCode.getFirstToken(body[i + 1]);
70118
const isPadded = isPaddingBetweenTokens(curLast, nextFirst);
71119
const isMulti = !astUtils.isTokenOnSameLine(curFirst, curLast);
72120
const skip = !isMulti && options[1].exceptAfterSingleLine;

tests/lib/rules/lines-between-class-members.js

+7
Original file line numberDiff line numberDiff line change
@@ -36,13 +36,20 @@ ruleTester.run("lines-between-class-members", rule, {
3636
"class foo{ bar(){}\n\nbaz(){}}",
3737
"class foo{ bar(){}\n\n/*comments*/baz(){}}",
3838
"class foo{ bar(){}\n\n//comments\nbaz(){}}",
39+
"class foo{ bar(){}\n//comments\n\nbaz(){}}",
40+
"class A{ foo() {} // a comment\n\nbar() {}}",
41+
"class A{ foo() {}\n/* a */ /* b */\n\nbar() {}}",
42+
"class A{ foo() {}/* a */ \n\n /* b */bar() {}}",
3943

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

4347
{ code: "class foo{ bar(){}\nbaz(){}}", options: ["never"] },
4448
{ code: "class foo{ bar(){}\n/*comments*/baz(){}}", options: ["never"] },
4549
{ code: "class foo{ bar(){}\n//comments\nbaz(){}}", options: ["never"] },
50+
{ code: "class foo{ bar(){}/* comments\n\n*/baz(){}}", options: ["never"] },
51+
{ code: "class foo{ bar(){}/* \ncomments\n*/baz(){}}", options: ["never"] },
52+
{ code: "class foo{ bar(){}\n/* \ncomments\n*/\nbaz(){}}", options: ["never"] },
4653

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

0 commit comments

Comments
 (0)