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
10 changes: 10 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -154,6 +154,16 @@ The rule works with both unix and windows line endings. For ESLint `--fix`, the
```
Possible values are `unix` for `\n` and `windows` for `\r\n` line endings.

### ESlint disable comments

Sometimes you want to add a eslint-disable for the whole file before the header. This can be done by adding a settings as follows:

```json
"rules": {
"header/header": [2, "block", ["Copyright 2018", "My Company"], {"allowDisableFirst": true}]
}
```

## Examples

The following examples are all valid.
Expand Down
36 changes: 29 additions & 7 deletions lib/rules/header.js
Original file line number Diff line number Diff line change
Expand Up @@ -26,18 +26,30 @@ function excludeShebangs(comments) {
// are ONLY separated by a single newline. Note that this does not actually
// check if they are at the start of the file since that is already checked by
// hasHeader().
function getLeadingComments(context, node) {
function getLeadingComments(context, node, allowDisableFirst) {
var all = excludeShebangs(context.getSourceCode().getAllComments(node.body.length ? node.body[0] : node));
if (all[0].type.toLowerCase() === "block") {
return [all[0]];

if (allowDisableFirst) {
for (var i = 0; i < all.length; ++i) {
if (all[i].type.toLowerCase() === "block") {
if (all[i].value.trim().indexOf("eslint-disable") === -1) {
return [all[i]];
}
}
}
} else {
if (all[0].type.toLowerCase() === "block") {
return [all[0]];
}
}
for (var i = 1; i < all.length; ++i) {
var txt = context.getSourceCode().getText().slice(all[i - 1].range[1], all[i].range[0]);

for (var j = 1; j < all.length; ++j) {
var txt = context.getSourceCode().getText().slice(all[j - 1].range[1], all[j].range[0]);
if (!txt.match(/^(\r\n|\r|\n)$/)) {
break;
}
}
return all.slice(0, i);
return all.slice(0, j);
}

function genCommentBody(commentType, textArray, eol, numNewlines) {
Expand Down Expand Up @@ -85,6 +97,15 @@ function findSettings(options) {
return null;
}

function getAllowDisableFirst(options) {
var settings = findSettings(options);
if (settings && settings.allowDisableFirst === true) {
return true;
}

return false;
}

function getEOL(options) {
var settings = findSettings(options);
if (settings && settings.lineEndings === "unix") {
Expand Down Expand Up @@ -127,6 +148,7 @@ module.exports = {
var options = context.options;
var numNewlines = options.length > 2 ? options[2] : 1;
var eol = getEOL(options);
var allowDisableFirst = getAllowDisableFirst(options);

// If just one option then read comment from file
if (options.length === 1 || (options.length === 2 && findSettings(options))) {
Expand Down Expand Up @@ -172,7 +194,7 @@ module.exports = {
fix: genPrependFixer(commentType, node, fixLines, eol, numNewlines)
});
} else {
var leadingComments = getLeadingComments(context, node);
var leadingComments = getLeadingComments(context, node, allowDisableFirst);

if (!leadingComments.length) {
context.report({
Expand Down
6 changes: 5 additions & 1 deletion tests/lib/rules/header.js
Original file line number Diff line number Diff line change
Expand Up @@ -123,7 +123,11 @@ ruleTester.run("header", rule, {
" * Copyright",
" "
], 0]
}
},
{
code: "/* eslint-disable max-lines */\n/**\n * Copyright 2020\n * My Company\n **/\n\n/*Log number one*/\nconsole.log(1);",
options: ["block", "*\n * Copyright 2020\n * My Company\n *", 2, {"allowDisableFirst": true}],
},
],
invalid: [
{
Expand Down