From 102209dcca6e728aabd2a8207458821708255c68 Mon Sep 17 00:00:00 2001 From: Nicholas Deis Date: Sat, 8 Jul 2017 01:43:34 -0400 Subject: [PATCH] #1: Notice will now appear above comments --- index.js | 11 ++++++++++- package-lock.json | 2 +- package.json | 4 ++-- staging/refresh.sh | 1 - staging/setup.sh | 2 -- staging/src/fixme-2.js | 3 --- staging/src/fixme-3.js | 7 ------- staging/src/fixme.js | 6 ------ staging/src/no-fix.js | 7 ------- tests/cli-test.js | 16 ---------------- tests/lib/rules/fix-result-1.js | 8 ++++++++ tests/lib/rules/fix-result-2.js | 11 +++++++++++ tests/lib/rules/notice.js | 8 +++++--- 13 files changed, 37 insertions(+), 49 deletions(-) delete mode 100644 staging/refresh.sh delete mode 100644 staging/setup.sh delete mode 100644 staging/src/fixme-2.js delete mode 100644 staging/src/fixme-3.js delete mode 100644 staging/src/fixme.js delete mode 100644 staging/src/no-fix.js delete mode 100644 tests/cli-test.js create mode 100644 tests/lib/rules/fix-result-1.js create mode 100644 tests/lib/rules/fix-result-2.js diff --git a/index.js b/index.js index 2c436a7..5a63c89 100644 --- a/index.js +++ b/index.js @@ -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` }; diff --git a/package-lock.json b/package-lock.json index da44545..4a043bd 100644 --- a/package-lock.json +++ b/package-lock.json @@ -1,6 +1,6 @@ { "name": "eslint-plugin-notice", - "version": "0.0.1", + "version": "0.2.3", "lockfileVersion": 1, "dependencies": { "acorn": { diff --git a/package.json b/package.json index aa4d8da..32a5e41 100644 --- a/package.json +++ b/package.json @@ -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", diff --git a/staging/refresh.sh b/staging/refresh.sh deleted file mode 100644 index 58ca6d1..0000000 --- a/staging/refresh.sh +++ /dev/null @@ -1 +0,0 @@ -npm i .. -g \ No newline at end of file diff --git a/staging/setup.sh b/staging/setup.sh deleted file mode 100644 index 797bb9a..0000000 --- a/staging/setup.sh +++ /dev/null @@ -1,2 +0,0 @@ -npm i eslint -g -npm i .. -g \ No newline at end of file diff --git a/staging/src/fixme-2.js b/staging/src/fixme-2.js deleted file mode 100644 index e293281..0000000 --- a/staging/src/fixme-2.js +++ /dev/null @@ -1,3 +0,0 @@ -function needsATop(){ - return "This file has no top"; -} \ No newline at end of file diff --git a/staging/src/fixme-3.js b/staging/src/fixme-3.js deleted file mode 100644 index f499448..0000000 --- a/staging/src/fixme-3.js +++ /dev/null @@ -1,7 +0,0 @@ -function curveBall(x){ - return x; -}; - -/** - * Now the first comment is down here - */ \ No newline at end of file diff --git a/staging/src/fixme.js b/staging/src/fixme.js deleted file mode 100644 index 2d87734..0000000 --- a/staging/src/fixme.js +++ /dev/null @@ -1,6 +0,0 @@ -/** - * Not exactly what I was looking for - */ -function needsTheCorrectTop(){ - return "This file has the incorrect top"; -} \ No newline at end of file diff --git a/staging/src/no-fix.js b/staging/src/no-fix.js deleted file mode 100644 index 1696087..0000000 --- a/staging/src/no-fix.js +++ /dev/null @@ -1,7 +0,0 @@ -/** - * Copyright (c) 2017, Nick Deis - * All rights reserved. - */ -function iShouldNotChange(){ - return "I'm perfect" -} \ No newline at end of file diff --git a/tests/cli-test.js b/tests/cli-test.js deleted file mode 100644 index 3cba0b5..0000000 --- a/tests/cli-test.js +++ /dev/null @@ -1,16 +0,0 @@ -const CLIEngine = require("eslint").CLIEngine; -const path = require("path"); -var cli = new CLIEngine( - { - useEslintrc: false, - rules:{ - notice:["error",{"mustMatch":"[0-9]{0,4}, Nick Deis","templateFile":path.join(__dirname,"../tests/test-template.js")}] - }, - fix:true, - rulePaths:[path.resolve(__dirname,"..")] - } -); - -const report = cli.executeOnFiles([path.resolve(__dirname,"../staging/src")]); - -console.log(JSON.stringify(report,null,2)); \ No newline at end of file diff --git a/tests/lib/rules/fix-result-1.js b/tests/lib/rules/fix-result-1.js new file mode 100644 index 0000000..3cbff78 --- /dev/null +++ b/tests/lib/rules/fix-result-1.js @@ -0,0 +1,8 @@ + +/** + * Copyright (c) 2017, Nick Deis + */ + +function noStyle(){ + return "I didn't read the style guide :("; +} diff --git a/tests/lib/rules/fix-result-2.js b/tests/lib/rules/fix-result-2.js new file mode 100644 index 0000000..4352ac0 --- /dev/null +++ b/tests/lib/rules/fix-result-2.js @@ -0,0 +1,11 @@ + +/** + * Copyright (c) 2017, Nick Deis + */ + +/** + * Not exactly what I was looking for + */ +function leastYouTried(){ + return false; +} diff --git a/tests/lib/rules/notice.js b/tests/lib/rules/notice.js index fe05853..ea4977c 100644 --- a/tests/lib/rules/notice.js +++ b/tests/lib/rules/notice.js @@ -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(); @@ -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:[{