@tony.ganchev/eslint-plugin-header 3.1.1

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/.eslintrc.yml ADDED
@@ -0,0 +1,94 @@
1
+ root: true
2
+
3
+ env:
4
+ node: true
5
+
6
+ extends:
7
+ "eslint:recommended"
8
+
9
+ rules:
10
+ indent: [2, 4, {SwitchCase: 1}]
11
+ brace-style: [2, "1tbs"]
12
+ camelcase: [2, { properties: "never" }]
13
+ callback-return: [2, ["cb", "callback", "next"]]
14
+ comma-spacing: 2
15
+ comma-style: [2, "last"]
16
+ consistent-return: 2
17
+ curly: [2, "all"]
18
+ default-case: 2
19
+ dot-notation: [2, { allowKeywords: true }]
20
+ eol-last: 2
21
+ eqeqeq: 2
22
+ func-style: [2, "declaration"]
23
+ guard-for-in: 2
24
+ key-spacing: [2, { beforeColon: false, afterColon: true }]
25
+ new-cap: 2
26
+ new-parens: 2
27
+ no-alert: 2
28
+ no-array-constructor: 2
29
+ no-caller: 2
30
+ no-console: 0
31
+ no-delete-var: 2
32
+ no-eval: 2
33
+ no-extend-native: 2
34
+ no-extra-bind: 2
35
+ no-fallthrough: 2
36
+ no-floating-decimal: 2
37
+ no-implied-eval: 2
38
+ no-invalid-this: 2
39
+ no-iterator: 2
40
+ no-label-var: 2
41
+ no-labels: 2
42
+ no-lone-blocks: 2
43
+ no-loop-func: 2
44
+ no-mixed-spaces-and-tabs: [2, false]
45
+ no-multi-spaces: 2
46
+ no-multi-str: 2
47
+ no-native-reassign: 2
48
+ no-nested-ternary: 2
49
+ no-new: 2
50
+ no-new-func: 2
51
+ no-new-object: 2
52
+ no-new-wrappers: 2
53
+ no-octal: 2
54
+ no-octal-escape: 2
55
+ no-process-exit: 2
56
+ no-proto: 2
57
+ no-redeclare: 2
58
+ no-return-assign: 2
59
+ no-script-url: 2
60
+ no-sequences: 2
61
+ no-shadow: 2
62
+ no-shadow-restricted-names: 2
63
+ no-spaced-func: 2
64
+ no-trailing-spaces: 2
65
+ no-undef: 2
66
+ no-undef-init: 2
67
+ no-undefined: 2
68
+ no-underscore-dangle: 2
69
+ no-unused-expressions: 2
70
+ no-unused-vars: [2, {vars: "all", args: "after-used"}]
71
+ no-use-before-define: 2
72
+ no-with: 2
73
+ quotes: [2, "double"]
74
+ radix: 2
75
+ semi: 2
76
+ semi-spacing: [2, {before: false, after: true}]
77
+ keyword-spacing: [2, {"after": true }]
78
+ space-before-blocks: 2
79
+ space-before-function-paren: [2, "never"]
80
+ space-infix-ops: 2
81
+ space-unary-ops: [2, {words: true, nonwords: false}]
82
+ spaced-comment: [2, "always", { exceptions: ["-"]}]
83
+ strict: [2, "global"]
84
+ valid-jsdoc: [2, { prefer: { "return": "returns"}}]
85
+ wrap-iife: 2
86
+ yoda: [2, "never"]
87
+
88
+ # Previously on by default in node environment
89
+ no-catch-shadow: 0
90
+ no-mixed-requires: 2
91
+ no-new-require: 2
92
+ no-path-concat: 2
93
+ global-strict: [0, "always"]
94
+ handle-callback-err: [2, "err"]
@@ -0,0 +1,22 @@
1
+ name: Test
2
+
3
+ on:
4
+ push:
5
+ branches: [ main ]
6
+ pull_request:
7
+ branches: [ main ]
8
+
9
+ jobs:
10
+ build:
11
+
12
+ runs-on: ubuntu-latest
13
+
14
+ steps:
15
+ - uses: actions/checkout@v2
16
+ - name: Use Node.js
17
+ uses: actions/setup-node@v1
18
+ with:
19
+ node-version: '14.x'
20
+ - name: Install dependencies
21
+ run: npm ci
22
+ - run: npm test
package/CHANGELOG.md ADDED
@@ -0,0 +1,39 @@
1
+ # 3.1.1
2
+
3
+ * Fix detecting header below shebang comment with Windows EOL (#30)
4
+
5
+ # 3.1.0
6
+
7
+ * Update to eslint 7.7.0
8
+ * Add a third option to specify number of linebreaks after the header. (#29)
9
+
10
+ # 3.0.0
11
+
12
+ * Allow regexp in multiline arrays (#23)
13
+ * Add `template` option for regexps, for `eslint --fix` (#23)
14
+ * Update eslint to v5.12.0 (#19)
15
+
16
+ # 2.0.0
17
+
18
+ * Use the OS's line endings (`\n` on *nix, `\r\n` on Windows) when parsing and fixing comments. This can be configured with the `lineEndings` option. Major version bump as this could be a breaking change for projects.
19
+
20
+ # 1.2.0
21
+
22
+ * Add auto fix functionality (eslint `--fix` option) (#12)
23
+
24
+ # 1.1.0
25
+
26
+ * Ignore shebangs above header comments to support ESLint 4+ (#11)
27
+
28
+ # 1.0.0
29
+
30
+ * Allow RegExp patterns in addition to strings (#2, #4)
31
+ * Fix line comment length mismatch issue (#3)
32
+
33
+ # 0.1.0
34
+
35
+ * Add config option to read header from file
36
+
37
+ # 0.0.2
38
+
39
+ * Initial release
package/LICENSE.md ADDED
@@ -0,0 +1,7 @@
1
+ Copyright (c) 2015-present Stuart Knightley and contributors
2
+
3
+ Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the “Software”), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:
4
+
5
+ The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.
6
+
7
+ THE SOFTWARE IS PROVIDED “AS IS”, WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
package/README.md ADDED
@@ -0,0 +1,207 @@
1
+ This is a fork of https://github.com/Stuk/eslint-plugin-header intended to make the plugin work with ESLint v9
2
+
3
+ eslint-plugin-header
4
+ ====================
5
+
6
+ ESLint plugin to ensure that files begin with given comment.
7
+
8
+ Often you will want to have a copyright notice at the top of every file. This ESLint plugin checks that the first comment in every file has the contents defined in the rule settings.
9
+
10
+ ## Usage
11
+
12
+ This rule takes 1, 2 or 3 arguments with an optional settings object.
13
+
14
+ ### 1 argument
15
+
16
+ In the 1 argument form the argument is the filename of a file that contains the comment(s) that should appear at the top of every file:
17
+
18
+ ```json
19
+ {
20
+ "plugins": [
21
+ "header"
22
+ ],
23
+ "rules": {
24
+ "header/header": [2, "config/header.js"]
25
+ }
26
+ }
27
+ ```
28
+
29
+ config/header.js:
30
+
31
+ ```js
32
+ // Copyright 2015
33
+ // My company
34
+ ```
35
+
36
+ Due to limitations in eslint plugins, the file is read relative to the working directory that eslint is executed in. If you run eslint from elsewhere in your tree then the header file will not be found.
37
+
38
+ ### 2 arguments
39
+
40
+ In the 2 argument form the first must be either `"block"` or `"line"` to indicate what style of comment should be used. The second is either a string (including newlines) of the comment, or an array of each line of the comment.
41
+
42
+ ```json
43
+ {
44
+ "plugins": [
45
+ "header"
46
+ ],
47
+ "rules": {
48
+ "header/header": [2, "block", "Copyright 2015\nMy Company"]
49
+ }
50
+ }
51
+ ```
52
+
53
+ ### 3 arguments
54
+
55
+ The optional third argument which defaults to 1 specifies the number of newlines that are enforced after the header.
56
+
57
+ Zero newlines:
58
+ ```json
59
+ {
60
+ "plugins": [
61
+ "header"
62
+ ],
63
+ "rules": {
64
+ "header/header": [2, "block", [" Copyright now","My Company "], 0]
65
+ }
66
+ }
67
+ ```
68
+ ```js
69
+ /* Copyright now
70
+ My Company */ console.log(1)
71
+ ```
72
+
73
+ One newline (default)
74
+ ```json
75
+ {
76
+ "plugins": [
77
+ "header"
78
+ ],
79
+ "rules": {
80
+ "header/header": [2, "block", [" Copyright now","My Company "], 1]
81
+ }
82
+ }
83
+ ```
84
+ ```js
85
+ /* Copyright now
86
+ My Company */
87
+ console.log(1)
88
+ ```
89
+
90
+ two newlines
91
+ ```json
92
+ {
93
+ "plugins": [
94
+ "header"
95
+ ],
96
+ "rules": {
97
+ "header/header": [2, "block", [" Copyright now","My Company "], 2]
98
+ }
99
+ }
100
+ ```
101
+ ```js
102
+ /* Copyright now
103
+ My Company */
104
+
105
+ console.log(1)
106
+ ```
107
+
108
+ #### Regular expressions
109
+
110
+ Instead of a string to be checked for exact matching you can also supply a regular expression. Be aware that you have to escape backslashes:
111
+
112
+ ```json
113
+ {
114
+ "plugins": [
115
+ "header"
116
+ ],
117
+ "rules": {
118
+ "header/header": [2, "block", [
119
+ {"pattern": " Copyright \\d{4}"},
120
+ "My Company"
121
+ ]]
122
+ }
123
+ }
124
+ ```
125
+
126
+ This would match:
127
+
128
+ ```js
129
+ /* Copyright 2808
130
+ My Company*/
131
+ ```
132
+
133
+ When you use a regular expression `pattern`, you can also provide a `template` property, to provide the comment value when using `eslint --fix`:
134
+
135
+ ```json
136
+ {
137
+ "plugins": [
138
+ "header"
139
+ ],
140
+ "rules": {
141
+ "header/header": [2, "block", [
142
+ {"pattern": " Copyright \\d{4}", "template": " Copyright 2019"},
143
+ "My Company"
144
+ ]]
145
+ }
146
+ }
147
+ ```
148
+
149
+ ### Line Endings
150
+
151
+ The rule works with both unix and windows line endings. For ESLint `--fix`, the rule will use the line ending format of the current operating system (via the node `os` package). This setting can be overwritten as follows:
152
+ ```json
153
+ "rules": {
154
+ "header/header": [2, "block", ["Copyright 2018", "My Company"], {"lineEndings": "windows"}]
155
+ }
156
+ ```
157
+ Possible values are `unix` for `\n` and `windows` for `\r\n` line endings.
158
+
159
+ ## Examples
160
+
161
+ The following examples are all valid.
162
+
163
+ `"block", "Copyright 2015, My Company"`:
164
+
165
+ ```js
166
+ /*Copyright 2015, My Company*/
167
+ console.log(1);
168
+ ```
169
+
170
+ `"line", ["Copyright 2015", "My Company"]]`:
171
+
172
+ ```js
173
+ //Copyright 2015
174
+ //My Company
175
+ console.log(1)
176
+ ```
177
+
178
+ `"line", [{pattern: "^Copyright \\d{4}$"}, {pattern: "^My Company$"}]]`:
179
+
180
+ ```js
181
+ //Copyright 2017
182
+ //My Company
183
+ console.log(1)
184
+ ```
185
+
186
+ ### With more decoration
187
+
188
+ ```json
189
+ "header/header": [2, "block", [
190
+ "************************",
191
+ " * Copyright 2015",
192
+ " * My Company",
193
+ " ************************"
194
+ ]
195
+ ```
196
+
197
+ ```js
198
+ /*************************
199
+ * Copyright 2015
200
+ * My Company
201
+ *************************/
202
+ console.log(1);
203
+ ```
204
+
205
+ ## License
206
+
207
+ MIT
package/index.js ADDED
@@ -0,0 +1,10 @@
1
+ "use strict";
2
+
3
+ module.exports = {
4
+ rules: {
5
+ "header": require("./lib/rules/header")
6
+ },
7
+ rulesConfig: {
8
+ "header": 0
9
+ }
10
+ };
@@ -0,0 +1,24 @@
1
+ "use strict";
2
+
3
+ // This is a really simple and dumb parser, that looks just for a
4
+ // single kind of comment. It won't detect multiple block comments.
5
+
6
+ module.exports = function commentParser(text) {
7
+ text = text.trim();
8
+
9
+ if (text.substr(0, 2) === "//") {
10
+ return [
11
+ "line",
12
+ text.split(/\r?\n/).map(function(line) {
13
+ return line.substr(2);
14
+ })
15
+ ];
16
+ } else if (
17
+ text.substr(0, 2) === "/*" &&
18
+ text.substr(-2) === "*/"
19
+ ) {
20
+ return ["block", text.substring(2, text.length - 2)];
21
+ } else {
22
+ throw new Error("Could not parse comment file: the file must contain either just line comments (//) or a single block comment (/* ... */)");
23
+ }
24
+ };
@@ -0,0 +1,265 @@
1
+ "use strict";
2
+
3
+ var fs = require("fs");
4
+ var commentParser = require("../comment-parser");
5
+ var os = require("os");
6
+
7
+ function isPattern(object) {
8
+ return typeof object === "object" && Object.prototype.hasOwnProperty.call(object, "pattern");
9
+ }
10
+
11
+ function match(actual, expected) {
12
+ if (expected.test) {
13
+ return expected.test(actual);
14
+ } else {
15
+ return expected === actual;
16
+ }
17
+ }
18
+
19
+ function excludeShebangs(comments) {
20
+ return comments.filter(function(comment) {
21
+ return comment.type !== "Shebang";
22
+ });
23
+ }
24
+
25
+ // Returns either the first block comment or the first set of line comments that
26
+ // are ONLY separated by a single newline. Note that this does not actually
27
+ // check if they are at the start of the file since that is already checked by
28
+ // hasHeader().
29
+ function getLeadingComments(context, node) {
30
+ var all = excludeShebangs(context.getSourceCode().getAllComments(node.body.length ? node.body[0] : node));
31
+ if (all[0].type.toLowerCase() === "block") {
32
+ return [all[0]];
33
+ }
34
+ for (var i = 1; i < all.length; ++i) {
35
+ var txt = context.getSourceCode().getText().slice(all[i - 1].range[1], all[i].range[0]);
36
+ if (!txt.match(/^(\r\n|\r|\n)$/)) {
37
+ break;
38
+ }
39
+ }
40
+ return all.slice(0, i);
41
+ }
42
+
43
+ function genCommentBody(commentType, textArray, eol, numNewlines) {
44
+ var eols = eol.repeat(numNewlines);
45
+ if (commentType === "block") {
46
+ return "/*" + textArray.join(eol) + "*/" + eols;
47
+ } else {
48
+ return "//" + textArray.join(eol + "//") + eols;
49
+ }
50
+ }
51
+
52
+ function genCommentsRange(context, comments, eol) {
53
+ var start = comments[0].range[0];
54
+ var end = comments.slice(-1)[0].range[1];
55
+ if (context.getSourceCode().text[end] === eol) {
56
+ end += eol.length;
57
+ }
58
+ return [start, end];
59
+ }
60
+
61
+ function genPrependFixer(commentType, node, headerLines, eol, numNewlines) {
62
+ return function(fixer) {
63
+ return fixer.insertTextBefore(
64
+ node,
65
+ genCommentBody(commentType, headerLines, eol, numNewlines)
66
+ );
67
+ };
68
+ }
69
+
70
+ function genReplaceFixer(commentType, context, leadingComments, headerLines, eol, numNewlines) {
71
+ return function(fixer) {
72
+ return fixer.replaceTextRange(
73
+ genCommentsRange(context, leadingComments, eol),
74
+ genCommentBody(commentType, headerLines, eol, numNewlines)
75
+ );
76
+ };
77
+ }
78
+
79
+ function findSettings(options) {
80
+ var lastOption = options.length > 0 ? options[options.length - 1] : null;
81
+ if (typeof lastOption === "object" && !Array.isArray(lastOption) && lastOption !== null
82
+ && !Object.prototype.hasOwnProperty.call(lastOption, "pattern")) {
83
+ return lastOption;
84
+ }
85
+ return null;
86
+ }
87
+
88
+ function getEOL(options) {
89
+ var settings = findSettings(options);
90
+ if (settings && settings.lineEndings === "unix") {
91
+ return "\n";
92
+ }
93
+ if (settings && settings.lineEndings === "windows") {
94
+ return "\r\n";
95
+ }
96
+ return os.EOL;
97
+ }
98
+
99
+ function hasHeader(src) {
100
+ if (src.substr(0, 2) === "#!") {
101
+ var m = src.match(/(\r\n|\r|\n)/);
102
+ if (m) {
103
+ src = src.slice(m.index + m[0].length);
104
+ }
105
+ }
106
+ return src.substr(0, 2) === "/*" || src.substr(0, 2) === "//";
107
+ }
108
+
109
+ function matchesLineEndings(src, num) {
110
+ for (var j = 0; j < num; ++j) {
111
+ var m = src.match(/^(\r\n|\r|\n)/);
112
+ if (m) {
113
+ src = src.slice(m.index + m[0].length);
114
+ } else {
115
+ return false;
116
+ }
117
+ }
118
+ return true;
119
+ }
120
+
121
+ module.exports = {
122
+ meta: {
123
+ type: "layout",
124
+ fixable: "whitespace",
125
+ schema: false
126
+ },
127
+ create: function(context) {
128
+ var options = context.options;
129
+ var numNewlines = options.length > 2 ? options[2] : 1;
130
+ var eol = getEOL(options);
131
+
132
+ // If just one option then read comment from file
133
+ if (options.length === 1 || (options.length === 2 && findSettings(options))) {
134
+ var text = fs.readFileSync(context.options[0], "utf8");
135
+ options = commentParser(text);
136
+ }
137
+
138
+ var commentType = options[0].toLowerCase();
139
+ var headerLines, fixLines = [];
140
+ // If any of the lines are regular expressions, then we can't
141
+ // automatically fix them. We set this to true below once we
142
+ // ensure none of the lines are of type RegExp
143
+ var canFix = false;
144
+ if (Array.isArray(options[1])) {
145
+ canFix = true;
146
+ headerLines = options[1].map(function(line) {
147
+ var isRegex = isPattern(line);
148
+ // Can only fix regex option if a template is also provided
149
+ if (isRegex && !line.template) {
150
+ canFix = false;
151
+ }
152
+ fixLines.push(line.template || line);
153
+ return isRegex ? new RegExp(line.pattern) : line;
154
+ });
155
+ } else if (isPattern(options[1])) {
156
+ var line = options[1];
157
+ headerLines = [new RegExp(line.pattern)];
158
+ fixLines.push(line.template || line);
159
+ // Same as above for regex and template
160
+ canFix = !!line.template;
161
+ } else {
162
+ canFix = true;
163
+ headerLines = options[1].split(/\r?\n/);
164
+ fixLines = headerLines;
165
+ }
166
+
167
+ return {
168
+ Program: function(node) {
169
+ if (!hasHeader(context.getSourceCode().getText())) {
170
+ context.report({
171
+ loc: node.loc,
172
+ message: "missing header",
173
+ fix: genPrependFixer(commentType, node, fixLines, eol, numNewlines)
174
+ });
175
+ } else {
176
+ var leadingComments = getLeadingComments(context, node);
177
+
178
+ if (!leadingComments.length) {
179
+ context.report({
180
+ loc: node.loc,
181
+ message: "missing header",
182
+ fix: canFix ? genPrependFixer(commentType, node, fixLines, eol, numNewlines) : null
183
+ });
184
+ } else if (leadingComments[0].type.toLowerCase() !== commentType) {
185
+ context.report({
186
+ loc: node.loc,
187
+ message: "header should be a {{commentType}} comment",
188
+ data: {
189
+ commentType: commentType
190
+ },
191
+ fix: canFix ? genReplaceFixer(commentType, context, leadingComments, fixLines, eol, numNewlines) : null
192
+ });
193
+ } else {
194
+ if (commentType === "line") {
195
+ if (leadingComments.length < headerLines.length) {
196
+ context.report({
197
+ loc: node.loc,
198
+ message: "incorrect header",
199
+ fix: canFix ? genReplaceFixer(commentType, context, leadingComments, fixLines, eol, numNewlines) : null
200
+ });
201
+ return;
202
+ }
203
+ for (var i = 0; i < headerLines.length; i++) {
204
+ if (!match(leadingComments[i].value, headerLines[i])) {
205
+ context.report({
206
+ loc: node.loc,
207
+ message: "incorrect header",
208
+ fix: canFix ? genReplaceFixer(commentType, context, leadingComments, fixLines, eol, numNewlines) : null
209
+ });
210
+ return;
211
+ }
212
+ }
213
+
214
+ var postLineHeader = context.getSourceCode().text.substr(leadingComments[headerLines.length - 1].range[1], numNewlines * 2);
215
+ if (!matchesLineEndings(postLineHeader, numNewlines)) {
216
+ context.report({
217
+ loc: node.loc,
218
+ message: "no newline after header",
219
+ fix: canFix ? genReplaceFixer(commentType, context, leadingComments, fixLines, eol, numNewlines) : null
220
+ });
221
+ }
222
+
223
+ } else {
224
+ // if block comment pattern has more than 1 line, we also split the comment
225
+ var leadingLines = [leadingComments[0].value];
226
+ if (headerLines.length > 1) {
227
+ leadingLines = leadingComments[0].value.split(/\r?\n/);
228
+ }
229
+
230
+ var hasError = false;
231
+ if (leadingLines.length > headerLines.length) {
232
+ hasError = true;
233
+ }
234
+ for (i = 0; !hasError && i < headerLines.length; i++) {
235
+ if (!match(leadingLines[i], headerLines[i])) {
236
+ hasError = true;
237
+ }
238
+ }
239
+
240
+ if (hasError) {
241
+ if (canFix && headerLines.length > 1) {
242
+ fixLines = [fixLines.join(eol)];
243
+ }
244
+ context.report({
245
+ loc: node.loc,
246
+ message: "incorrect header",
247
+ fix: canFix ? genReplaceFixer(commentType, context, leadingComments, fixLines, eol, numNewlines) : null
248
+ });
249
+ } else {
250
+ var postBlockHeader = context.getSourceCode().text.substr(leadingComments[0].range[1], numNewlines * 2);
251
+ if (!matchesLineEndings(postBlockHeader, numNewlines)) {
252
+ context.report({
253
+ loc: node.loc,
254
+ message: "no newline after header",
255
+ fix: canFix ? genReplaceFixer(commentType, context, leadingComments, fixLines, eol, numNewlines) : null
256
+ });
257
+ }
258
+ }
259
+ }
260
+ }
261
+ }
262
+ }
263
+ };
264
+ }
265
+ };
package/package.json ADDED
@@ -0,0 +1,31 @@
1
+ {
2
+ "name": "@tony.ganchev/eslint-plugin-header",
3
+ "version": "3.1.1",
4
+ "description": "ESLint plugin to ensure that files begin with given comment",
5
+ "main": "index.js",
6
+ "scripts": {
7
+ "test": "npm run lint && npm run unit",
8
+ "unit": "mocha tests/lib/*.js tests/lib/**/*.js",
9
+ "lint": "eslint ."
10
+ },
11
+ "devDependencies": {
12
+ "eslint": "^7.7.0",
13
+ "mocha": "^8.1.1"
14
+ },
15
+ "peerDependencies": {
16
+ "eslint": ">=7.7.0"
17
+ },
18
+ "keywords": [
19
+ "eslint",
20
+ "eslintplugin"
21
+ ],
22
+ "repository": {
23
+ "type": "git",
24
+ "url": "https://github.com/tonyganchev/eslint-plugin-header.git"
25
+ },
26
+ "author": "Stuart Knightley",
27
+ "license": "MIT",
28
+ "contributors": [
29
+ "Tony Ganchev"
30
+ ]
31
+ }
@@ -0,0 +1,23 @@
1
+ /* eslint-env mocha */
2
+ "use strict";
3
+
4
+ var assert = require("assert");
5
+ var commentParser = require("../../lib/comment-parser");
6
+
7
+ describe("comment parser", function() {
8
+ it("parses block comments", function() {
9
+ var result = commentParser("/* pass1\n pass2 */ ");
10
+ assert.deepEqual(result, ["block", " pass1\n pass2 "]);
11
+ });
12
+
13
+ it("throws an error when a block comment isn't ended", function() {
14
+ assert.throws(function() {
15
+ commentParser("/* fail");
16
+ });
17
+ });
18
+
19
+ it("parses line comments", function() {
20
+ var result = commentParser("// pass1\n// pass2\n ");
21
+ assert.deepEqual(result, ["line", [" pass1", " pass2"]]);
22
+ });
23
+ });
@@ -0,0 +1,285 @@
1
+ "use strict";
2
+
3
+ var rule = require("../../../lib/rules/header");
4
+ var RuleTester = require("eslint").RuleTester;
5
+
6
+ var ruleTester = new RuleTester();
7
+ ruleTester.run("header", rule, {
8
+ valid: [
9
+ {
10
+ code: "/*Copyright 2015, My Company*/\nconsole.log(1);",
11
+ options: ["block", "Copyright 2015, My Company"]
12
+ },
13
+ {
14
+ code: "//Copyright 2015, My Company\nconsole.log(1);",
15
+ options: ["line", "Copyright 2015, My Company"]
16
+ },
17
+ {
18
+ code: "/*Copyright 2015, My Company*/",
19
+ options: ["block", "Copyright 2015, My Company", 0]
20
+ },
21
+ {
22
+ code: "//Copyright 2015\n//My Company\nconsole.log(1)",
23
+ options: ["line", "Copyright 2015\nMy Company"]
24
+ },
25
+ {
26
+ code: "//Copyright 2015\n//My Company\nconsole.log(1)",
27
+ options: ["line", ["Copyright 2015", "My Company"]]
28
+ },
29
+ {
30
+ code: "/*Copyright 2015\nMy Company*/\nconsole.log(1)",
31
+ options: ["block", ["Copyright 2015", "My Company"]]
32
+ },
33
+ {
34
+ code: "/*************************\n * Copyright 2015\n * My Company\n *************************/\nconsole.log(1)",
35
+ options: ["block", [
36
+ "************************",
37
+ " * Copyright 2015",
38
+ " * My Company",
39
+ " ************************"
40
+ ]]
41
+ },
42
+ {
43
+ code: "/*\nCopyright 2015\nMy Company\n*/\nconsole.log(1)",
44
+ options: ["tests/support/block.js"]
45
+ },
46
+ {
47
+ code: "// Copyright 2015\n// My Company\nconsole.log(1)",
48
+ options: ["tests/support/line.js"]
49
+ },
50
+ {
51
+ code: "//Copyright 2015\n//My Company\n/* DOCS */",
52
+ options: ["line", "Copyright 2015\nMy Company"]
53
+ },
54
+ {
55
+ code: "// Copyright 2017",
56
+ options: ["line", {pattern: "^ Copyright \\d+$"}, 0]
57
+ },
58
+ {
59
+ code: "// Copyright 2017\n// Author: abc@example.com",
60
+ options: ["line", [{pattern: "^ Copyright \\d+$"}, {pattern: "^ Author: \\w+@\\w+\\.\\w+$"}], 0]
61
+ },
62
+ {
63
+ code: "/* Copyright 2017\n Author: abc@example.com */",
64
+ options: ["block", {pattern: "^ Copyright \\d{4}\\n Author: \\w+@\\w+\\.\\w+ $"}, 0]
65
+ },
66
+ {
67
+ code: "#!/usr/bin/env node\n/**\n * Copyright\n */",
68
+ options: ["block", [
69
+ "*",
70
+ " * Copyright",
71
+ " "
72
+ ], 0]
73
+ },
74
+ {
75
+ code: "// Copyright 2015\r\n// My Company\r\nconsole.log(1)",
76
+ options: ["tests/support/line.js"]
77
+ },
78
+ {
79
+ code: "//Copyright 2018\r\n//My Company\r\n/* DOCS */",
80
+ options: ["line", ["Copyright 2018", "My Company"]]
81
+ },
82
+ {
83
+ code: "/*Copyright 2018\r\nMy Company*/\r\nconsole.log(1)",
84
+ options: ["block", ["Copyright 2018", "My Company"], {"lineEndings": "windows"}]
85
+ },
86
+ {
87
+ code: "/*Copyright 2018\nMy Company*/\nconsole.log(1)",
88
+ options: ["block", ["Copyright 2018", "My Company"], {"lineEndings": "unix"}]
89
+ },
90
+ {
91
+ code: "/*************************\n * Copyright 2015\n * My Company\n *************************/\nconsole.log(1)",
92
+ options: ["block", [
93
+ "************************",
94
+ { pattern: " \\* Copyright \\d{4}" },
95
+ " * My Company",
96
+ " ************************"
97
+ ]]
98
+ },
99
+ {
100
+ code: "/*Copyright 2020, My Company*/\nconsole.log(1);",
101
+ options: ["block", "Copyright 2020, My Company", 1],
102
+ },
103
+ {
104
+ code: "/*Copyright 2020, My Company*/\n\nconsole.log(1);",
105
+ options: ["block", "Copyright 2020, My Company", 2],
106
+ },
107
+ {
108
+ code: "/*Copyright 2020, My Company*/\n\n// Log number one\nconsole.log(1);",
109
+ options: ["block", "Copyright 2020, My Company", 2],
110
+ },
111
+ {
112
+ code: "/*Copyright 2020, My Company*/\n\n/*Log number one*/\nconsole.log(1);",
113
+ options: ["block", "Copyright 2020, My Company", 2],
114
+ },
115
+ {
116
+ code: "/**\n * Copyright 2020\n * My Company\n **/\n\n/*Log number one*/\nconsole.log(1);",
117
+ options: ["block", "*\n * Copyright 2020\n * My Company\n *", 2],
118
+ },
119
+ {
120
+ code: "#!/usr/bin/env node\r\n/**\r\n * Copyright\r\n */",
121
+ options: ["block", [
122
+ "*",
123
+ " * Copyright",
124
+ " "
125
+ ], 0]
126
+ }
127
+ ],
128
+ invalid: [
129
+ {
130
+ code: "console.log(1);",
131
+ options: ["block", "Copyright 2015, My Company"],
132
+ errors: [
133
+ {message: "missing header"}
134
+ ],
135
+ output: "/*Copyright 2015, My Company*/\nconsole.log(1);"
136
+ },
137
+ {
138
+ code: "//Copyright 2014, My Company\nconsole.log(1);",
139
+ options: ["block", "Copyright 2015, My Company"],
140
+ errors: [
141
+ {message: "header should be a block comment"}
142
+ ],
143
+ output: "/*Copyright 2015, My Company*/\nconsole.log(1);"
144
+ },
145
+ {
146
+ code: "/*Copyright 2014, My Company*/\nconsole.log(1);",
147
+ options: ["line", "Copyright 2015, My Company"],
148
+ errors: [
149
+ {message: "header should be a line comment"}
150
+ ],
151
+ output: "//Copyright 2015, My Company\nconsole.log(1);"
152
+ },
153
+ {
154
+ code: "/*Copyright 2014, My Company*/\nconsole.log(1);",
155
+ options: ["block", "Copyright 2015, My Company"],
156
+ errors: [
157
+ {message: "incorrect header"}
158
+ ],
159
+ output: "/*Copyright 2015, My Company*/\nconsole.log(1);"
160
+ },
161
+ {
162
+ // Test extra line in comment
163
+ code: "/*Copyright 2015\nMy Company\nExtra*/\nconsole.log(1);",
164
+ options: ["block", ["Copyright 2015", "My Company"]],
165
+ errors: [
166
+ {message: "incorrect header"}
167
+ ],
168
+ output: "/*Copyright 2015\nMy Company*/\nconsole.log(1);"
169
+ },
170
+ {
171
+ code: "/*Copyright 2015\n*/\nconsole.log(1);",
172
+ options: ["block", ["Copyright 2015", "My Company"]],
173
+ errors: [
174
+ {message: "incorrect header"}
175
+ ],
176
+ output: "/*Copyright 2015\nMy Company*/\nconsole.log(1);"
177
+ },
178
+ {
179
+ code: "//Copyright 2014\n//My Company\nconsole.log(1)",
180
+ options: ["line", "Copyright 2015\nMy Company"],
181
+ errors: [
182
+ {message: "incorrect header"}
183
+ ],
184
+ output: "//Copyright 2015\n//My Company\nconsole.log(1)"
185
+ },
186
+ {
187
+ code: "//Copyright 2015",
188
+ options: ["line", "Copyright 2015\nMy Company"],
189
+ errors: [
190
+ {message: "incorrect header"}
191
+ ],
192
+ output: "//Copyright 2015\n//My Company\n"
193
+ },
194
+ {
195
+ code: "// Copyright 2017 trailing",
196
+ options: ["line", {pattern: "^ Copyright \\d+$"}],
197
+ errors: [
198
+ {message: "incorrect header"}
199
+ ]
200
+ },
201
+ {
202
+ code: "// Copyright 2017 trailing",
203
+ options: ["line", {pattern: "^ Copyright \\d+$", template: " Copyright 2018"}],
204
+ errors: [
205
+ {message: "incorrect header"}
206
+ ],
207
+ output: "// Copyright 2018\n"
208
+ },
209
+ {
210
+ code: "// Copyright 2017 trailing\n// Someone",
211
+ options: ["line", [{pattern: "^ Copyright \\d+$", template: " Copyright 2018"}, " My Company"]],
212
+ errors: [
213
+ {message: "incorrect header"}
214
+ ],
215
+ output: "// Copyright 2018\n// My Company\n"
216
+ },
217
+ {
218
+ code: "// Copyright 2017\n// Author: ab-c@example.com",
219
+ options: ["line", [{pattern: "Copyright \\d+"}, {pattern: "^ Author: \\w+@\\w+\\.\\w+$"}]],
220
+ errors: [
221
+ {message: "incorrect header"}
222
+ ]
223
+ },
224
+ {
225
+ code: "/* Copyright 2017-01-02\n Author: abc@example.com */",
226
+ options: ["block", {pattern: "^ Copyright \\d+\\n Author: \\w+@\\w+\\.\\w+ $"}],
227
+ errors: [
228
+ {message: "incorrect header"}
229
+ ]
230
+ },
231
+ {
232
+ code: "/*************************\n * Copyright 2015\n * All your base are belong to us!\n *************************/\nconsole.log(1)",
233
+ options: ["block", [
234
+ "************************",
235
+ { pattern: " \\* Copyright \\d{4}", template: " * Copyright 2019" },
236
+ " * My Company",
237
+ " ************************"
238
+ ]],
239
+ errors: [
240
+ {message: "incorrect header"}
241
+ ],
242
+ output: "/*************************\n * Copyright 2019\n * My Company\n *************************/\nconsole.log(1)"
243
+ },
244
+ {
245
+ code: "/*Copyright 2020, My Company*/console.log(1);",
246
+ options: ["block", "Copyright 2020, My Company", 2],
247
+ errors: [
248
+ {message: "no newline after header"}
249
+ ],
250
+ output: "/*Copyright 2020, My Company*/\n\nconsole.log(1);"
251
+ },
252
+ {
253
+ code: "/*Copyright 2020, My Company*/console.log(1);",
254
+ options: ["block", "Copyright 2020, My Company", 1],
255
+ errors: [
256
+ {message: "no newline after header"}
257
+ ],
258
+ output: "/*Copyright 2020, My Company*/\nconsole.log(1);"
259
+ },
260
+ {
261
+ code: "//Copyright 2020\n//My Company\nconsole.log(1);",
262
+ options: ["line", ["Copyright 2020", "My Company"], 2],
263
+ errors: [
264
+ {message: "no newline after header"}
265
+ ],
266
+ output: "//Copyright 2020\n//My Company\n\nconsole.log(1);"
267
+ },
268
+ {
269
+ code: "/*Copyright 2020, My Company*/\nconsole.log(1);\n//Comment\nconsole.log(2);\n//Comment",
270
+ options: ["block", "Copyright 2020, My Company", 2],
271
+ errors: [
272
+ {message: "no newline after header"}
273
+ ],
274
+ output: "/*Copyright 2020, My Company*/\n\nconsole.log(1);\n//Comment\nconsole.log(2);\n//Comment"
275
+ },
276
+ {
277
+ code: "//Copyright 2020\n//My Company\nconsole.log(1);\n//Comment\nconsole.log(2);\n//Comment",
278
+ options: ["line", ["Copyright 2020", "My Company"], 2],
279
+ errors: [
280
+ {message: "no newline after header"}
281
+ ],
282
+ output: "//Copyright 2020\n//My Company\n\nconsole.log(1);\n//Comment\nconsole.log(2);\n//Comment"
283
+ }
284
+ ]
285
+ });
@@ -0,0 +1,4 @@
1
+ /*
2
+ Copyright 2015
3
+ My Company
4
+ */
@@ -0,0 +1,2 @@
1
+ // Copyright 2015
2
+ // My Company