basant307/AI_Governance_Project
048
1/**2 * Shared lib between rule and tests3 */4 5const _ = require("lodash"),6 fs = require("fs"),7 findRoot = require("find-root"),8 path = require("path");9 10const COULD_NOT_FIND = `Missing notice header`;11const REPORT_AND_SKIP = `Found a header comment which did not have a notice header, skipping fix and reporting`;12const OUTSIDE_TOLERANCE = `Found a header comment which was too different from the required notice header (similarity={{ similarity }})`;13 14const DEFAULT_MESSAGE_CONFIG = {15 whenFailedToMatch: COULD_NOT_FIND,16 reportAndSkip: REPORT_AND_SKIP,17 whenOutsideTolerance: OUTSIDE_TOLERANCE18};19 20const ESCAPE = /[\-\[\]\/\{\}\(\)\*\+\?\.\\\^\$\|]/g;21const YEAR_REGEXP = /20\d{2}/;22const NON_MATCHING_HEADER_ACTIONS = ["prepend", "replace", "report"];23 24function escapeRegExp(str) {25 return String(str).replace(ESCAPE, "\\$&");26}27 28function stringifyRegexp(regexp) {29 return regexp instanceof RegExp ? regexp.source : String(regexp);30}31 32function regexpizeTemplate({ template, varRegexps }) {33 const allRegexpVars = Object.assign({}, { YEAR: YEAR_REGEXP }, varRegexps);34 const allPatternVars = _.mapValues(allRegexpVars, stringifyRegexp);35 return new RegExp(_.template(escapeRegExp(template))(allPatternVars));36}37 38function resolveTemplate({ templateFile, template, fileName }) {39 if (template) return template;40 //No template file, so move foward and disable --fix41 if (!templateFile) return null;42 //Naively look for the templateFile first43 if (fs.existsSync(templateFile)) {44 return fs.readFileSync(templateFile, "utf8");45 }46 if (!fs.existsSync(fileName)) {47 throw new Error(`Could not find the file name ${fileName}. This is necessary to find the root`);48 }49 const root = findRoot(fileName);50 const rootTemplateFile = path.join(root, templateFile);51 if (fs.existsSync(rootTemplateFile)) {52 return fs.readFileSync(rootTemplateFile, "utf8");53 }54 const absRootTemplateFile = path.resolve(rootTemplateFile);55 if (fs.existsSync(absRootTemplateFile)) {56 return fs.readFileSync(absRootTemplateFile, "utf8");57 }58 throw new Error(`Can't find templateFile @ ${absRootTemplateFile}`);59}60 61function resolveOptions(62 {63 mustMatch,64 templateFile,65 template,66 templateVars,67 chars,68 onNonMatchingHeader,69 varRegexps,70 nonMatchingTolerance,71 messages72 },73 fileName74) {75 onNonMatchingHeader = onNonMatchingHeader || "prepend";76 templateVars = templateVars || {};77 varRegexps = varRegexps || {};78 chars = chars || 1000;79 nonMatchingTolerance = nonMatchingTolerance || null;80 messages = Object.assign({}, DEFAULT_MESSAGE_CONFIG, messages || {});81 82 let mustMatchTemplate = false;83 if (!mustMatch) {84 mustMatchTemplate = true;85 } else if (!(mustMatch instanceof RegExp)) {86 mustMatch = new RegExp(mustMatch);87 }88 template = resolveTemplate({ templateFile, template, fileName });89 if(typeof template === 'string'){90 template = template.replace(/\r\n/g, "\n");91 }92 const YEAR = new Date().getFullYear();93 const allVars = Object.assign({}, { YEAR }, templateVars);94 if (mustMatchTemplate && template) {95 //create mustMatch from varRegexps and template96 mustMatch = regexpizeTemplate({ template, varRegexps });97 } else if (!template && mustMatchTemplate) {98 throw new Error("Either mustMatch, template, or templateFile must be set");99 }100 const resolvedTemplate = _.template(template)(allVars).replace(/\r\n/g, "\n");101 102 return { resolvedTemplate, mustMatch, chars, onNonMatchingHeader, nonMatchingTolerance, messages };103}104 105function createFixer({ resolvedTemplate, hasHeaderComment, topNode, onNonMatchingHeader }) {106 if (!resolvedTemplate) {107 return undefined;108 }109 if (!hasHeaderComment || (hasHeaderComment && onNonMatchingHeader === "prepend")) {110 return fixer => fixer.insertTextBeforeRange([0, 0], resolvedTemplate);111 }112 if (hasHeaderComment && onNonMatchingHeader === "replace") {113 return fixer => fixer.replaceText(topNode, resolvedTemplate);114 }115}116 117module.exports = { createFixer, regexpizeTemplate, COULD_NOT_FIND, REPORT_AND_SKIP, resolveOptions };118 