-
Notifications
You must be signed in to change notification settings - Fork 3
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
8 changed files
with
126 additions
and
13 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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"}] | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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") | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
} | ||
} | ||
} | ||
} | ||
}; |
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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)); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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}] | ||
}] | ||
}); |