Skip to content

Commit

Permalink
Initial import
Browse files Browse the repository at this point in the history
  • Loading branch information
Stuk committed Aug 7, 2015
0 parents commit 9e66f7b
Show file tree
Hide file tree
Showing 7 changed files with 276 additions and 0 deletions.
98 changes: 98 additions & 0 deletions .eslintrc
Original file line number Diff line number Diff line change
@@ -0,0 +1,98 @@
root: true

env:
node: true

extends:
"eslint:recommended"

rules:
indent: [2, 4, {SwitchCase: 1}]
brace-style: [2, "1tbs"]
camelcase: [2, { properties: "never" }]
callback-return: [2, ["cb", "callback", "next"]]
comma-spacing: 2
comma-style: [2, "last"]
consistent-return: 2
curly: [2, "all"]
default-case: 2
dot-notation: [2, { allowKeywords: true }]
eol-last: 2
eqeqeq: 2
func-style: [2, "declaration"]
guard-for-in: 2
key-spacing: [2, { beforeColon: false, afterColon: true }]
new-cap: 2
new-parens: 2
no-alert: 2
no-array-constructor: 2
no-caller: 2
no-console: 0
no-delete-var: 2
no-empty-label: 2
no-eval: 2
no-extend-native: 2
no-extra-bind: 2
no-fallthrough: 2
no-floating-decimal: 2
no-implied-eval: 2
no-invalid-this: 2
no-iterator: 2
no-label-var: 2
no-labels: 2
no-lone-blocks: 2
no-loop-func: 2
no-mixed-spaces-and-tabs: [2, false]
no-multi-spaces: 2
no-multi-str: 2
no-native-reassign: 2
no-nested-ternary: 2
no-new: 2
no-new-func: 2
no-new-object: 2
no-new-wrappers: 2
no-octal: 2
no-octal-escape: 2
no-process-exit: 2
no-proto: 2
no-redeclare: 2
no-return-assign: 2
no-script-url: 2
no-sequences: 2
no-shadow: 2
no-shadow-restricted-names: 2
no-spaced-func: 2
no-trailing-spaces: 2
no-undef: 2
no-undef-init: 2
no-undefined: 2
no-underscore-dangle: 2
no-unused-expressions: 2
no-unused-vars: [2, {vars: "all", args: "after-used"}]
no-use-before-define: 2
no-with: 2
quotes: [2, "double"]
radix: 2
semi: 2
semi-spacing: [2, {before: false, after: true}]
space-after-keywords: [2, "always"]
space-before-blocks: 2
space-before-function-paren: [2, "never"]
space-infix-ops: 2
space-return-throw-case: 2
space-unary-ops: [2, {words: true, nonwords: false}]
spaced-comment: [2, "always", { exceptions: ["-"]}]
strict: [2, "global"]
valid-jsdoc: [2, { prefer: { "return": "returns"}}]
wrap-iife: 2
yoda: [2, "never"]

# Previously on by default in node environment
no-catch-shadow: 0
no-console: 0
no-mixed-requires: 2
no-new-require: 2
no-path-concat: 2
no-process-exit: 2
global-strict: [0, "always"]
handle-callback-err: [2, "err"]
3 changes: 3 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
.DS_Store
node_modules
npm-debug.log
17 changes: 17 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
eslint-plugin-header
====================

Ensure that source files have a certain header.

## Usage

```json
{
"plugins": [
"header"
],
"rules": {
"header": [2, "block", "Copyright 2015\nMy Company"]
}
}
```
10 changes: 10 additions & 0 deletions index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
"use strict";

module.exports = {
rules: {
"header": require("./lib/rules/header")
},
rulesConfig: {
"header": 0
}
};
52 changes: 52 additions & 0 deletions lib/rules/header.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
"use strict";

module.exports = function(context) {
var commentType = context.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;
if (commentType === "line") {
if (Array.isArray(context.options[1])) {
headerLines = context.options[1];
} else {
// TODO split on \r as well
headerLines = context.options[1].split("\n");
}
} else {
if (Array.isArray(context.options[1])) {
header = context.options[1].join("\n");
} else {
header = context.options[1];
}
}

return {
Program: function(node) {

var leadingComments;
if (node.body.length) {
leadingComments = context.getComments(node.body[0]).leading;
} else {
leadingComments = context.getComments(node).leading;
}

if (!leadingComments.length) {
context.report(node, "missing header");
} else if (leadingComments[0].type.toLowerCase() !== commentType) {
context.report(node, "header should be a " + commentType + " comment");
} else {
if (commentType === "line") {
for (var i = 0; i < headerLines.length; i++) {
if (leadingComments[i].value !== headerLines[i]) {
context.report(node, "incorrect header");
return;
}
}
} else if (leadingComments[0].value !== header) {
context.report(node, "incorrect header");
}
}
}
};
};
24 changes: 24 additions & 0 deletions package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
{
"name": "eslint-plugin-header",
"version": "0.0.1",
"description": "ESLint plugin to ensure that files begin with given comment",
"main": "index.js",
"scripts": {
"test": "mocha tests/**/*.js",
"lint": "eslint ."
},
"devDependencies": {
"eslint": "^1.0.0",
"eslint-tester": "^0.8.2",
"mocha": "^2.2.5"
},
"peerDependencies": {
"eslint": ">=0.18.0"
},
"keywords": [
"eslint",
"eslintplugin"
],
"author": "Stuart Knightley",
"license": "MIT"
}
72 changes: 72 additions & 0 deletions tests/lib/rules/header.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,72 @@
"use strict";

var rule = require("../../../lib/rules/header");
var RuleTester = require("eslint").RuleTester;

var ruleTester = new RuleTester();
ruleTester.run("header", rule, {
valid: [
{
code: "/*Copyright 2015, My Company*/\nconsole.log(1);",
options: ["block", "Copyright 2015, My Company"]
},
{
code: "//Copyright 2015, My Company\nconsole.log(1);",
options: ["line", "Copyright 2015, My Company"]
},
{
code: "/*Copyright 2015, My Company*/",
options: ["block", "Copyright 2015, My Company"]
},
{
code: "//Copyright 2015\n//My Company\nconsole.log(1)",
options: ["line", "Copyright 2015\nMy Company"]
},
{
code: "//Copyright 2015\n//My Company\nconsole.log(1)",
options: ["line", ["Copyright 2015", "My Company"]]
},
{
code: "/*Copyright 2015\nMy Company*/\nconsole.log(1)",
options: ["block", ["Copyright 2015", "My Company"]]
}
],
invalid: [
{
code: "console.log(1);",
options: ["block", "Copyright 2015, My Company"],
errors: [
{message: "missing header"}
]
},
{
code: "//Copyright 2014, My Company\nconsole.log(1);",
options: ["block", "Copyright 2015, My Company"],
errors: [
{message: "header should be a block comment"}
]
},
{
code: "/*Copyright 2014, My Company*/\nconsole.log(1);",
options: ["line", "Copyright 2015, My Company"],
errors: [
{message: "header should be a line comment"}
]
},
{
code: "/*Copyright 2014, My Company*/\nconsole.log(1);",
options: ["block", "Copyright 2015, My Company"],
errors: [
{message: "incorrect header"}
]
},
{
code: "//Copyright 2014\n//My Company\nconsole.log(1)",
options: ["line", "Copyright 2015\nMy Company"],
errors: [
{message: "incorrect header"}
]

}
]
});

0 comments on commit 9e66f7b

Please sign in to comment.