From a66bf19aa409bcac8020e8adf97e864b2a41eaa4 Mon Sep 17 00:00:00 2001 From: alberto Date: Tue, 3 May 2016 19:39:04 +0200 Subject: [PATCH] Fix: `lines-around-comment` multiple errors on same line (fixes #5965) (#5994) --- lib/rules/lines-around-comment.js | 24 ++++++++++++++---------- tests/lib/rules/lines-around-comment.js | 2 -- 2 files changed, 14 insertions(+), 12 deletions(-) diff --git a/lib/rules/lines-around-comment.js b/lib/rules/lines-around-comment.js index a32e8dda45ff..8e7d7b1aea80 100644 --- a/lib/rules/lines-around-comment.js +++ b/lib/rules/lines-around-comment.js @@ -126,6 +126,14 @@ module.exports = { var sourceCode = context.getSourceCode(); + var lines = sourceCode.lines, + numLines = lines.length + 1, + comments = sourceCode.getAllComments(), + commentLines = getCommentLineNums(comments), + emptyLines = getEmptyLineNums(lines), + commentAndEmptyLines = commentLines.concat(emptyLines), + reportedLines = []; + /** * Returns whether or not comments are on lines starting with or ending with code * @param {ASTNode} node The comment node to check. @@ -267,14 +275,6 @@ module.exports = { * @returns {void} */ function checkForEmptyLine(node, opts) { - - var lines = sourceCode.lines, - numLines = lines.length + 1, - comments = sourceCode.getAllComments(), - commentLines = getCommentLineNums(comments), - emptyLines = getEmptyLineNums(lines), - commentAndEmptyLines = commentLines.concat(emptyLines); - var after = opts.after, before = opts.before; @@ -306,12 +306,16 @@ module.exports = { } // check for newline before - if (!exceptionStartAllowed && before && !contains(prevLineNum, commentAndEmptyLines)) { + if (!exceptionStartAllowed && before && !contains(prevLineNum, commentAndEmptyLines) && + reportedLines.indexOf(prevLineNum) < 0) { + reportedLines.push(prevLineNum); context.report(node, "Expected line before comment."); } // check for newline after - if (!exceptionEndAllowed && after && !contains(nextLineNum, commentAndEmptyLines)) { + if (!exceptionEndAllowed && after && !contains(nextLineNum, commentAndEmptyLines) && + reportedLines.indexOf(nextLineNum) < 0) { + reportedLines.push(nextLineNum); context.report(node, "Expected line after comment."); } diff --git a/tests/lib/rules/lines-around-comment.js b/tests/lib/rules/lines-around-comment.js index fc633329ec92..9bbe3fada2c0 100644 --- a/tests/lib/rules/lines-around-comment.js +++ b/tests/lib/rules/lines-around-comment.js @@ -834,8 +834,6 @@ ruleTester.run("lines-around-comment", rule, { code: "bar()\n/* first block comment */ /* second block comment */\nvar a = 1;", options: [{ afterBlockComment: true, beforeBlockComment: true }], errors: [ - { message: beforeMessage, type: "Block", line: 2 }, - { message: afterMessage, type: "Block", line: 2 }, { message: beforeMessage, type: "Block", line: 2 }, { message: afterMessage, type: "Block", line: 2 } ]