Skip to content

Commit

Permalink
#1: Notice will now appear above comments
Browse files Browse the repository at this point in the history
  • Loading branch information
nickdeis committed Jul 8, 2017
1 parent 8028d13 commit 102209d
Show file tree
Hide file tree
Showing 13 changed files with 37 additions and 49 deletions.
11 changes: 10 additions & 1 deletion index.js
Original file line number Diff line number Diff line change
Expand Up @@ -35,10 +35,19 @@ module.exports = {
const resolvedTemplate = _.template(template)(allVars);
const sourceCode = context.getSourceCode();
const text = sourceCode.getText().substring(0, chars);
const firstComment = sourceCode.getAllComments()[0];
return {
Program(node) {
function fix(fixer) {
return fixer.insertTextBefore(node, resolvedTemplate);
let topNode;
if(!firstComment){
topNode=node;
}else if(firstComment.loc.start.line <= node.loc.start.line){
topNode=firstComment;
}else{
topNode=node;
}
return fixer.insertTextBefore(topNode, resolvedTemplate);
}
if (!String(text).match(mustMatch)) {
const report = { node, message: `Could not find a match for the mustMatch pattern` };
Expand Down
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.

4 changes: 2 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
@@ -1,13 +1,13 @@
{
"name": "eslint-plugin-notice",
"version": "0.2.2",
"version": "0.2.3",
"description": "An eslint rule that checks the top of files and --fix them too!",
"main": "index.js",
"directories": {
"test": "tests"
},
"scripts": {
"test": "node tests/lib/rules/notice.js && node tests/cli-test.js"
"test": "node tests/lib/rules/notice.js"
},
"keywords": [
"eslint",
Expand Down
1 change: 0 additions & 1 deletion staging/refresh.sh

This file was deleted.

2 changes: 0 additions & 2 deletions staging/setup.sh

This file was deleted.

3 changes: 0 additions & 3 deletions staging/src/fixme-2.js

This file was deleted.

7 changes: 0 additions & 7 deletions staging/src/fixme-3.js

This file was deleted.

6 changes: 0 additions & 6 deletions staging/src/fixme.js

This file was deleted.

7 changes: 0 additions & 7 deletions staging/src/no-fix.js

This file was deleted.

16 changes: 0 additions & 16 deletions tests/cli-test.js

This file was deleted.

8 changes: 8 additions & 0 deletions tests/lib/rules/fix-result-1.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@

/**
* Copyright (c) 2017, Nick Deis
*/

function noStyle(){
return "I didn't read the style guide :(";
}
11 changes: 11 additions & 0 deletions tests/lib/rules/fix-result-2.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@

/**
* Copyright (c) 2017, Nick Deis
*/

/**
* Not exactly what I was looking for
*/
function leastYouTried(){
return false;
}
8 changes: 5 additions & 3 deletions tests/lib/rules/notice.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ const templateFile = path.join(__dirname,"../../test-template.js");

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

const mustMatch = "Copyright \\(c\\) [0-9]{0,4}, Nick Deis";
const mustMatch = /Copyright \(c\) [0-9]{0,4}, Nick Deis/;


const ruleTester = new RuleTester();
Expand All @@ -38,12 +38,14 @@ ruleTester.run("notice",rule,{
{
code:noStyle,
options:[{mustMatch,template}],
errors: [{ message: `Could not find a match for the mustMatch pattern`}]
errors: [{ message: `Could not find a match for the mustMatch pattern`}],
output:fs.readFileSync(__dirname+"/fix-result-1.js","utf8")
},
{
code:notExact,
options:[{mustMatch,template}],
errors:[{message:`Could not find a match for the mustMatch pattern`}]
errors:[{message:`Could not find a match for the mustMatch pattern`}],
output:fs.readFileSync(__dirname+"/fix-result-2.js","utf8")
}
],
valid:[{
Expand Down

0 comments on commit 102209d

Please sign in to comment.