Skip to content

Commit

Permalink
#5: no such file or directory
Browse files Browse the repository at this point in the history
Summary: Will try to use the filename of the source to search for the root, will print a more useful error message on failure
  • Loading branch information
nickdeis committed Feb 16, 2018
1 parent 9c494c9 commit 9c3b7bb
Show file tree
Hide file tree
Showing 7 changed files with 51 additions and 26 deletions.
13 changes: 7 additions & 6 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,8 @@ module.exports = {
},
create(context) {
const { resolvedTemplate, mustMatch, chars, onNonMatchingHeader, nonMatchingTolerance } = resolveOptions(
context.options[0]
context.options[0],
context.getFilename()
);
const sourceCode = context.getSourceCode();
const text = sourceCode.getText().substring(0, chars);
Expand All @@ -42,16 +43,16 @@ module.exports = {
topNode = node;
}
let headerMatches = false;
if(!headerMatches && mustMatch && text){
if (!headerMatches && mustMatch && text) {
headerMatches = !!String(text).match(mustMatch);
//If the header matches, return early
if(headerMatches) return;
}
if (headerMatches) return;
}
//If chars doesn't match, a header comment/template exists and nonMatchingTolerance is set, try calculating string distance
if (!headerMatches && hasHeaderComment && resolvedTemplate && _.isNumber(nonMatchingTolerance)) {
const dist = metriclcs(resolvedTemplate,firstComment.value);
const dist = metriclcs(resolvedTemplate, firstComment.value);
//Return early, mark as true for future work if needed
if(nonMatchingTolerance <= dist){
if (nonMatchingTolerance <= dist) {
headerMatches = true;
return;
}
Expand Down
7 changes: 6 additions & 1 deletion package-lock.json

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

3 changes: 2 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "eslint-plugin-notice",
"version": "0.5.6",
"version": "0.6.6",
"description": "An eslint rule that checks the top of files and --fix them too!",
"main": "index.js",
"directories": {
Expand All @@ -25,6 +25,7 @@
"eslint": "^4.10.0"
},
"dependencies": {
"find-root": "^1.1.0",
"lodash": ">=2.4.0",
"metric-lcs": "^0.1.2"
},
Expand Down
2 changes: 1 addition & 1 deletion tests/lib/rules/fix-result-1.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@

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

function noStyle(){
Expand Down
2 changes: 1 addition & 1 deletion tests/lib/rules/fix-result-2.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@

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

/**
Expand Down
2 changes: 1 addition & 1 deletion tests/lib/rules/fix-result-3.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@

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


Expand Down
48 changes: 33 additions & 15 deletions utils.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,9 @@
*/

const _ = require("lodash"),
fs = require("fs");
fs = require("fs"),
findRoot = require("find-root"),
path = require("path");

const COULD_NOT_FIND = `Could not find a match for the mustMatch pattern`;
const REPORT_AND_SKIP = `Found a header comment which did not match the mustMatch pattern, skipping fix and reporting`;
Expand All @@ -26,16 +28,35 @@ function regexpizeTemplate({ template, varRegexps }) {
return new RegExp(_.template(escapeRegExp(template))(allPatternVars));
}

function resolveOptions({
mustMatch,
templateFile,
template,
templateVars,
chars,
onNonMatchingHeader,
varRegexps,
nonMatchingTolerance
}) {
function resolveTemplate({ templateFile, template, fileName }) {

if(template) return template;
if(!templateFile){
throw new Error(`Either template or templateFile must be set`);
}
//Naively look for the templateFile first
if(fs.existsSync(templateFile)){
return fs.readFileSync(templateFile, "utf8");
}
if(!fs.existsSync(fileName)){
throw new Error(`Could not find the file name ${fileName}. This is necessary to find the root`);
}
const root = findRoot(fileName);
const rootTemplateFile = path.join(root,templateFile);
if(fs.existsSync(rootTemplateFile)){
return fs.readFileSync(rootTemplateFile, "utf8");
}
const absRootTemplateFile = path.resolve(rootTemplateFile);
if(fs.existsSync(absRootTemplateFile)){
return fs.readFileSync(absRootTemplateFile,"utf8");
}
throw new Error(`Can't find templateFile @ ${absRootTemplateFile}`);
}

function resolveOptions(
{ mustMatch, templateFile, template, templateVars, chars, onNonMatchingHeader, varRegexps, nonMatchingTolerance },
fileName
) {
onNonMatchingHeader = onNonMatchingHeader || "prepend";
templateVars = templateVars || {};
varRegexps = varRegexps || {};
Expand All @@ -48,10 +69,7 @@ function resolveOptions({
} else if (!(mustMatch instanceof RegExp)) {
mustMatch = new RegExp(mustMatch);
}

if (!template && templateFile) {
template = fs.readFileSync(templateFile, "utf8");
}
template = resolveTemplate({ templateFile, template, fileName });

const YEAR = new Date().getFullYear();
const allVars = Object.assign({}, { YEAR }, templateVars);
Expand Down

0 comments on commit 9c3b7bb

Please sign in to comment.