Huge News!Announcing our $40M Series B led by Abstract Ventures.Learn More
Socket
Sign inDemoInstall
Socket

eslint-plugin-header

Package Overview
Dependencies
Maintainers
1
Versions
10
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

eslint-plugin-header - npm Package Compare versions

Comparing version 0.1.0 to 1.0.0

CHANGELOG.md

33

lib/rules/header.js

@@ -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 @@ }

2

package.json
{
"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"}
]
}
]
});
SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap
  • Changelog

Packages

npm

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc