eslint-plugin-header
Advanced tools
Comparing version 1.1.0 to 1.2.0
@@ -0,1 +1,5 @@ | ||
# 1.2.0 | ||
* Add auto fix functionality (eslint `--fix` option) (#12) | ||
# 1.1.0 | ||
@@ -2,0 +6,0 @@ |
@@ -19,3 +19,5 @@ "use strict"; | ||
function excludeShebangs(comments) { | ||
return comments.filter(function(comment) { return comment.type !== "Shebang"; }); | ||
return comments.filter(function(comment) { | ||
return comment.type !== "Shebang"; | ||
}); | ||
} | ||
@@ -29,2 +31,37 @@ | ||
function genCommentBody(commentType, textArray) { | ||
if (commentType === "block") { | ||
return "/*" + textArray[0] + "*/\n"; | ||
} else { | ||
return "//" + textArray.join("\n//") + "\n"; | ||
} | ||
} | ||
function genCommentsRange(context, comments) { | ||
var start = comments[0].range[0]; | ||
var end = comments.slice(-1)[0].range[1]; | ||
if (context.getSourceCode().text[end] === "\n") { | ||
end++; | ||
} | ||
return [start, end]; | ||
} | ||
function genPrependFixer(commentType, node, headerLines) { | ||
return function(fixer) { | ||
return fixer.insertTextBefore( | ||
node, | ||
genCommentBody(commentType, headerLines) | ||
); | ||
}; | ||
} | ||
function genReplaceFixer(commentType, context, leadingComments, headerLines) { | ||
return function(fixer) { | ||
return fixer.replaceTextRange( | ||
genCommentsRange(context, leadingComments), | ||
genCommentBody(commentType, headerLines) | ||
); | ||
}; | ||
} | ||
module.exports = function(context) { | ||
@@ -43,6 +80,15 @@ var options = context.options; | ||
var header, headerLines; | ||
// If any of the lines are regular expressions, then we can't | ||
// automatically fix them. We set this to true below once we | ||
// ensure none of the lines are of type RegExp | ||
var canFix = false; | ||
if (commentType === "line") { | ||
if (Array.isArray(options[1])) { | ||
canFix = true; | ||
headerLines = options[1].map(function(line) { | ||
return isPattern(line) ? new RegExp(line.pattern) : line; | ||
var isRegex = isPattern(line); | ||
if (isRegex) { | ||
canFix = false; | ||
} | ||
return isRegex ? new RegExp(line.pattern) : line; | ||
}); | ||
@@ -52,2 +98,3 @@ } else if (isPattern(options[1])) { | ||
} else { | ||
canFix = true; | ||
// TODO split on \r as well | ||
@@ -58,2 +105,3 @@ headerLines = options[1].split("\n"); | ||
if (Array.isArray(options[1])) { | ||
canFix = true; | ||
header = options[1].join("\n"); | ||
@@ -63,2 +111,3 @@ } else if (isPattern(options[1])) { | ||
} else { | ||
canFix = true; | ||
header = options[1]; | ||
@@ -73,9 +122,24 @@ } | ||
if (!leadingComments.length) { | ||
context.report(node, "missing header"); | ||
context.report({ | ||
loc: node.loc, | ||
message: "missing header", | ||
fix: canFix ? genPrependFixer(commentType, node, header ? [header] : headerLines) : null | ||
}); | ||
} else if (leadingComments[0].type.toLowerCase() !== commentType) { | ||
context.report(node, "header should be a " + commentType + " comment"); | ||
context.report({ | ||
loc: node.loc, | ||
message: "header should be a {{commentType}} comment", | ||
data: { | ||
commentType: commentType | ||
}, | ||
fix: canFix ? genReplaceFixer(commentType, context, leadingComments, header ? [header] : headerLines) : null | ||
}); | ||
} else { | ||
if (commentType === "line") { | ||
if (leadingComments.length < headerLines.length) { | ||
context.report(node, "incorrect header"); | ||
context.report({ | ||
loc: node.loc, | ||
message: "incorrect header", | ||
fix: canFix ? genReplaceFixer(commentType, context, leadingComments, headerLines) : null | ||
}); | ||
return; | ||
@@ -85,3 +149,7 @@ } | ||
if (!match(leadingComments[i].value, headerLines[i])) { | ||
context.report(node, "incorrect header"); | ||
context.report({ | ||
loc: node.loc, | ||
message: "incorrect header", | ||
fix: canFix ? genReplaceFixer(commentType, context, leadingComments, headerLines) : null | ||
}); | ||
return; | ||
@@ -92,3 +160,7 @@ } | ||
if (!match(leadingComments[0].value, header)) { | ||
context.report(node, "incorrect header"); | ||
context.report({ | ||
loc: node.loc, | ||
message: "incorrect header", | ||
fix: canFix ? genReplaceFixer(commentType, context, leadingComments, [header]) : null | ||
}); | ||
} | ||
@@ -95,0 +167,0 @@ } |
{ | ||
"name": "eslint-plugin-header", | ||
"version": "1.1.0", | ||
"version": "1.2.0", | ||
"description": "ESLint plugin to ensure that files begin with given comment", | ||
"main": "index.js", | ||
"scripts": { | ||
"test": "mocha tests/lib/*.js tests/lib/**/*.js", | ||
"test": "npm run lint && npm run unit", | ||
"unit": "mocha tests/lib/*.js tests/lib/**/*.js", | ||
"lint": "eslint ." | ||
@@ -9,0 +10,0 @@ }, |
@@ -67,8 +67,8 @@ "use strict"; | ||
{ | ||
code: "#!/usr/bin/env node\n/**\n * Copyright\n */", | ||
options: ["block", [ | ||
"*", | ||
" * Copyright", | ||
" " | ||
]] | ||
code: "#!/usr/bin/env node\n/**\n * Copyright\n */", | ||
options: ["block", [ | ||
"*", | ||
" * Copyright", | ||
" " | ||
]] | ||
} | ||
@@ -82,3 +82,4 @@ ], | ||
{message: "missing header"} | ||
] | ||
], | ||
output: "/*Copyright 2015, My Company*/\nconsole.log(1);" | ||
}, | ||
@@ -90,3 +91,4 @@ { | ||
{message: "header should be a block comment"} | ||
] | ||
], | ||
output: "/*Copyright 2015, My Company*/\nconsole.log(1);" | ||
}, | ||
@@ -98,3 +100,4 @@ { | ||
{message: "header should be a line comment"} | ||
] | ||
], | ||
output: "//Copyright 2015, My Company\nconsole.log(1);" | ||
}, | ||
@@ -106,3 +109,4 @@ { | ||
{message: "incorrect header"} | ||
] | ||
], | ||
output: "/*Copyright 2015, My Company*/\nconsole.log(1);" | ||
}, | ||
@@ -114,3 +118,4 @@ { | ||
{message: "incorrect header"} | ||
] | ||
], | ||
output: "//Copyright 2015\n//My Company\nconsole.log(1)" | ||
}, | ||
@@ -122,3 +127,4 @@ { | ||
{message: "incorrect header"} | ||
] | ||
], | ||
output: "//Copyright 2015\n//My Company\n" | ||
}, | ||
@@ -125,0 +131,0 @@ { |
Sorry, the diff of this file is not supported yet
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
19229
14
348