Skip to content

Commit

Permalink
Tests in place, switch to notice
Browse files Browse the repository at this point in the history
  • Loading branch information
nickdeis committed Jun 7, 2017
1 parent 8099675 commit 33007ae
Show file tree
Hide file tree
Showing 8 changed files with 126 additions and 13 deletions.
4 changes: 2 additions & 2 deletions .eslintrc
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"plugins":["top"],
"plugins":["notice"],
"rule":{
"top":["error",{"mustMatch":"[0-9]{0,4}, Nicholas Deis","templateFile":"../tests/test-template.js"}]
"notice":["error",{"mustMatch":"[0-9]{0,4}, Nicholas Deis","templateFile":"../tests/test-template.js"}]
}
}
Empty file added README.md
Empty file.
2 changes: 1 addition & 1 deletion index.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
module.exports = {
rules:{
top:require("./lib/rules/top.js")
notice:require("./lib/rules/notice.js")
}
}
48 changes: 48 additions & 0 deletions lib/rules/notice.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
"use strict";

const fs = require("fs"),
_ = require("lodash");



module.exports = {
meta: {
docs: {
description: "An eslint rule that checks the top comment and fixes it too!",
category: "Stylistic Issues"
},
fixable: "code"
},
create(context) {
let {mustMatch,templateFile,template,templateVars,chars} = context.options[0];
if(!(mustMatch instanceof RegExp)){
mustMatch = new RegExp(mustMatch);
}

if(!template && templateFile){
template = fs.readFileSync(templateFile,"utf8");
}
templateVars = templateVars || {};
chars = chars || 1000;
const YEAR = new Date().getFullYear();
const allVars = Object.assign({},{YEAR},templateVars);
const resolvedTemplate = _.template(template)(allVars);
const sourceCode = context.getSourceCode();
const text = sourceCode.getText().substring(0,chars);
return {
Program(node){
function fix(fixer){
return fixer.insertTextBefore(node,resolvedTemplate);
}
if(!String(text).match(mustMatch)){
const report = {node,message:`Could not find a match for the mustMatch pattern`};
if(resolvedTemplate){
report.fix = fix;
}
context.report(report);
return;
}
}
}
}
};
2 changes: 1 addition & 1 deletion package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

10 changes: 7 additions & 3 deletions package.json
Original file line number Diff line number Diff line change
@@ -1,19 +1,20 @@
{
"name": "eslint-plugin-top",
"name": "eslint-plugin-notice",
"version": "0.0.1",
"description": "An eslint rule that checks the top comment and fixes it too!",
"main": "index.js",
"directories": {
"test": "tests"
},
"scripts": {
"test": "echo \"Error: no test specified\" && exit 1"
"test": "node tests/lib/rules/notice.js && node tests/cli-test.js"
},
"keywords": [
"eslint",
"plugin",
"eslint-plugin",
"top",
"notice",
"copyright",
"header",
"lint"
],
Expand All @@ -24,5 +25,8 @@
},
"dependencies": {
"lodash": "^4.17.4"
},
"peerDependencies": {
"eslint": "^3.19.0"
}
}
12 changes: 6 additions & 6 deletions tests/cli-test.js
Original file line number Diff line number Diff line change
@@ -1,16 +1,16 @@
var CLIEngine = require("eslint").CLIEngine;
const CLIEngine = require("eslint").CLIEngine;
const path = require("path");
var cli = new CLIEngine(
{
useEslintrc: false,
plugins:["top"],
rules:{
top:["error",{"mustMatch":"[0-9]{0,4}, Nicholas Deis","templateFile":"../tests/test-template.js"}]
notice:["error",{"mustMatch":"[0-9]{0,4}, Nicholas Deis","templateFile":path.join(__dirname,"../tests/test-template.js")}]
},
fix:true,
rulePaths:["../lib/rules"]
rulePaths:[path.resolve(__dirname,"../lib/rules")]
}
);
);

var report = cli.executeOnFiles(["../staging/src"]);
const report = cli.executeOnFiles([path.resolve(__dirname,"../staging/src")]);

console.log(JSON.stringify(report,null,2));
61 changes: 61 additions & 0 deletions tests/lib/rules/notice.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,61 @@
/**
* @fileoverview Tests for top rule
* @author Nicholas Deis
*/
const RuleTester = require("eslint/lib/testers/rule-tester"),
rule = require("../../../lib/rules/notice"),
fs = require("fs"),
path = require("path");

const templateFile = path.join(__dirname,"../../test-template.js");

const template = fs.readFileSync(templateFile,"utf8");

const mustMatch = "[0-9]{0,4}, Nicholas Deis";


const ruleTester = new RuleTester();

const notExact = `
/**
* Not exactly what I was looking for
*/
function leastYouTried(){
return false;
}
`;

const noStyle = `
function noStyle(){
return "I didn't read the style guide :(";
}
`;



ruleTester.run("notice",rule,{
invalid:[
{
code:noStyle,
options:[{mustMatch,template}],
errors: [{ message: `Could not find a match for the mustMatch pattern`}]
},
{
code:notExact,
options:[{mustMatch,template}],
errors:[{message:`Could not find a match for the mustMatch pattern`}]
}
],
valid:[{
code:`
/**
* Copyright (c) 2017, Nicholas Deis
* All rights reserved.
*/
function stylin(){
return "I read the style guide, or eslint handled it for me";
}
`,
options:[{mustMatch,template}]
}]
});

0 comments on commit 33007ae

Please sign in to comment.