Skip to content
This repository has been archived by the owner on Feb 12, 2024. It is now read-only.

Commit

Permalink
Fix: lines-around-comment multiple errors on same line (fixes eslin…
Browse files Browse the repository at this point in the history
  • Loading branch information
alberto authored and ilyavolodin committed May 3, 2016
1 parent a2cc54e commit a66bf19
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 12 deletions.
24 changes: 14 additions & 10 deletions lib/rules/lines-around-comment.js
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down Expand Up @@ -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;

Expand Down Expand Up @@ -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.");
}

Expand Down
2 changes: 0 additions & 2 deletions tests/lib/rules/lines-around-comment.js
Original file line number Diff line number Diff line change
Expand Up @@ -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 }
]
Expand Down

0 comments on commit a66bf19

Please sign in to comment.