diff options
Diffstat (limited to 'tools/node_modules/eslint/lib/rules/lines-between-class-members.js')
-rw-r--r-- | tools/node_modules/eslint/lib/rules/lines-between-class-members.js | 57 |
1 files changed, 53 insertions, 4 deletions
diff --git a/tools/node_modules/eslint/lib/rules/lines-between-class-members.js b/tools/node_modules/eslint/lib/rules/lines-between-class-members.js index 85e8c69358..252984da5a 100644 --- a/tools/node_modules/eslint/lib/rules/lines-between-class-members.js +++ b/tools/node_modules/eslint/lib/rules/lines-between-class-members.js @@ -15,7 +15,8 @@ module.exports = { docs: { description: "require or disallow an empty line between class members", category: "Stylistic Issues", - recommended: false + recommended: false, + url: "https://eslint.org/docs/rules/lines-between-class-members" }, fixable: "whitespace", @@ -55,7 +56,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 { @@ -65,8 +115,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; |