Skip to content
Open
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
75 changes: 59 additions & 16 deletions lib/rules/header.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,10 @@ function isPattern(object) {
return typeof object === "object" && Object.prototype.hasOwnProperty.call(object, "pattern");
}

function isMultiline(object) {
return isPattern(object) && Object.prototype.hasOwnProperty.call(object, "multiline") && object.multiline === true;
}

function match(actual, expected) {
if (expected.test) {
return expected.test(actual);
Expand Down Expand Up @@ -135,7 +139,7 @@ module.exports = {
}

var commentType = options[0].toLowerCase();
var headerLines, fixLines = [];
var headerLines, fixLines = [], multiLines;
// If any of the lines are regular expressions, then we can't
// automatically fix them. We set this to true below once we
// ensure none of the lines are of type RegExp
Expand All @@ -151,9 +155,11 @@ module.exports = {
fixLines.push(line.template || line);
return isRegex ? new RegExp(line.pattern) : line;
});
multiLines = options[1].map(line => isMultiline(line));
} else if (isPattern(options[1])) {
var line = options[1];
headerLines = [new RegExp(line.pattern)];
multiLines = [isMultiline(line)];
fixLines.push(line.template || line);
// Same as above for regex and template
canFix = !!line.template;
Expand Down Expand Up @@ -191,23 +197,42 @@ module.exports = {
});
} else {
if (commentType === "line") {
if (leadingComments.length < headerLines.length) {
context.report({
loc: node.loc,
message: "incorrect header",
fix: canFix ? genReplaceFixer(commentType, context, leadingComments, fixLines, eol, numNewlines) : null
});
return;
}
for (var i = 0; i < headerLines.length; i++) {
if (!match(leadingComments[i].value, headerLines[i])) {
for (var i = 0, j = 0; i < headerLines.length && j < leadingComments.length; ) {
if (!match(leadingComments[j].value, headerLines[i])) {
if (multiLines[i]) {
if (++i < headerLines.length) {
if (!match(leadingComments[j].value, headerLines[i])) {
context.report({
loc: node.loc,
message: "incorrect header",
fix: canFix ? genReplaceFixer(commentType, context, leadingComments, fixLines, eol, numNewlines) : null
});
return;
}
}
else {
context.report({
loc: node.loc,
message: "incorrect header",
fix: canFix ? genReplaceFixer(commentType, context, leadingComments, fixLines, eol, numNewlines) : null
});
return;
}
++j;
if (!multiLines[i])
++i;
continue;
}
context.report({
loc: node.loc,
message: "incorrect header",
fix: canFix ? genReplaceFixer(commentType, context, leadingComments, fixLines, eol, numNewlines) : null
});
return;
}
++j;
if (!multiLines[i])
++i;
}

var postLineHeader = context.getSourceCode().text.substr(leadingComments[headerLines.length - 1].range[1], numNewlines * 2);
Expand All @@ -227,13 +252,31 @@ module.exports = {
}

var hasError = false;
if (leadingLines.length > headerLines.length) {
hasError = true;
}
for (i = 0; !hasError && i < headerLines.length; i++) {
if (!match(leadingLines[i], headerLines[i])) {

for (i = 0, j = 0; !hasError && i < headerLines.length && j < leadingLines.length; ) {
if (!match(leadingLines[j], headerLines[i])) {
if(multiLines[i]) {
if (++i < headerLines.length) {
if (!match(leadingLines[j], headerLines[i])) {
hasError = true;
continue;
}
}
else {
hasError = true;
continue;
}
++j;
if (!multiLines[i])
++i;
continue;
}
hasError = true;
}
++j;
if (!multiLines[i])
++i;
continue;
}

if (hasError) {
Expand Down