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 1.2.0 to 2.0.0

4

CHANGELOG.md

@@ -0,1 +1,5 @@

# 2.0.0
* 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.
# 1.2.0

@@ -2,0 +6,0 @@

3

lib/comment-parser.js

@@ -12,4 +12,3 @@ "use strict";

"line",
// TODO \r as well
text.split("\n").map(function(line) {
text.split(/\r?\n/).map(function(line) {
return line.substr(2);

@@ -16,0 +15,0 @@ })

@@ -5,2 +5,3 @@ "use strict";

var commentParser = require("../comment-parser");
var os = require("os");

@@ -31,15 +32,15 @@ function isPattern(object) {

function genCommentBody(commentType, textArray) {
function genCommentBody(commentType, textArray, eol) {
if (commentType === "block") {
return "/*" + textArray[0] + "*/\n";
return "/*" + textArray[0] + "*/" + eol;
} else {
return "//" + textArray.join("\n//") + "\n";
return "//" + textArray.join(eol + "//") + eol;
}
}
function genCommentsRange(context, comments) {
function genCommentsRange(context, comments, eol) {
var start = comments[0].range[0];
var end = comments.slice(-1)[0].range[1];
if (context.getSourceCode().text[end] === "\n") {
end++;
if (context.getSourceCode().text[end] === eol) {
end += eol.length;
}

@@ -49,7 +50,7 @@ return [start, end];

function genPrependFixer(commentType, node, headerLines) {
function genPrependFixer(commentType, node, headerLines, eol) {
return function(fixer) {
return fixer.insertTextBefore(
node,
genCommentBody(commentType, headerLines)
genCommentBody(commentType, headerLines, eol)
);

@@ -59,7 +60,7 @@ };

function genReplaceFixer(commentType, context, leadingComments, headerLines) {
function genReplaceFixer(commentType, context, leadingComments, headerLines, eol) {
return function(fixer) {
return fixer.replaceTextRange(
genCommentsRange(context, leadingComments),
genCommentBody(commentType, headerLines)
genCommentsRange(context, leadingComments, eol),
genCommentBody(commentType, headerLines, eol)
);

@@ -69,7 +70,27 @@ };

function findSettings(options) {
var lastOption = options.length > 0 ? options[options.length - 1] : null;
if (typeof lastOption === "object" && !Array.isArray(lastOption) && lastOption !== null && !lastOption.hasOwnProperty("pattern")) {
return lastOption;
}
}
function getEOL(options) {
var settings = findSettings(options);
if (settings && settings.lineEndings === "unix") {
return "\n";
}
if (settings && settings.lineEndings === "windows") {
return "\r\n";
}
return os.EOL;
}
module.exports = function(context) {
var options = context.options;
var eol = getEOL(options);
// If just one option then read comment from file
if (options.length === 1) {
if (options.length === 1 || (options.length === 2 && findSettings(options))) {
var text = fs.readFileSync(context.options[0], "utf8");

@@ -101,4 +122,3 @@ options = commentParser(text);

canFix = true;
// TODO split on \r as well
headerLines = options[1].split("\n");
headerLines = options[1].split(/\r?\n/);
}

@@ -108,3 +128,3 @@ } else {

canFix = true;
header = options[1].join("\n");
header = options[1].join(eol);
} else if (isPattern(options[1])) {

@@ -126,3 +146,3 @@ header = new RegExp(options[1].pattern);

message: "missing header",
fix: canFix ? genPrependFixer(commentType, node, header ? [header] : headerLines) : null
fix: canFix ? genPrependFixer(commentType, node, header ? [header] : headerLines, eol) : null
});

@@ -136,3 +156,3 @@ } else if (leadingComments[0].type.toLowerCase() !== commentType) {

},
fix: canFix ? genReplaceFixer(commentType, context, leadingComments, header ? [header] : headerLines) : null
fix: canFix ? genReplaceFixer(commentType, context, leadingComments, header ? [header] : headerLines, eol) : null
});

@@ -145,3 +165,3 @@ } else {

message: "incorrect header",
fix: canFix ? genReplaceFixer(commentType, context, leadingComments, headerLines) : null
fix: canFix ? genReplaceFixer(commentType, context, leadingComments, headerLines, eol) : null
});

@@ -155,3 +175,3 @@ return;

message: "incorrect header",
fix: canFix ? genReplaceFixer(commentType, context, leadingComments, headerLines) : null
fix: canFix ? genReplaceFixer(commentType, context, leadingComments, headerLines, eol) : null
});

@@ -166,3 +186,3 @@ return;

message: "incorrect header",
fix: canFix ? genReplaceFixer(commentType, context, leadingComments, [header]) : null
fix: canFix ? genReplaceFixer(commentType, context, leadingComments, [header], eol) : null
});

@@ -169,0 +189,0 @@ }

{
"name": "eslint-plugin-header",
"version": "1.2.0",
"version": "2.0.0",
"description": "ESLint plugin to ensure that files begin with given comment",

@@ -5,0 +5,0 @@ "main": "index.js",

@@ -10,3 +10,3 @@ eslint-plugin-header

This rule takes 1 or 2 arguments.
This rule takes 1 or 2 arguments with an optional settings object.

@@ -39,3 +39,3 @@ ### 1 argument

In the 2 argument form the first must be either `"block"` or `"line"` to indiciate 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.
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.

@@ -66,2 +66,12 @@ ```json

### Line Endings
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:
```json
"rules": {
"header/header": [2, "block", ["Copyright 2018", "My Company"], {"lineEndings": "windows"}]
}
```
Possible values are `unix` for `\n` and `windows` for `\r\n` line endings.
## Examples

@@ -68,0 +78,0 @@

@@ -73,2 +73,18 @@ "use strict";

]]
},
{
code: "// Copyright 2015\r\n// My Company\r\nconsole.log(1)",
options: ["tests/support/line.js"]
},
{
code: "//Copyright 2018\r\n//My Company\r\n/* DOCS */",
options: ["line", ["Copyright 2018", "My Company"]]
},
{
code: "/*Copyright 2018\r\nMy Company*/\r\nconsole.log(1)",
options: ["block", ["Copyright 2018", "My Company"], {"lineEndings": "windows"}]
},
{
code: "/*Copyright 2018\nMy Company*/\nconsole.log(1)",
options: ["block", ["Copyright 2018", "My Company"], {"lineEndings": "unix"}]
}

@@ -145,4 +161,11 @@ ],

]
},
{
code: "/*Copyright 2018\r\nMy Company*/\r\nconsole.log(1)",
options: ["block", ["Copyright 2018", "My Company"], {"lineEndings": "unix"}],
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