A flexible and --fix
able 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.
This plugin is best used with a formatter like prettier.
This plugin aims to be a successor to the popular but defunct plugin
eslint-plugin-header.
eslint-plugin-headers
strives to implement the same features as
eslint-plugin-header
where possible, while sensibly extending features,
supporting modern JavaScript frameworks, and maintaining compatibility with
other common tools.
You'll first need to install ESLint:
npm i eslint --save-dev
Next, install eslint-plugin-headers
:
npm install eslint-plugin-headers --save-dev
Import the default export from the estlin-plugin-headers
module. Add the
headers
key to the plugins section of your config object in the configuration
file. You can omit the eslint-plugin-
prefix:
import headers from "eslint-plugin-headers";
export default [{
plugins: {
headers
}
}]
Then configure the rules you want to use under the rules section.
import headers from "eslint-plugin-headers";
export default [{
plugins: {
headers
}
rules: {
"headers/header-format": [
"error",
{
source: "string",
content: "Copyright 2024. All rights reserved."
}
]
}
}]
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."
}
]
}
}
This project supports parsing Vue files via the AST generated by the
vue-eslint-parser
package. To properly apply this plugin's rules to Vue
files, you must:
- Install the
vue-eslint-parser
package as a dev dependency. - Specify the
vue-eslint-parser
in the configuration'slanguageOptions.parser
field (or the"parser"
field for legacy configurations), and - Set the
enableVueSupport
flag for the appropriate rules.
Example configuration:
import headers from "eslint-plugin-headers";
import vueEslintParser from "vue-eslint-parser";
export default [
{
plugins: {
headers
},
files: ["**/*.vue"],
rules: {
"headers/header-format": [
"error",
{
source: "string",
content: "This is a header.",
enableVueSupport: true,
}
]
},
languageOptions: {
parser: vueEslintParser,
},
}
];
With this configuration the following file would be transformed like so:
Before:
<template>
<p>This is an example Vue file.</p>
</template>
After applying the fix:
<!--
This is a header.
-->
<template>
<p>This is an example Vue file.</p>
</template>
Since the vue-eslint-parser
package strives to maintain compatibility with
rules targeting JavaScript, it exposes Vue AST tokens to rules in different
ways than the default ESlint parser. The means of exposing HTML Comment nodes
are of specific interest to this plugin, and the mechanism to access these
are substantially different than how a plugin would typically read a comment
node, making it necessary to both specify this particular parser as well as
setting the flag.
Example 0:
Example Configuration:
{
"rules": {
"headers/header-format": [
"error",
{
"source": "string",
"content": "Copyright 2024. All rights reserved."
}
]
}
}
Using the above configuration, 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 are supplied through a single object with the following properties:
Name | Type | Required | Default | Description |
---|---|---|---|---|
source | "file" | "string" |
Yes | Indicates 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. No effect if enableVueSupport: true . |
content | string | When source: "string" |
The string to enforce in the header comment. | |
path | string | When source: "file" |
The path to a file containing the header content to enforce. | |
preservePragmas | boolean | No | true |
Preserves existing pragma expressions in leading comments when updating header. No effect when style: "line" . |
blockPrefix | string | No | See below | Content at the start of the leading comment block. |
blockSuffix | string | No | See below | Content at the end of the leading comment block. |
linePrefix | string | No | See below | Content prepended to the start of each line of content. |
trailingNewlines | number | No | Number of empty lines to enforce after the leading comment. | |
variables | object | No | The keys to find and values to fill when formatting the provided header. Values must be strings. | |
enableVueSupport | boolean | No | false |
EXPERIMENTAL! Enable support for parsing .vue files. Must be used with vue-eslint-parser . See above for details. |
Example configuration:
export default [
{
// ...
rules: {
"headers/header-format": [
"error",
{
source: "string",
content: "This is a header.",
// ...{Additional Configuration}
}
]
},
}
]
The subsequent section titles contain the additional configuration inserted above, and the resulting comment that will be produced.
Expected/produced header:
// This is a header.
- Default block prefix: None
- Default block suffix: None
- Default line prefix:
" "
Expected/produced header:
/**
* This is a header.
*/
- Default block prefix:
"*\n"
- Default block suffix:
"\n "
- Default line prefix:
" * "
Expected/produced header:
<!--
This is a header.
-->
- Default block prefix:
"\n"
- Default block suffix:
"\n"
- Default line prefix:
" "
- Add support for common pragma expressions that don't utilize the
@
symbol (e.g. eslint-disable)
🔧 Automatically fixable by the --fix
CLI option.
Name | Description | 🔧 |
---|---|---|
header-format | Verifies the content and format of a file's leading comment block. | 🔧 |