Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
13 changes: 11 additions & 2 deletions lib/comment-parser.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,20 +4,29 @@
// 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) === "//") {
return [
"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 (/* ... */)");
}
Expand Down
5 changes: 3 additions & 2 deletions lib/rules/header.js
Original file line number Diff line number Diff line change
Expand Up @@ -125,15 +125,16 @@ 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))) {
var text = fs.readFileSync(context.options[0], "utf8");
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
Expand Down
9 changes: 7 additions & 2 deletions tests/lib/comment-parser-test.js
Original file line number Diff line number Diff line change
Expand Up @@ -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() {
Expand All @@ -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]);
});
});