UNPKG

diffjam

Version:
90 lines (87 loc) 4.56 kB
"use strict"; var __importDefault = (this && this.__importDefault) || function (mod) { return (mod && mod.__esModule) ? mod : { "default": mod }; }; Object.defineProperty(exports, "__esModule", { value: true }); exports.findMatches = exports.FileMatcher = void 0; /* Finds matches for a given set of `needles` to search for, calling `onMatch` for each match found. The `needles` has a primary regex with the global and multiline flags, so we cycle through each of its matches using `.exec`. Any other regexes in the `needles` are used to further filter whether the first line matched in meets other terms. If this is indeed a match, we compute the line number and column number as well as format the match for display. */ var chalk_1 = __importDefault(require("chalk")); var lodash_1 = require("lodash"); var newLineRegExp = new RegExp("\n", "gm"); var FileMatcher = /** @class */ (function () { function FileMatcher(path, contents) { this.path = path; this.contents = contents; this.newLineIndexes = []; var newLineRegExpMatch; while ((newLineRegExpMatch = newLineRegExp.exec(contents))) { this.newLineIndexes.push(newLineRegExpMatch.index); } } FileMatcher.prototype.findMatches = function (needles, onMatch) { var regexMatch; var _loop_1 = function () { var found = regexMatch[0]; var foundStartIndex = regexMatch.index; var foundLength = found.length; var foundEndIndex = foundStartIndex + foundLength; var startLineNumber = this_1.newLineIndexes.length && (foundStartIndex > (0, lodash_1.last)(this_1.newLineIndexes) ? this_1.newLineIndexes.length : this_1.newLineIndexes.findIndex(function (index) { return index > foundStartIndex; })); var startLineIndex = startLineNumber === 0 ? 0 : this_1.newLineIndexes[startLineNumber - 1] + 1; var startLineEndsIndex = this_1.newLineIndexes[startLineNumber]; var foundOnStartLine = foundEndIndex > startLineEndsIndex ? this_1.contents.substring(foundStartIndex, startLineEndsIndex) : found; var beforeInSameLine = this_1.contents.substring(startLineIndex, foundStartIndex); var afterInSameLine = foundEndIndex > startLineEndsIndex ? "" : this_1.contents.substring(foundStartIndex + foundLength, this_1.newLineIndexes[startLineNumber] || this_1.contents.length); var startWholeLine = beforeInSameLine + foundOnStartLine + afterInSameLine; if (needles.negative.some(function (term) { return startWholeLine.includes(term); })) { return "continue"; } if (!needles.positive.every(function (term) { return startWholeLine.includes(term); })) { return "continue"; } if (!needles.otherRegexes.every(function (term) { return term.test(startWholeLine); })) { return "continue"; } var endLineNumber = this_1.newLineIndexes.length && (1 + (0, lodash_1.findLastIndex)(this_1.newLineIndexes, function (index) { return index < foundEndIndex; })); var endLineIndex = endLineNumber === 0 ? 0 : this_1.newLineIndexes[endLineNumber - 1] + 1; var startColumn = foundStartIndex - startLineIndex; var endColumn = foundEndIndex - endLineIndex; var startWholeLineFormatted = beforeInSameLine + chalk_1.default.bold(foundOnStartLine) + afterInSameLine; var breachPath = "".concat(this_1.path, "(").concat(startLineNumber + 1, ":").concat(startColumn + 1, ")"); onMatch({ startLineNumber: startLineNumber, endLineNumber: endLineNumber, startColumn: startColumn, endColumn: endColumn, found: found, startWholeLine: startWholeLine, startWholeLineFormatted: startWholeLineFormatted, breachPath: breachPath, path: this_1.path, }); }; var this_1 = this; while (regexMatch = needles.regex.exec(this.contents)) { _loop_1(); } }; return FileMatcher; }()); exports.FileMatcher = FileMatcher; function findMatches(fileContents, needles) { var fileMatcher = new FileMatcher("", fileContents); var matches = []; fileMatcher.findMatches(needles, function (match) { return matches.push(match); }); return matches; } exports.findMatches = findMatches;