From 33a0630c9371b64c6e5d70ac4508bca7a794839d Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?L=C3=A9o=20Maradan?= <3043485+Leomaradan@users.noreply.github.com> Date: Thu, 4 Aug 2022 17:20:22 +0200 Subject: [PATCH 1/3] Add an option to allow eslint-disable before the header --- lib/rules/header.js | 30 +++++++++++++++++++++++++++--- 1 file changed, 27 insertions(+), 3 deletions(-) diff --git a/lib/rules/header.js b/lib/rules/header.js index 3504b6f..892331f 100644 --- a/lib/rules/header.js +++ b/lib/rules/header.js @@ -26,11 +26,25 @@ 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") { + + if(allowDisableFirst) { + for (var i = 1; 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]); if (!txt.match(/^(\r\n|\r|\n)$/)) { @@ -85,6 +99,15 @@ function findSettings(options) { return null; } +function getAllowDisableFirst(options) { + var settings = findSettings(options); + if (settings && settings.allowDisableFirst !== undefined) { + return !!settings.allowDisableFirst; + } + + return false; +} + function getEOL(options) { var settings = findSettings(options); if (settings && settings.lineEndings === "unix") { @@ -127,6 +150,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))) { @@ -172,7 +196,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({ From d1a01e0ed2712544cf2abba4aa1254fa7a2926c3 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?L=C3=A9o=20Maradan?= <3043485+Leomaradan@users.noreply.github.com> Date: Thu, 4 Aug 2022 17:46:05 +0200 Subject: [PATCH 2/3] Update doc and test --- README.md | 10 ++++++++++ lib/rules/header.js | 32 +++++++++++++++----------------- tests/lib/rules/header.js | 6 +++++- 3 files changed, 30 insertions(+), 18 deletions(-) diff --git a/README.md b/README.md index 8e4fa81..1d8b1aa 100644 --- a/README.md +++ b/README.md @@ -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. diff --git a/lib/rules/header.js b/lib/rules/header.js index 892331f..8b8eb69 100644 --- a/lib/rules/header.js +++ b/lib/rules/header.js @@ -29,29 +29,27 @@ function excludeShebangs(comments) { function getLeadingComments(context, node, allowDisableFirst) { var all = excludeShebangs(context.getSourceCode().getAllComments(node.body.length ? node.body[0] : node)); - if(allowDisableFirst) { - for (var i = 1; i < all.length; ++i) { - if (all[i].type.toLowerCase() === "block") { - if(all[i].value.trim().indexOf('eslint-disable') === -1) { - return [all[i]]; - } + if (allowDisableFirst) { + for (var i = 1; 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]]; - } + 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) { @@ -101,8 +99,8 @@ function findSettings(options) { function getAllowDisableFirst(options) { var settings = findSettings(options); - if (settings && settings.allowDisableFirst !== undefined) { - return !!settings.allowDisableFirst; + if (settings && settings.allowDisableFirst === true) { + return true; } return false; diff --git a/tests/lib/rules/header.js b/tests/lib/rules/header.js index b42f8f0..a50d77a 100644 --- a/tests/lib/rules/header.js +++ b/tests/lib/rules/header.js @@ -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: [ { From 4c2b9afb55ec1068c2fd77a6713bacdfa1d1d0d5 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?L=C3=A9o=20Maradan?= <3043485+Leomaradan@users.noreply.github.com> Date: Thu, 4 Aug 2022 17:55:26 +0200 Subject: [PATCH 3/3] Index start at 0 --- lib/rules/header.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/lib/rules/header.js b/lib/rules/header.js index 8b8eb69..a864934 100644 --- a/lib/rules/header.js +++ b/lib/rules/header.js @@ -30,7 +30,7 @@ function getLeadingComments(context, node, allowDisableFirst) { var all = excludeShebangs(context.getSourceCode().getAllComments(node.body.length ? node.body[0] : node)); if (allowDisableFirst) { - for (var i = 1; i < all.length; ++i) { + 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]];