eslint-plugin-headers
Advanced tools
Comparing version
@@ -9,4 +9,5 @@ # Verifies the content and format of a file's leading comment block (`headers/header-format`) | ||
files, these often conflict with tools that use or add preprocessor directives | ||
or pragmas embedded in these comments (e.g. jest). This rule exists to enforce | ||
the existence of specific header content while maintaining these directives. | ||
or pragmas embedded in these comments (e.g. jest and `@jest-environment`). This | ||
rule exists to enforce the existence of specific header content while | ||
maintaining these directives. | ||
@@ -19,2 +20,28 @@ ## Rule Details | ||
### Templates | ||
The provided header content can be formatted with variable information using | ||
the `variables` configuration option. Keys within the header content must be | ||
wrapped in braces to be formatted properly. For example, the configuration: | ||
```json | ||
{ | ||
... | ||
"content": "This is an {exampleKey}.", | ||
"variables": { | ||
"exampleKey": "example value" | ||
} | ||
} | ||
``` | ||
produces the following header: | ||
```js | ||
/** | ||
* This is an example value. | ||
*/ | ||
``` | ||
### Examples | ||
Examples of **incorrect** code for this rule: | ||
@@ -28,3 +55,3 @@ | ||
**Example 0:** | ||
**Example 0: Enforcing content** | ||
Configuration: | ||
@@ -61,3 +88,3 @@ | ||
**Example 1:** | ||
**Example 1: Enforcing content from a file** | ||
Configuration: | ||
@@ -91,3 +118,2 @@ | ||
*/ | ||
module.exports = 1701; | ||
@@ -104,6 +130,46 @@ ``` | ||
*/ | ||
module.exports = 1701; | ||
``` | ||
**Example 2: Using template variables** | ||
Configuration: | ||
```json | ||
{ | ||
"rules": { | ||
"headers/header-format": [ | ||
"error", | ||
{ | ||
"source": "string", | ||
"content": "Copyright Star Date {stardate} {company}. All rights reserved.", | ||
"variables": { | ||
"stardate": "101012.2", | ||
"company": "United Federation of Planets" | ||
} | ||
} | ||
] | ||
} | ||
} | ||
``` | ||
Original file: | ||
```js | ||
/** | ||
* @author Jean-Luc Picard | ||
*/ | ||
module.exports = "1701-D"; | ||
``` | ||
Fixed file: | ||
```js | ||
/** | ||
* Copyright Star Date 101012.2 United Federation of Planets. All rights reserved. | ||
* | ||
* @author Jean-Luc Picard | ||
*/ | ||
module.exports = "1701-D"; | ||
``` | ||
### Options | ||
@@ -122,5 +188,7 @@ | ||
| 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. | | ||
## When Not To Use It | ||
Do not use this rule if you have no use for enforcing content in a file header. | ||
Do not use this rule if you have no use for enforcing leading text content in a | ||
file header. |
@@ -116,2 +116,11 @@ /** | ||
}, | ||
variables: { | ||
type: "object", | ||
patternProperties: { | ||
"^.+$": { | ||
anyOf: [{ type: "string" }], | ||
}, | ||
}, | ||
additionalProperties: false, | ||
}, | ||
}, | ||
@@ -149,2 +158,12 @@ required: ["source"], | ||
let variables = context.options[0]["variables"]; | ||
if (variables) { | ||
Object.keys(variables).forEach((key) => { | ||
expectedHeaderText = expectedHeaderText.replaceAll( | ||
`{${key}}`, | ||
variables[key], | ||
); | ||
}); | ||
} | ||
const headerEol = getEolCharacter(expectedHeaderText); | ||
@@ -151,0 +170,0 @@ const eol = getEolCharacter(context.sourceCode.getText()); |
{ | ||
"name": "eslint-plugin-headers", | ||
"version": "1.0.5", | ||
"version": "1.1.0", | ||
"description": "A flexible and `--fix`able rule for checking, inserting, and formatting file headers.", | ||
@@ -5,0 +5,0 @@ "repository": { |
@@ -6,5 +6,5 @@ # eslint-plugin-headers | ||
Supports configurable usage of block or line comments, custom comment block | ||
prefixes and suffixes, custom line prefixes, and spacing between the header | ||
and code. | ||
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. | ||
@@ -47,3 +47,3 @@ Useful for inserting, enforcing, and updating copyright or licensing notices | ||
"source": "string", | ||
"content": "Copyright 2023. All rights reserved." | ||
"content": "Copyright 2024. All rights reserved." | ||
} | ||
@@ -67,3 +67,3 @@ ] | ||
/** | ||
* Copyright 2023. All rights reserved. | ||
* Copyright 2024. All rights reserved. | ||
*/ | ||
@@ -89,3 +89,3 @@ module.exports = 42; | ||
/** | ||
* Copyright 2023. All rights reserved. | ||
* Copyright 2024. All rights reserved. | ||
* | ||
@@ -98,2 +98,41 @@ * @fileoverview This file contains a magic number. | ||
**Example 2:** | ||
Using the following configuration, variable values can be injected into the provided header: | ||
```json | ||
{ | ||
"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: | ||
```js | ||
/** | ||
* Copyright 1947 Hardware LLC. All rights reserved. | ||
*/ | ||
module.exports = 42; | ||
``` | ||
And get the resulting header: | ||
```js | ||
/** | ||
* Copyright 2077 Software Incorporated. All rights reserved. | ||
*/ | ||
module.exports = 42; | ||
``` | ||
### Options | ||
@@ -114,2 +153,3 @@ | ||
| 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. | | ||
@@ -126,6 +166,6 @@ ## Future | ||
| Name | Description | 🔧 | | ||
| :------------------------------------------- | :----------------------------------------------------------------- | :-- | | ||
| [header-format](docs/rules/header-format.md) | Verifies the content and format of a file's leading comment block. | 🔧 | | ||
| Name | Description | 🔧 | | ||
| :------------------------------------------- | :----------------------------------------------------------------- | :--- | | ||
| [header-format](docs/rules/header-format.md) | Verifies the content and format of a file's leading comment block. | 🔧 | | ||
<!-- end auto-generated rules list --> |
@@ -139,2 +139,14 @@ /** | ||
}, | ||
{ | ||
options: [ | ||
{ | ||
source: "string", | ||
content: "This is a {template}", | ||
variables: { | ||
template: "header", | ||
}, | ||
}, | ||
], | ||
code: "/**\n * This is a header\n */\nmodule.exports = 42;\n", | ||
}, | ||
], | ||
@@ -264,3 +276,17 @@ | ||
}, | ||
{ | ||
options: [ | ||
{ | ||
source: "string", | ||
content: "This is a {template}", | ||
variables: { | ||
template: "header", | ||
}, | ||
}, | ||
], | ||
code: "/**\n * This is a bad header\n */\nmodule.exports = 42;\n", | ||
errors: [{ messageId: "headerContentMismatch" }], | ||
output: "/**\n * This is a header\n */\nmodule.exports = 42;\n", | ||
}, | ||
], | ||
}); |
48837
8.45%1076
4.26%165
32%