@tony.ganchev/eslint-plugin-header 3.1.11 → 3.1.12

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/README.md CHANGED
@@ -10,17 +10,18 @@ defined in the rule settings.
10
10
  ## Table of Contents
11
11
 
12
12
  1. [Scope and Acknowledgements](#scope-and-acknowledgements)
13
- 2. [Usage](#usage)
13
+ 2. [Compatibility](#compatibility)
14
+ 3. [Usage](#usage)
14
15
  1. [File-based Configuration](#file-based-configuration)
15
16
  2. [Inline Configuration](#inline-configuration)
16
17
  1. [Header Contents Configuration](#header-contents-configuration)
17
18
  2. [Trailing Empty Lines Configuration](#trailing-empty-lines-configuration)
18
19
  3. [Line Endings](#line-endings)
19
- 3. [Examples](#examples)
20
- 4. [Versioning](#versioning)
20
+ 4. [Examples](#examples)
21
+ 5. [Versioning](#versioning)
21
22
  1. [What is a Feature?](#what-is-a-feature)
22
23
  2. [What is Backward-compatibility?](#what-is-backward-compatibility)
23
- 5. [License](#license)
24
+ 6. [License](#license)
24
25
 
25
26
  ## Scope and Acknowledgements
26
27
 
@@ -39,6 +40,11 @@ It addresses the following issus:
39
40
  forward. This would come at the expense of plugin compatibility and the
40
41
  portability of fixes to the upstream repository.
41
42
 
43
+ ## Compatibility
44
+
45
+ The plugin supports ESLint 7 / 8 / 9 / 10rc0 (check package.json for details).
46
+ Both flat config and legacy, hierarchical config can be used.
47
+
42
48
  ## Usage
43
49
 
44
50
  This rule takes between 1 and 4 arguments after the rule validation severity.
@@ -0,0 +1,42 @@
1
+ /*
2
+ * MIT License
3
+ *
4
+ * Copyright (c) 2025-present Tony Ganchev and contributors
5
+ *
6
+ * Permission is hereby granted, free of charge, to any person obtaining a copy
7
+ * of this software and associated documentation files (the “Software”), to deal
8
+ * in the Software without restriction, including without limitation the rights
9
+ * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
10
+ * copies of the Software, and to permit persons to whom the Software is
11
+ * furnished to do so, subject to the following conditions:
12
+ *
13
+ * The above copyright notice and this permission notice shall be included in
14
+ * all copies or substantial portions of the Software.
15
+ *
16
+ * THE SOFTWARE IS PROVIDED “AS IS”, WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
17
+ * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18
+ * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
19
+ * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
20
+ * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
21
+ * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
22
+ * SOFTWARE.
23
+ */
24
+
25
+ "use strict";
26
+
27
+ /**
28
+ * @typedef {import('eslint').Rule.RuleContext} RuleContext
29
+ * @typedef {import('eslint').SourceCode} SourceCode
30
+ */
31
+
32
+ module.exports = {
33
+ /**
34
+ * Provides compatibility wrapper for ESLint 7 through 10 for getting the
35
+ * full source code object from the execution context.
36
+ * @param {RuleContext} context ESLint execution context.
37
+ * @returns {SourceCode} the source-code object.
38
+ */
39
+ contextSourceCode: function(context) {
40
+ return context.sourceCode || context.getSourceCode();
41
+ }
42
+ };
@@ -28,6 +28,7 @@ const assert = require("assert");
28
28
  const fs = require("fs");
29
29
  const os = require("os");
30
30
  const commentParser = require("../comment-parser");
31
+ const { contextSourceCode } = require("./eslint-utils");
31
32
  const { description, recommended, url } = require("./header.docs");
32
33
  const { commentTypeOptions, lineEndingOptions, schema } = require("./header.schema");
33
34
 
@@ -110,13 +111,14 @@ function excludeShebangs(comments) {
110
111
  * @returns {Comment[]} lines that constitute the leading comment.
111
112
  */
112
113
  function getLeadingComments(context, node) {
113
- const all = excludeShebangs(context.sourceCode.getAllComments(node.body.length ? node.body[0] : node));
114
+ const sourceCode = contextSourceCode(context);
115
+ const all = excludeShebangs(sourceCode.getAllComments(node.body.length ? node.body[0] : node));
114
116
  if (all[0].type.toLowerCase() === commentTypeOptions.block) {
115
117
  return [all[0]];
116
118
  }
117
119
  let i = 1;
118
120
  for (; i < all.length; ++i) {
119
- const txt = context.sourceCode.text.slice(all[i - 1].range[1], all[i].range[0]);
121
+ const txt = sourceCode.text.slice(all[i - 1].range[1], all[i].range[0]);
120
122
  if (!txt.match(/^(\r\n|\r|\n)$/)) {
121
123
  break;
122
124
  }
@@ -187,14 +189,15 @@ function genPrependFixer(commentType, context, headerLines, eol, numNewlines) {
187
189
  return function(fixer) {
188
190
  let insertPos = 0;
189
191
  let newHeader = genCommentBody(commentType, headerLines, eol, numNewlines);
190
- if (context.sourceCode.text.startsWith("#!")) {
191
- const firstNewLinePos = context.sourceCode.text.indexOf("\n");
192
- insertPos = firstNewLinePos === -1 ? context.sourceCode.text.length : firstNewLinePos + 1;
192
+ const sourceCode = contextSourceCode(context);
193
+ if (sourceCode.text.startsWith("#!")) {
194
+ const firstNewLinePos = sourceCode.text.indexOf("\n");
195
+ insertPos = firstNewLinePos === -1 ? sourceCode.text.length : firstNewLinePos + 1;
193
196
  if (firstNewLinePos === -1) {
194
197
  newHeader = eol + newHeader;
195
198
  }
196
199
  }
197
- const numEmptyLines = leadingEmptyLines(context.sourceCode.text.substring(insertPos));
200
+ const numEmptyLines = leadingEmptyLines(sourceCode.text.substring(insertPos));
198
201
  const additionalEmptyLines = Math.max(0, numNewlines - numEmptyLines);
199
202
  newHeader += eol.repeat(additionalEmptyLines);
200
203
  return fixer.insertTextBeforeRange(
@@ -219,7 +222,7 @@ function genPrependFixer(commentType, context, headerLines, eol, numNewlines) {
219
222
  function genReplaceFixer(commentType, context, leadingComments, headerLines, eol, numNewlines) {
220
223
  return function(fixer) {
221
224
  const commentRange = genCommentsRange(leadingComments);
222
- const emptyLines = leadingEmptyLines(context.sourceCode.text.substring(commentRange[1]));
225
+ const emptyLines = leadingEmptyLines(contextSourceCode(context).text.substring(commentRange[1]));
223
226
  const missingNewlines = Math.max(0, numNewlines - emptyLines);
224
227
  const eols = eol.repeat(missingNewlines);
225
228
  return fixer.replaceTextRange(
@@ -397,8 +400,9 @@ module.exports = {
397
400
  * @returns {void}
398
401
  */
399
402
  Program: function(node) {
400
- if (!hasHeader(context.sourceCode.text)) {
401
- const hasShebang = context.sourceCode.text.startsWith("#!");
403
+ const sourceCode = contextSourceCode(context);
404
+ if (!hasHeader(sourceCode.text)) {
405
+ const hasShebang = sourceCode.text.startsWith("#!");
402
406
  const line = hasShebang ? 2 : 1;
403
407
  context.report({
404
408
  loc: {
@@ -577,8 +581,8 @@ module.exports = {
577
581
  }
578
582
  }
579
583
 
580
- const actualLeadingEmptyLines = leadingEmptyLines(
581
- context.sourceCode.text.substring(leadingComments[headerLines.length - 1].range[1]));
584
+ const actualLeadingEmptyLines =
585
+ leadingEmptyLines(sourceCode.text.substring(leadingComments[headerLines.length - 1].range[1]));
582
586
  const missingEmptyLines = numNewlines - actualLeadingEmptyLines;
583
587
  if (missingEmptyLines > 0) {
584
588
  context.report({
@@ -717,8 +721,8 @@ module.exports = {
717
721
  return;
718
722
  }
719
723
 
720
- const actualLeadingEmptyLines = leadingEmptyLines(
721
- context.sourceCode.text.substring(leadingComments[0].range[1]));
724
+ const actualLeadingEmptyLines =
725
+ leadingEmptyLines(sourceCode.text.substring(leadingComments[0].range[1]));
722
726
  const missingEmptyLines = numNewlines - actualLeadingEmptyLines;
723
727
  if (missingEmptyLines > 0) {
724
728
  context.report({
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@tony.ganchev/eslint-plugin-header",
3
- "version": "3.1.11",
3
+ "version": "3.1.12",
4
4
  "description": "ESLint plugin to ensure files begin with a given comment, usually a copyright or license notice.",
5
5
  "main": "index.js",
6
6
  "files": [
@@ -11,19 +11,21 @@
11
11
  "eslint": "npx eslint .",
12
12
  "lint": "npm run eslint && npm run markdownlint",
13
13
  "markdownlint": "npx markdownlint-cli *.md",
14
- "test": "npm run lint && npm run unit",
15
- "unit": "npx nyc --reporter=html --reporter=text --reporter=text-summary --reporter=lcov --check-coverage=true --statements=100 --branches=100 --lines=100 --functions=100 mocha tests/lib/*.js tests/lib/**/*.js"
14
+ "test": "npm run lint && npm run unit && npm run e2e",
15
+ "unit": "npx nyc --reporter=html --reporter=text --reporter=text-summary --reporter=lcov --check-coverage=true --statements=100 --branches=100 --lines=100 --functions=100 mocha tests/lib/*.js tests/lib/**/*.js",
16
+ "e2e": "npx mocha --timeout 60000 tests/e2e/*.js"
16
17
  },
17
18
  "devDependencies": {
18
- "@eslint/eslintrc": "^3.3.1",
19
- "@eslint/js": "^9.39.1",
19
+ "@eslint/eslintrc": "^3.3.3",
20
+ "@eslint/js": "^9.39.2",
20
21
  "@eslint/markdown": "^7.5.1",
21
- "@stylistic/eslint-plugin": "^5.5.0",
22
- "eslint": "^9.39.1",
23
- "eslint-plugin-eslint-plugin": "^7.2.0",
24
- "eslint-plugin-jsdoc": "^61.1.12",
25
- "eslint-plugin-n": "^17.23.1",
26
- "mocha": "^11.7.5",
22
+ "@stylistic/eslint-plugin": "^5.7.0",
23
+ "eslint": "^9.39.2",
24
+ "eslint-plugin-eslint-plugin": "^7.3.0",
25
+ "eslint-plugin-jsdoc": "^62.1.0",
26
+ "eslint-plugin-n": "^17.23.2",
27
+ "markdownlint-cli": "^0.47.0",
28
+ "mocha": "^12.0.0-beta-5",
27
29
  "nyc": "^17.1.0",
28
30
  "testdouble": "^3.20.2"
29
31
  },
@@ -40,7 +42,7 @@
40
42
  },
41
43
  "author": "Stuart Knightley",
42
44
  "license": "MIT",
43
- "contributors": [
45
+ "maintainers": [
44
46
  "Tony Ganchev"
45
47
  ]
46
48
  }