eslint-plugin-header
Advanced tools
Comparing version 0.1.0 to 1.0.0
@@ -6,2 +6,14 @@ "use strict"; | ||
function isPattern(object) { | ||
return typeof object === "object" && object.hasOwnProperty("pattern"); | ||
} | ||
function match(actual, expected) { | ||
if (expected.test) { | ||
return expected.test(actual); | ||
} else { | ||
return expected === actual; | ||
} | ||
} | ||
module.exports = function(context) { | ||
@@ -22,3 +34,7 @@ var options = context.options; | ||
if (Array.isArray(options[1])) { | ||
headerLines = options[1]; | ||
headerLines = options[1].map(function(line) { | ||
return isPattern(line) ? new RegExp(line.pattern) : line; | ||
}); | ||
} else if (isPattern(options[1])) { | ||
headerLines = [new RegExp(options[1].pattern)]; | ||
} else { | ||
@@ -31,2 +47,4 @@ // TODO split on \r as well | ||
header = options[1].join("\n"); | ||
} else if (isPattern(options[1])) { | ||
header = new RegExp(options[1].pattern); | ||
} else { | ||
@@ -39,3 +57,2 @@ header = options[1]; | ||
Program: function(node) { | ||
var leadingComments; | ||
@@ -54,4 +71,8 @@ if (node.body.length) { | ||
if (commentType === "line") { | ||
if (leadingComments.length < headerLines.length) { | ||
context.report(node, "incorrect header"); | ||
return; | ||
} | ||
for (var i = 0; i < headerLines.length; i++) { | ||
if (leadingComments[i].value !== headerLines[i]) { | ||
if (!match(leadingComments[i].value, headerLines[i])) { | ||
context.report(node, "incorrect header"); | ||
@@ -61,4 +82,6 @@ return; | ||
} | ||
} else if (leadingComments[0].value !== header) { | ||
context.report(node, "incorrect header"); | ||
} else { | ||
if (!match(leadingComments[0].value, header)) { | ||
context.report(node, "incorrect header"); | ||
} | ||
} | ||
@@ -65,0 +88,0 @@ } |
{ | ||
"name": "eslint-plugin-header", | ||
"version": "0.1.0", | ||
"version": "1.0.0", | ||
"description": "ESLint plugin to ensure that files begin with given comment", | ||
@@ -5,0 +5,0 @@ "main": "index.js", |
@@ -51,2 +51,15 @@ 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): | ||
```json | ||
{ | ||
"plugins": [ | ||
"header" | ||
], | ||
"rules": { | ||
"header/header": [2, "block", {"pattern": "^ Copyright \\d{4}\\n My Company$"}] | ||
} | ||
} | ||
``` | ||
## Examples | ||
@@ -71,2 +84,10 @@ | ||
`"line", [{pattern: "^Copyright \\d{4}$"}, {pattern: "^My Company$"}]]`: | ||
```js | ||
//Copyright 2017 | ||
//My Company | ||
console.log(1) | ||
``` | ||
### With more decoration | ||
@@ -73,0 +94,0 @@ |
@@ -49,2 +49,18 @@ "use strict"; | ||
options: ["tests/support/line.js"] | ||
}, | ||
{ | ||
code: "//Copyright 2015\n//My Company\n/* DOCS */", | ||
options: ["line", "Copyright 2015\nMy Company"] | ||
}, | ||
{ | ||
code: "// Copyright 2017", | ||
options: ["line", {pattern: "^ Copyright \\d+$"}] | ||
}, | ||
{ | ||
code: "// Copyright 2017\n// Author: abc@example.com", | ||
options: ["line", [{pattern: "^ Copyright \\d+$"}, {pattern: "^ Author: \\w+@\\w+\\.\\w+$"}]] | ||
}, | ||
{ | ||
code: "/* Copyright 2017\n Author: abc@example.com */", | ||
options: ["block", {pattern: "^ Copyright \\d{4}\\n Author: \\w+@\\w+\\.\\w+ $"}] | ||
} | ||
@@ -87,4 +103,32 @@ ], | ||
] | ||
}, | ||
{ | ||
code: "//Copyright 2015", | ||
options: ["line", "Copyright 2015\nMy Company"], | ||
errors: [ | ||
{message: "incorrect header"} | ||
] | ||
}, | ||
{ | ||
code: "// Copyright 2017 trailing", | ||
options: ["line", {pattern: "^ Copyright \\d+$"}], | ||
errors: [ | ||
{message: "incorrect header"} | ||
] | ||
}, | ||
{ | ||
code: "// Copyright 2017\n// Author: ab-c@example.com", | ||
options: ["line", [{pattern: "Copyright \\d+"}, {pattern: "^ Author: \\w+@\\w+\\.\\w+$"}]], | ||
errors: [ | ||
{message: "incorrect header"} | ||
] | ||
}, | ||
{ | ||
code: "/* Copyright 2017-01-02\n Author: abc@example.com */", | ||
options: ["block", {pattern: "^ Copyright \\d+\\n Author: \\w+@\\w+\\.\\w+ $"}], | ||
errors: [ | ||
{message: "incorrect header"} | ||
] | ||
} | ||
] | ||
}); |
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
No v1
QualityPackage is not semver >=1. This means it is not stable and does not support ^ ranges.
Found 1 instance in 1 package
15679
263
1
112