@tony.ganchev/eslint-plugin-header 3.1.8 → 3.1.9
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 +154 -75
- package/lib/rules/header.js +11 -10
- package/package.json +1 -1
package/README.md
CHANGED
|
@@ -1,4 +1,30 @@
|
|
|
1
|
-
|
|
1
|
+
# eslint-plugin-header
|
|
2
|
+
|
|
3
|
+
ESLint plugin / rule to ensure that files begin with a given comment - usually a
|
|
4
|
+
copyright notice.
|
|
5
|
+
|
|
6
|
+
Often you will want to have a copyright notice at the top of every file. This
|
|
7
|
+
ESLint plugin checks that the first comment in every file has the contents
|
|
8
|
+
defined in the rule settings.
|
|
9
|
+
|
|
10
|
+
## Table of Contents
|
|
11
|
+
|
|
12
|
+
1. [Scope and Acknowledgements](#scope-and-acknowledgements)
|
|
13
|
+
2. [Usage](#usage)
|
|
14
|
+
1. [File-based Configuration](#file-based-configuration)
|
|
15
|
+
2. [Inline Configuration](#inline-configuration)
|
|
16
|
+
1. [Header Contents Configuration](#header-contents-configuration)
|
|
17
|
+
2. [Trailing Empty Lines Configuration](#trailing-empty-lines-configuration)
|
|
18
|
+
3. [Line Endings](#line-endings)
|
|
19
|
+
3. [Examples](#examples)
|
|
20
|
+
4. [Versioning](#versioning)
|
|
21
|
+
1. [What is a Feature?](#what-is-a-feature)
|
|
22
|
+
2. [What is Backward-compatibility?](#what-is-backward-compatibility)
|
|
23
|
+
5. [License](#license)
|
|
24
|
+
|
|
25
|
+
## Scope and Acknowledgements
|
|
26
|
+
|
|
27
|
+
This is a fork of <https://github.com/Stuk/eslint-plugin-header>.
|
|
2
28
|
|
|
3
29
|
It addresses the following issus:
|
|
4
30
|
|
|
@@ -13,36 +39,27 @@ It addresses the following issus:
|
|
|
13
39
|
forward. This would come at the expense of plugin compatibility and the
|
|
14
40
|
portability of fixes to the upstream repository.
|
|
15
41
|
|
|
16
|
-
eslint-plugin-header
|
|
17
|
-
====================
|
|
18
|
-
|
|
19
|
-
ESLint plugin to ensure that files begin with given comment.
|
|
20
|
-
|
|
21
|
-
Often you will want to have a copyright notice at the top of every file. This
|
|
22
|
-
ESLint plugin checks that the first comment in every file has the contents
|
|
23
|
-
defined in the rule settings.
|
|
24
|
-
|
|
25
42
|
## Usage
|
|
26
43
|
|
|
27
44
|
This rule takes between 1 and 4 arguments after the rule validation severity.
|
|
28
45
|
|
|
29
46
|
The configuration can take any of the following forms:
|
|
30
47
|
|
|
31
|
-
|
|
32
|
-
|
|
33
|
-
|
|
48
|
+
- File-based Configuration
|
|
49
|
+
- `[<severity>, "<file>"]` - read the header template from a file.
|
|
50
|
+
- `[<severity>, "<file>", {<settings>}]` - read the header template from a
|
|
34
51
|
file with additional settings.
|
|
35
|
-
|
|
36
|
-
|
|
52
|
+
- Inline Configuration
|
|
53
|
+
- `"<severity>", "<comment-type>", <header-contents>` - define the header
|
|
37
54
|
contents inline.
|
|
38
|
-
|
|
55
|
+
- `[<severity>, "<comment-type>", <header-contents>, {<settings>}]` - define
|
|
39
56
|
the header contents inline and pass additional settings.
|
|
40
|
-
|
|
57
|
+
- `[<severity>, "<comment-type>", <header-contents>, <n-empty-lines>]` -
|
|
41
58
|
define the header contents inline and an expected number of empty lines
|
|
42
59
|
after the header.
|
|
43
|
-
|
|
44
|
-
define the header contents inline and an expected number of
|
|
45
|
-
after the header and pass additional settings.
|
|
60
|
+
- `[<severity>, "<comment-type>", <header-contents>, <n-empty-lines>,
|
|
61
|
+
{<settings>}]` - define the header contents inline and an expected number of
|
|
62
|
+
empty lines after the header and pass additional settings.
|
|
46
63
|
|
|
47
64
|
### File-based Configuration
|
|
48
65
|
|
|
@@ -78,6 +95,7 @@ tree then the header file will not be found.
|
|
|
78
95
|
### Inline Configuration
|
|
79
96
|
|
|
80
97
|
The inline configuration expects at least two arguments to be given:
|
|
98
|
+
|
|
81
99
|
- _comment-type_ which is either `"block"` or `"line"` to indicate what style
|
|
82
100
|
of comment should be used.
|
|
83
101
|
- _header-contents_ which defines the lines of the header. It can be either a
|
|
@@ -88,6 +106,7 @@ The inline configuration expects at least two arguments to be given:
|
|
|
88
106
|
#### Header Contents Configuration
|
|
89
107
|
|
|
90
108
|
Suppose we want our header to look like this:
|
|
109
|
+
|
|
91
110
|
```js
|
|
92
111
|
/*
|
|
93
112
|
* Copyright (c) 2015
|
|
@@ -97,10 +116,11 @@ Suppose we want our header to look like this:
|
|
|
97
116
|
```
|
|
98
117
|
|
|
99
118
|
All of the following configurations will match the header:
|
|
100
|
-
|
|
119
|
+
|
|
120
|
+
- **Single string**:
|
|
121
|
+
|
|
101
122
|
```json
|
|
102
123
|
{
|
|
103
|
-
...
|
|
104
124
|
"rules": {
|
|
105
125
|
"header/header": [
|
|
106
126
|
2,
|
|
@@ -110,23 +130,30 @@ All of the following configurations will match the header:
|
|
|
110
130
|
}
|
|
111
131
|
}
|
|
112
132
|
```
|
|
133
|
+
|
|
113
134
|
Note that the above would work for both Windows and POSIX systems even
|
|
114
135
|
though the EOL in the header content was specified as `\n`.
|
|
115
136
|
|
|
116
137
|
Also, notice how we have an empty space before each line. This is because
|
|
117
|
-
the plugin only strips the leading `//` characters from a line comment
|
|
118
|
-
|
|
119
|
-
preserved.
|
|
138
|
+
the plugin only strips the leading `//` characters from a line comment.
|
|
139
|
+
Similarly, for a block comment, only the opening `/*` and closing `*/` will
|
|
140
|
+
be preserved with all new lines and whitespace preserved. Keep this in mind
|
|
141
|
+
as this can lead to poorly configured header matching rules that never
|
|
142
|
+
pass. In a future release error messages would be more detailed and show
|
|
143
|
+
exactly where header validation failed.
|
|
144
|
+
|
|
145
|
+
- **Single regular expression**:
|
|
120
146
|
|
|
121
|
-
* **Single regular expression**:
|
|
122
147
|
```json
|
|
123
148
|
{
|
|
124
|
-
...
|
|
125
149
|
"rules": {
|
|
126
150
|
"header/header": [
|
|
127
151
|
2,
|
|
128
152
|
"block",
|
|
129
|
-
{
|
|
153
|
+
{
|
|
154
|
+
"pattern":
|
|
155
|
+
"\\n \\* Copyright \\(c\\) 2015\\n \\* My Company\\n "
|
|
156
|
+
}
|
|
130
157
|
]
|
|
131
158
|
}
|
|
132
159
|
}
|
|
@@ -136,53 +163,48 @@ All of the following configurations will match the header:
|
|
|
136
163
|
`RegExp` objects, the backslashes need to be present in the string instead
|
|
137
164
|
of disappear as escape characters.
|
|
138
165
|
|
|
139
|
-
|
|
140
|
-
|
|
141
|
-
|
|
142
|
-
|
|
143
|
-
|
|
144
|
-
|
|
145
|
-
|
|
146
|
-
|
|
147
|
-
|
|
148
|
-
|
|
149
|
-
|
|
150
|
-
|
|
151
|
-
|
|
152
|
-
|
|
153
|
-
[
|
|
154
|
-
"",
|
|
155
|
-
" * Copyright (c) 2015",
|
|
156
|
-
" * My Company",
|
|
157
|
-
" "
|
|
166
|
+
- **Array of strings**:
|
|
167
|
+
|
|
168
|
+
```json
|
|
169
|
+
{
|
|
170
|
+
"rules": {
|
|
171
|
+
"header/header": [
|
|
172
|
+
2,
|
|
173
|
+
"block",
|
|
174
|
+
[
|
|
175
|
+
"",
|
|
176
|
+
" * Copyright (c) 2015",
|
|
177
|
+
" * My Company",
|
|
178
|
+
" "
|
|
179
|
+
]
|
|
158
180
|
]
|
|
159
|
-
|
|
160
|
-
|
|
161
|
-
|
|
162
|
-
|
|
163
|
-
|
|
164
|
-
|
|
165
|
-
|
|
166
|
-
|
|
167
|
-
|
|
168
|
-
|
|
169
|
-
|
|
170
|
-
|
|
171
|
-
|
|
172
|
-
|
|
173
|
-
|
|
174
|
-
|
|
175
|
-
|
|
181
|
+
}
|
|
182
|
+
}
|
|
183
|
+
```
|
|
184
|
+
|
|
185
|
+
- **Array of strings and/or patterns**:
|
|
186
|
+
|
|
187
|
+
```json
|
|
188
|
+
{
|
|
189
|
+
"rules": {
|
|
190
|
+
"header/header": [
|
|
191
|
+
2,
|
|
192
|
+
"block",
|
|
193
|
+
[
|
|
194
|
+
"",
|
|
195
|
+
{ "pattern": " \\* Copyright \\(c\\) 2015" },
|
|
196
|
+
" * My Company",
|
|
197
|
+
" "
|
|
198
|
+
]
|
|
176
199
|
]
|
|
177
|
-
|
|
178
|
-
|
|
179
|
-
|
|
180
|
-
```
|
|
200
|
+
}
|
|
201
|
+
}
|
|
202
|
+
```
|
|
181
203
|
|
|
182
204
|
Regular expressions allow for a number of improvements in the maintainability
|
|
183
205
|
of the headers. Given the example above, what is clear is that new sources may
|
|
184
206
|
have been created later than 2015 and a comment with a different year should be
|
|
185
|
-
perfectly valid such as:
|
|
207
|
+
perfectly valid, such as:
|
|
186
208
|
|
|
187
209
|
```js
|
|
188
210
|
/*
|
|
@@ -191,8 +213,11 @@ perfectly valid such as:
|
|
|
191
213
|
*/
|
|
192
214
|
...
|
|
193
215
|
```
|
|
194
|
-
|
|
195
|
-
|
|
216
|
+
|
|
217
|
+
Moreover, suppose your legal department expects that the year of first and last
|
|
218
|
+
change be added except if all changes happen in the same year, we also need to
|
|
219
|
+
support:
|
|
220
|
+
|
|
196
221
|
```js
|
|
197
222
|
/*
|
|
198
223
|
* Copyright 2017-2022
|
|
@@ -200,11 +225,11 @@ except if all changes happen in the same year, we also need to support:
|
|
|
200
225
|
*/
|
|
201
226
|
...
|
|
202
227
|
```
|
|
228
|
+
|
|
203
229
|
We can use a regular expression to support all of these cases for your header:
|
|
204
230
|
|
|
205
231
|
```json
|
|
206
232
|
{
|
|
207
|
-
...
|
|
208
233
|
"rules": {
|
|
209
234
|
"header/header": [
|
|
210
235
|
2,
|
|
@@ -224,11 +249,10 @@ Note on auto-fixes i.e. `eslint --fix`: whenever strings are used to define the
|
|
|
224
249
|
header - counting in file-based configuration - the same strings would be used
|
|
225
250
|
to replace a header comment that did not pass validation. This is not possible
|
|
226
251
|
with regular expressions. For regular expression pattern-objects, a second
|
|
227
|
-
property `template` adds a replacement string.
|
|
252
|
+
property `template` adds a replacement string.
|
|
228
253
|
|
|
229
254
|
```json
|
|
230
255
|
{
|
|
231
|
-
...
|
|
232
256
|
"rules": {
|
|
233
257
|
"header/header": [
|
|
234
258
|
2,
|
|
@@ -244,7 +268,9 @@ property `template` adds a replacement string.
|
|
|
244
268
|
}
|
|
245
269
|
}
|
|
246
270
|
```
|
|
271
|
+
|
|
247
272
|
There are a number of things to consider:
|
|
273
|
+
|
|
248
274
|
- Templates need to be matched by the regular expression pattern or otherwise
|
|
249
275
|
an auto-fixed source would again fail linting. This needs to be validated
|
|
250
276
|
manually today as the plugin does not do it for you.
|
|
@@ -257,6 +283,7 @@ The third argument of the rule configuration which defaults to 1 specifies the
|
|
|
257
283
|
number of newlines that are enforced after the header.
|
|
258
284
|
|
|
259
285
|
Zero newlines:
|
|
286
|
+
|
|
260
287
|
```json
|
|
261
288
|
{
|
|
262
289
|
"plugins": [
|
|
@@ -275,12 +302,14 @@ Zero newlines:
|
|
|
275
302
|
}
|
|
276
303
|
}
|
|
277
304
|
```
|
|
305
|
+
|
|
278
306
|
```js
|
|
279
307
|
/* Copyright now
|
|
280
308
|
My Company */ console.log(1)
|
|
281
309
|
```
|
|
282
310
|
|
|
283
311
|
One newline (default):
|
|
312
|
+
|
|
284
313
|
```json
|
|
285
314
|
{
|
|
286
315
|
"plugins": [
|
|
@@ -299,6 +328,7 @@ One newline (default):
|
|
|
299
328
|
}
|
|
300
329
|
}
|
|
301
330
|
```
|
|
331
|
+
|
|
302
332
|
```js
|
|
303
333
|
/* Copyright now
|
|
304
334
|
My Company */
|
|
@@ -306,6 +336,7 @@ console.log(1)
|
|
|
306
336
|
```
|
|
307
337
|
|
|
308
338
|
Two newlines:
|
|
339
|
+
|
|
309
340
|
```json
|
|
310
341
|
{
|
|
311
342
|
"plugins": [
|
|
@@ -324,6 +355,7 @@ Two newlines:
|
|
|
324
355
|
}
|
|
325
356
|
}
|
|
326
357
|
```
|
|
358
|
+
|
|
327
359
|
```js
|
|
328
360
|
/* Copyright now
|
|
329
361
|
My Company */
|
|
@@ -336,6 +368,7 @@ console.log(1)
|
|
|
336
368
|
The rule works with both Unix/POSIX and Windows line endings. For ESLint
|
|
337
369
|
`--fix`, the rule will use the line ending format of the current operating
|
|
338
370
|
system (via Node's `os` package). This setting can be overwritten as follows:
|
|
371
|
+
|
|
339
372
|
```json
|
|
340
373
|
"rules": {
|
|
341
374
|
"header/header": [
|
|
@@ -351,6 +384,7 @@ system (via Node's `os` package). This setting can be overwritten as follows:
|
|
|
351
384
|
]
|
|
352
385
|
}
|
|
353
386
|
```
|
|
387
|
+
|
|
354
388
|
Possible values are `"unix"` for `\n` and `"windows"` for `\r\n` line endings.
|
|
355
389
|
|
|
356
390
|
## Examples
|
|
@@ -380,7 +414,7 @@ console.log(1)
|
|
|
380
414
|
console.log(1)
|
|
381
415
|
```
|
|
382
416
|
|
|
383
|
-
|
|
417
|
+
With more decoration:
|
|
384
418
|
|
|
385
419
|
```json
|
|
386
420
|
"header/header": [2, "block", [
|
|
@@ -399,6 +433,51 @@ console.log(1)
|
|
|
399
433
|
console.log(1);
|
|
400
434
|
```
|
|
401
435
|
|
|
436
|
+
## Versioning
|
|
437
|
+
|
|
438
|
+
The project follows standard [NPM semantic versioning policy](
|
|
439
|
+
https://docs.npmjs.com/about-semantic-versioning).
|
|
440
|
+
|
|
441
|
+
The following guidelines apply:
|
|
442
|
+
|
|
443
|
+
- **major versions** - new functionality that breaks compatibility.
|
|
444
|
+
- **minor versions** - new features that do not break compatibility. For the
|
|
445
|
+
most part we would aim to continue releasing new versions in the 3.x product
|
|
446
|
+
line and have opt-in flags for changes in behavior of existign features.
|
|
447
|
+
- **revisions** - bugfixes and minor non-feature improvements that do not break
|
|
448
|
+
compatibility. Note that bug-fixes are allowed to break compatibility with
|
|
449
|
+
previous version if the older version regressed previous expected behavior.
|
|
450
|
+
|
|
451
|
+
Two concepts are important when going over the above guidelines and we will go
|
|
452
|
+
over them in the next sections.
|
|
453
|
+
|
|
454
|
+
### What is a Feature?
|
|
455
|
+
|
|
456
|
+
We keep the distinction between a feature and a non-feature improvement / bug
|
|
457
|
+
fix as simple as possible:
|
|
458
|
+
|
|
459
|
+
- If configuration changes, it's a **feature**.
|
|
460
|
+
- If it doesn't, then you have two cases:
|
|
461
|
+
- If it changes behavior back to what is expected, it is a bug.
|
|
462
|
+
- If it changes the expected behavior, it is an improvement.
|
|
463
|
+
|
|
464
|
+
### What is Backward-compatibility?
|
|
465
|
+
|
|
466
|
+
Backward compatibility in the context of this plugin relates to how the plugin
|
|
467
|
+
consistently passes or fails one and the same code in between upgrades to newer
|
|
468
|
+
backward-compatible versions. This guarantees that plugin updates can be done
|
|
469
|
+
without breaking CI/CD pipeline linting.
|
|
470
|
+
|
|
471
|
+
Backward-compatibility does not cover the following functional aspects:
|
|
472
|
+
|
|
473
|
+
- Rule violation messages are not kept stable between backward-compatible
|
|
474
|
+
versions. This allows us to improve error reporting in addition to bug fixes.
|
|
475
|
+
- Auto-fix behavior is not stable between backward-compatible versions. As auto-
|
|
476
|
+
fixes are not part of CI/CD processes results of them may vary.
|
|
477
|
+
- Bugs to released functionality. Bugs are considered regression to expected
|
|
478
|
+
functionality regardless of whether they went into a release or not. We fix
|
|
479
|
+
bugs without maintaining bug-compatibility.
|
|
480
|
+
|
|
402
481
|
## License
|
|
403
482
|
|
|
404
483
|
MIT
|
package/lib/rules/header.js
CHANGED
|
@@ -454,18 +454,19 @@ module.exports = {
|
|
|
454
454
|
message: "incorrect header",
|
|
455
455
|
fix: canFix ? genReplaceFixer(commentType, context, leadingComments, fixLines, eol, numNewlines) : null
|
|
456
456
|
});
|
|
457
|
-
}
|
|
458
|
-
return;
|
|
459
|
-
}
|
|
460
|
-
for (var i = 0; i < headerLines.length; i++) {
|
|
461
|
-
if (!match(leadingComments[i].value, headerLines[i])) {
|
|
462
|
-
context.report({
|
|
463
|
-
loc: node.loc,
|
|
464
|
-
message: "incorrect header",
|
|
465
|
-
fix: canFix ? genReplaceFixer(commentType, context, leadingComments, fixLines, eol, numNewlines) : null
|
|
466
|
-
});
|
|
467
457
|
return;
|
|
468
458
|
}
|
|
459
|
+
} else {
|
|
460
|
+
for (var i = 0; i < headerLines.length; i++) {
|
|
461
|
+
if (!match(leadingComments[i].value, headerLines[i])) {
|
|
462
|
+
context.report({
|
|
463
|
+
loc: node.loc,
|
|
464
|
+
message: "incorrect header",
|
|
465
|
+
fix: canFix ? genReplaceFixer(commentType, context, leadingComments, fixLines, eol, numNewlines) : null
|
|
466
|
+
});
|
|
467
|
+
return;
|
|
468
|
+
}
|
|
469
|
+
}
|
|
469
470
|
}
|
|
470
471
|
|
|
471
472
|
var postLineHeader = context.getSourceCode().text.substr(leadingComments[headerLines.length - 1].range[1], numNewlines * 2);
|
package/package.json
CHANGED