diff --git a/lib/comment-parser.js b/lib/comment-parser.js index 5a67386..2794c28 100644 --- a/lib/comment-parser.js +++ b/lib/comment-parser.js @@ -4,6 +4,14 @@ // single kind of comment. It won't detect multiple block comments. module.exports = function commentParser(text) { + // @see https://stackoverflow.com/a/20060315 + var trailingNewLinesMatches = text.match(/\n(?=\s*$)/g); + var trailingNewLinesCount = 1; + + if (trailingNewLinesMatches) { + trailingNewLinesCount += trailingNewLinesMatches.length; + } + text = text.trim(); if (text.substr(0, 2) === "//") { @@ -11,13 +19,14 @@ module.exports = function commentParser(text) { "line", text.split(/\r?\n/).map(function(line) { return line.substr(2); - }) + }), + trailingNewLinesCount ]; } else if ( text.substr(0, 2) === "/*" && text.substr(-2) === "*/" ) { - return ["block", text.substring(2, text.length - 2)]; + return ["block", text.substring(2, text.length - 2), trailingNewLinesCount]; } else { throw new Error("Could not parse comment file: the file must contain either just line comments (//) or a single block comment (/* ... */)"); } diff --git a/lib/rules/header.js b/lib/rules/header.js index 3504b6f..6210d7d 100644 --- a/lib/rules/header.js +++ b/lib/rules/header.js @@ -125,8 +125,6 @@ module.exports = { }, create: function(context) { var options = context.options; - var numNewlines = options.length > 2 ? options[2] : 1; - var eol = getEOL(options); // If just one option then read comment from file if (options.length === 1 || (options.length === 2 && findSettings(options))) { @@ -134,6 +132,9 @@ module.exports = { options = commentParser(text); } + var numNewlines = options.length > 2 ? options[2] : 1; + var eol = getEOL(options); + var commentType = options[0].toLowerCase(); var headerLines, fixLines = []; // If any of the lines are regular expressions, then we can't diff --git a/tests/lib/comment-parser-test.js b/tests/lib/comment-parser-test.js index f43c6d4..08fe36d 100644 --- a/tests/lib/comment-parser-test.js +++ b/tests/lib/comment-parser-test.js @@ -7,7 +7,12 @@ var commentParser = require("../../lib/comment-parser"); describe("comment parser", function() { it("parses block comments", function() { var result = commentParser("/* pass1\n pass2 */ "); - assert.deepEqual(result, ["block", " pass1\n pass2 "]); + assert.deepEqual(result, ["block", " pass1\n pass2 ", 0]); + }); + + it("parses block comments with Windows EOLs", function() { + var result = commentParser("/* pass1\r\n pass2 */\r\n\r\n "); + assert.deepEqual(result, ["block", " pass1\r\n pass2 ", 2]); }); it("throws an error when a block comment isn't ended", function() { @@ -18,6 +23,6 @@ describe("comment parser", function() { it("parses line comments", function() { var result = commentParser("// pass1\n// pass2\n "); - assert.deepEqual(result, ["line", [" pass1", " pass2"]]); + assert.deepEqual(result, ["line", [" pass1", " pass2"], 1]); }); });