1.1.2 ā€¢ Published 3 months ago

eslint-plugin-headers v1.1.2

Weekly downloads
-
License
ISC
Repository
github
Last release
3 months ago

eslint-plugin-headers

A flexible and --fixable rule for checking, inserting, and formatting file headers.

Supports variable content injection, configurable usage of block or line comments, custom comment block prefixes and suffixes, custom line prefixes, and spacing between the header and code.

Useful for inserting, enforcing, and updating copyright or licensing notices while preserving pragma expressions in leading content blocks.

Installation

You'll first need to install ESLint:

npm i eslint --save-dev

Next, install eslint-plugin-headers:

npm install eslint-plugin-headers --save-dev

Usage

Add headers to the plugins section of your .eslintrc configuration file. You can omit the eslint-plugin- prefix:

{
  "plugins": ["headers"]
}

Then configure the rules you want to use under the rules section.

{
  "rules": {
    "headers/header-format": [
      "error",
      {
        "source": "string",
        "content": "Copyright 2024. All rights reserved."
      }
    ]
  }
}

Example 0:

Using the configuration from above, here's a file without a matching header:

module.exports = 42;

When the fix is applied, the file now appears so:

/**
 * Copyright 2024. All rights reserved.
 */
module.exports = 42;

Example 1:

Using the same configuration, this file already has a header, this one containing pragmas:

/**
 * @fileoverview This file contains a magic number.
 * @author Rob Misasi
 */
module.exports = 42;

When the fix is applied, the file now appears so:

/**
 * Copyright 2024. All rights reserved.
 *
 * @fileoverview This file contains a magic number.
 * @author Rob Misasi
 */
module.exports = 42;

Example 2: Using the following configuration, variable values can be injected into the provided header:

{
  "rules": {
    "headers/header-format": [
      "error",
      {
        "source": "string",
        "content": "Copyright {year} {company}. All rights reserved.",
        "variables": {
          "year": "2077",
          "company": "Software Incorporated"
        }
      }
    ]
  }
}

We can then apply a fix to the following file:

/**
 * Copyright 1947 Hardware LLC. All rights reserved.
 */
module.exports = 42;

And get the resulting header:

/**
 * Copyright 2077 Software Incorporated. All rights reserved.
 */
module.exports = 42;

Options

Options are supplied through a single object with the following properties:

NameTypeRequiredDefaultDescription
source"file" | "string"YesIndicates how the header content is supplied.
style"line" | "jsdoc"No"jsdoc"Indicates the comment style to enforce. A leading line-style comment block will only include adjacent line comments, although a line comment's content may be empty.
contentstringWhen source: "string"The string to enforce in the header comment.
pathstringWhen source: "file"The path to a file containing the header content to enforce.
preservePragmasbooleanNotruePreserves existing pragma expressions in leading comments when updating header. No effect when style: "line".
blockPrefixstringNo"*" + newline when style: "jsdoc"Content at the start of the leading comment block.
blockSuffixstringNonewline + " " when style: "jsdoc"Content at the end of the leading comment block.
linePrefixstringNo" * " when style: "jsdoc", " " when style: "line"Content prepended to the start of each line of content.
trailingNewlinesnumberNoNumber of empty lines to enforce after the leading comment.
variablesobjectNoThe keys to find and values to fill when formatting the provided header. Values must be strings.

Future

  • Add support for common pragma expressions that don't utilize the @ symbol (e.g. eslint-disable)

Rules

šŸ”§ Automatically fixable by the --fix CLI option.

NameDescriptionšŸ”§
header-formatVerifies the content and format of a file's leading comment block.šŸ”§
1.1.1

3 months ago

1.1.2

3 months ago

1.1.0

5 months ago

1.0.5

5 months ago

1.0.4

5 months ago

1.0.3

10 months ago

1.0.2

10 months ago

1.0.1

10 months ago

1.0.0

10 months ago

0.1.0

10 months ago