Skip to content

Commit

Permalink
Fixes for issue #2 and #3
Browse files Browse the repository at this point in the history
  • Loading branch information
nickdeis committed Aug 28, 2017
1 parent 19a166a commit 429ff21
Show file tree
Hide file tree
Showing 4 changed files with 63 additions and 13 deletions.
20 changes: 11 additions & 9 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,11 +10,16 @@ const fs = require("fs"),
utils = require("./utils");

const NON_MATCHING_HEADER_ACTIONS = ["prepend", "replace", "report"];
const YEAR_REGEXP = /20\d{2}/;
const { escapeRegExp } = utils;
const { regexpizeTemplate } = utils;



function resolveOptions({ mustMatch, templateFile, template, templateVars, chars, onNonMatchingHeader, varRegexps }) {
onNonMatchingHeader = onNonMatchingHeader || "prepend";
templateVars = templateVars || {};
varRegexps = varRegexps || {};
chars = chars || 1000;

let mustMatchTemplate = false;
if (!mustMatch) {
mustMatchTemplate = true;
Expand All @@ -26,16 +31,12 @@ function resolveOptions({ mustMatch, templateFile, template, templateVars, chars
template = fs.readFileSync(templateFile, "utf8");
}

templateVars = templateVars || {};
chars = chars || 1000;
const YEAR = new Date().getFullYear();
const allVars = Object.assign({}, { YEAR }, templateVars);

if (mustMatchTemplate && template) {
//create mustMatch from varRegexps and template
varRegexps = varRegexps || {};
const allRegexpVars = Object.assign({}, { YEAR: YEAR_REGEXP }, varRegexps);
mustMatch = new RegExp(_.template(escapeRegExp(template))(allRegexpVars));
mustMatch = regexpizeTemplate({ template, varRegexps });
} else if (!template && mustMatchTemplate) {
throw new Error("Either mustMatch, template, or templateFile must be set");
}
Expand Down Expand Up @@ -71,6 +72,7 @@ module.exports = {
} else {
topNode = node;
}
const headerMatches = !!String(text).match(mustMatch);
//Select fixer based off onNonMatchingHeader
let fix;
//If there is no template, then there can be no fix.
Expand All @@ -83,15 +85,15 @@ module.exports = {
} else if (hasHeaderComment && onNonMatchingHeader === "replace") {
fix = fixer => fixer.replaceText(topNode, resolvedTemplate);
//report and skip
} else if (hasHeaderComment && onNonMatchingHeader === "report") {
} else if (hasHeaderComment && onNonMatchingHeader === "report" && !headerMatches) {
const report = {
node,
message: utils.REPORT_AND_SKIP
};
context.report(report);
return;
}
if (!String(text).match(mustMatch)) {
if (!headerMatches) {
const report = { node, message: utils.COULD_NOT_FIND, fix };
context.report(report);
return;
Expand Down
6 changes: 3 additions & 3 deletions package.json
Original file line number Diff line number Diff line change
@@ -1,13 +1,13 @@
{
"name": "eslint-plugin-notice",
"version": "0.4.4",
"version": "0.4.5",
"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"
"test": "node tests/lib/rules/notice.js && node tests/test-utils.js"
},
"keywords": [
"eslint",
Expand All @@ -25,7 +25,7 @@
"eslint": ">=3.0.0"
},
"dependencies": {
"lodash": ">=1.0.0"
"lodash": ">=2.4.0"
},
"peerDependencies": {
"eslint": ">=3.0.0"
Expand Down
35 changes: 35 additions & 0 deletions tests/test-utils.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
const { regexpizeTemplate } = require("../utils");
const assert = require("assert");

const template = `
/**
* Copyright (c) <%= YEAR %>, <%= NAME %>
*/
`;

const header1 = `
/**
* Copyright (c) 2017, Nick Deis
*/
`;

const header2 = `
/**
* Copyright (c) 2016, Nicholas Deis
*/
`;

const HEADERS = [header1, header2];

function testRegepizeTemplate() {
const varRegexps = { NAME: /(Nick|Nicholas) Deis/ };
const mustMatch = regexpizeTemplate({ template, varRegexps });
console.log(mustMatch);
HEADERS.forEach(header => {
if (!header.match(mustMatch)) {
throw new Error(`Expected ${header} to match ${mustMatch}`);
}
});
}

testRegepizeTemplate();
15 changes: 14 additions & 1 deletion utils.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,13 +2,26 @@
* Shared lib between rule and tests
*/

const _ = require("lodash");

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`;

const ESCAPE = /[\-\[\]\/\{\}\(\)\*\+\?\.\\\^\$\|]/g;
const YEAR_REGEXP = /20\d{2}/;

function escapeRegExp(str) {
return String(str).replace(ESCAPE, "\\$&");
}

module.exports = { escapeRegExp, COULD_NOT_FIND, REPORT_AND_SKIP };
function stringifyRegexp(regexp) {
return regexp instanceof RegExp ? regexp.source : String(regexp);
}

function regexpizeTemplate({ template, varRegexps }) {
const allRegexpVars = Object.assign({}, { YEAR: YEAR_REGEXP }, varRegexps);
const allPatternVars = _.mapValues(allRegexpVars, stringifyRegexp);
return new RegExp(_.template(escapeRegExp(template))(allPatternVars));
}

module.exports = { regexpizeTemplate, COULD_NOT_FIND, REPORT_AND_SKIP };

0 comments on commit 429ff21

Please sign in to comment.