eslint-plugin-header
Advanced tools
Comparing version
@@ -0,1 +1,7 @@ | ||
# 3.0.0 | ||
* Allow regexp in multiline arrays (#23) | ||
* Add `template` option for regexps, for `eslint --fix` (#23) | ||
* Update eslint to v5.12.0 (#19) | ||
# 2.0.0 | ||
@@ -2,0 +8,0 @@ |
@@ -33,3 +33,3 @@ "use strict"; | ||
if (commentType === "block") { | ||
return "/*" + textArray[0] + "*/" + eol; | ||
return "/*" + textArray.join(eol) + "*/" + eol; | ||
} else { | ||
@@ -72,2 +72,3 @@ return "//" + textArray.join(eol + "//") + eol; | ||
} | ||
return null; | ||
} | ||
@@ -98,5 +99,3 @@ | ||
var commentType = options[0]; | ||
// If commentType is line then we want an array of the lines, | ||
// but if block then we want just a string | ||
var header, headerLines; | ||
var headerLines, fixLines = []; | ||
// If any of the lines are regular expressions, then we can't | ||
@@ -106,28 +105,23 @@ // automatically fix them. We set this to true below once we | ||
var canFix = false; | ||
if (commentType === "line") { | ||
if (Array.isArray(options[1])) { | ||
canFix = true; | ||
headerLines = options[1].map(function(line) { | ||
var isRegex = isPattern(line); | ||
if (isRegex) { | ||
canFix = false; | ||
} | ||
return isRegex ? new RegExp(line.pattern) : line; | ||
}); | ||
} else if (isPattern(options[1])) { | ||
headerLines = [new RegExp(options[1].pattern)]; | ||
} else { | ||
canFix = true; | ||
headerLines = options[1].split(/\r?\n/); | ||
} | ||
if (Array.isArray(options[1])) { | ||
canFix = true; | ||
headerLines = options[1].map(function(line) { | ||
var isRegex = isPattern(line); | ||
// Can only fix regex option if a template is also provided | ||
if (isRegex && !line.template) { | ||
canFix = false; | ||
} | ||
fixLines.push(line.template || line); | ||
return isRegex ? new RegExp(line.pattern) : line; | ||
}); | ||
} else if (isPattern(options[1])) { | ||
var line = options[1]; | ||
headerLines = [new RegExp(line.pattern)]; | ||
fixLines.push(line.template || line); | ||
// Same as above for regex and template | ||
canFix = !!line.template; | ||
} else { | ||
if (Array.isArray(options[1])) { | ||
canFix = true; | ||
header = options[1].join(eol); | ||
} else if (isPattern(options[1])) { | ||
header = new RegExp(options[1].pattern); | ||
} else { | ||
canFix = true; | ||
header = options[1]; | ||
} | ||
canFix = true; | ||
headerLines = options[1].split(/\r?\n/); | ||
fixLines = headerLines; | ||
} | ||
@@ -143,3 +137,3 @@ | ||
message: "missing header", | ||
fix: canFix ? genPrependFixer(commentType, node, header ? [header] : headerLines, eol) : null | ||
fix: canFix ? genPrependFixer(commentType, node, fixLines, eol) : null | ||
}); | ||
@@ -153,3 +147,3 @@ } else if (leadingComments[0].type.toLowerCase() !== commentType) { | ||
}, | ||
fix: canFix ? genReplaceFixer(commentType, context, leadingComments, header ? [header] : headerLines, eol) : null | ||
fix: canFix ? genReplaceFixer(commentType, context, leadingComments, fixLines, eol) : null | ||
}); | ||
@@ -162,3 +156,3 @@ } else { | ||
message: "incorrect header", | ||
fix: canFix ? genReplaceFixer(commentType, context, leadingComments, headerLines, eol) : null | ||
fix: canFix ? genReplaceFixer(commentType, context, leadingComments, fixLines, eol) : null | ||
}); | ||
@@ -172,3 +166,3 @@ return; | ||
message: "incorrect header", | ||
fix: canFix ? genReplaceFixer(commentType, context, leadingComments, headerLines, eol) : null | ||
fix: canFix ? genReplaceFixer(commentType, context, leadingComments, fixLines, eol) : null | ||
}); | ||
@@ -179,7 +173,26 @@ return; | ||
} else { | ||
if (!match(leadingComments[0].value, header)) { | ||
// if block comment pattern has more than 1 line, we also split the comment | ||
var leadingLines = [leadingComments[0].value]; | ||
if (headerLines.length > 1) { | ||
leadingLines = leadingComments[0].value.split(/\r?\n/); | ||
} | ||
var hasError = false; | ||
if (leadingLines.length > headerLines.length) { | ||
hasError = true; | ||
} | ||
for (i = 0; !hasError && i < headerLines.length; i++) { | ||
if (!match(leadingLines[i], headerLines[i])) { | ||
hasError = true; | ||
} | ||
} | ||
if (hasError) { | ||
if (canFix && headerLines.length > 1) { | ||
fixLines = [fixLines.join(eol)]; | ||
} | ||
context.report({ | ||
loc: node.loc, | ||
message: "incorrect header", | ||
fix: canFix ? genReplaceFixer(commentType, context, leadingComments, [header], eol) : null | ||
fix: canFix ? genReplaceFixer(commentType, context, leadingComments, fixLines, eol) : null | ||
}); | ||
@@ -186,0 +199,0 @@ } |
{ | ||
"name": "eslint-plugin-header", | ||
"version": "2.0.0", | ||
"version": "3.0.0", | ||
"description": "ESLint plugin to ensure that files begin with given comment", | ||
@@ -12,4 +12,4 @@ "main": "index.js", | ||
"devDependencies": { | ||
"eslint": "^1.0.0", | ||
"mocha": "^2.2.5" | ||
"eslint": "^5.12.0", | ||
"mocha": "^5.2.0" | ||
}, | ||
@@ -16,0 +16,0 @@ "peerDependencies": { |
@@ -51,4 +51,6 @@ eslint-plugin-header | ||
Instead of a string to be checked for exact matching you can also supply a regular expression (beware that you have to quote backslashes): | ||
#### Regular expressions | ||
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: | ||
```json | ||
@@ -60,3 +62,6 @@ { | ||
"rules": { | ||
"header/header": [2, "block", {"pattern": "^ Copyright \\d{4}\\n My Company$"}] | ||
"header/header": [2, "block", [ | ||
{"pattern": " Copyright \\d{4}"}, | ||
"My Company" | ||
]] | ||
} | ||
@@ -66,2 +71,25 @@ } | ||
This would match: | ||
```js | ||
/* Copyright 2808 | ||
My Company*/ | ||
``` | ||
When you use a regular expression `pattern`, you can also provide a `template` property, to provide the comment value when using `eslint --fix`: | ||
```json | ||
{ | ||
"plugins": [ | ||
"header" | ||
], | ||
"rules": { | ||
"header/header": [2, "block", [ | ||
{"pattern": " Copyright \\d{4}", "template": " Copyright 2019"}, | ||
"My Company" | ||
]] | ||
} | ||
} | ||
``` | ||
### Line Endings | ||
@@ -125,2 +153,2 @@ | ||
MIT | ||
MIT |
@@ -89,2 +89,11 @@ "use strict"; | ||
options: ["block", ["Copyright 2018", "My Company"], {"lineEndings": "unix"}] | ||
}, | ||
{ | ||
code: "/*************************\n * Copyright 2015\n * My Company\n *************************/\nconsole.log(1)", | ||
options: ["block", [ | ||
"************************", | ||
{ pattern: " \\* Copyright \\d{4}" }, | ||
" * My Company", | ||
" ************************" | ||
]] | ||
} | ||
@@ -126,2 +135,19 @@ ], | ||
{ | ||
// Test extra line in comment | ||
code: "/*Copyright 2015\nMy Company\nExtra*/\nconsole.log(1);", | ||
options: ["block", ["Copyright 2015", "My Company"]], | ||
errors: [ | ||
{message: "incorrect header"} | ||
], | ||
output: "/*Copyright 2015\nMy Company*/\nconsole.log(1);" | ||
}, | ||
{ | ||
code: "/*Copyright 2015\n*/\nconsole.log(1);", | ||
options: ["block", ["Copyright 2015", "My Company"]], | ||
errors: [ | ||
{message: "incorrect header"} | ||
], | ||
output: "/*Copyright 2015\nMy Company*/\nconsole.log(1);" | ||
}, | ||
{ | ||
code: "//Copyright 2014\n//My Company\nconsole.log(1)", | ||
@@ -150,2 +176,18 @@ options: ["line", "Copyright 2015\nMy Company"], | ||
{ | ||
code: "// Copyright 2017 trailing", | ||
options: ["line", {pattern: "^ Copyright \\d+$", template: " Copyright 2018"}], | ||
errors: [ | ||
{message: "incorrect header"} | ||
], | ||
output: "// Copyright 2018\n" | ||
}, | ||
{ | ||
code: "// Copyright 2017 trailing\n// Someone", | ||
options: ["line", [{pattern: "^ Copyright \\d+$", template: " Copyright 2018"}, " My Company"]], | ||
errors: [ | ||
{message: "incorrect header"} | ||
], | ||
output: "// Copyright 2018\n// My Company\n" | ||
}, | ||
{ | ||
code: "// Copyright 2017\n// Author: ab-c@example.com", | ||
@@ -165,9 +207,15 @@ options: ["line", [{pattern: "Copyright \\d+"}, {pattern: "^ Author: \\w+@\\w+\\.\\w+$"}]], | ||
{ | ||
code: "/*Copyright 2018\r\nMy Company*/\r\nconsole.log(1)", | ||
options: ["block", ["Copyright 2018", "My Company"], {"lineEndings": "unix"}], | ||
code: "/*************************\n * Copyright 2015\n * All your base are belong to us!\n *************************/\nconsole.log(1)", | ||
options: ["block", [ | ||
"************************", | ||
{ pattern: " \\* Copyright \\d{4}", template: " * Copyright 2019" }, | ||
" * My Company", | ||
" ************************" | ||
]], | ||
errors: [ | ||
{message: "incorrect header"} | ||
] | ||
], | ||
output: "/*************************\n * Copyright 2019\n * My Company\n *************************/\nconsole.log(1)" | ||
} | ||
] | ||
}); |
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
24666
14.9%446
15.25%151
23.77%