UNPKG

@lint-todo/utils

Version:

![CI Build](https://github.com/lint-todo/utils/workflows/CI%20Build/badge.svg) [![npm version](https://badge.fury.io/js/%40lint-todo%2Futils.svg)](https://badge.fury.io/js/%40lint-todo%2Futils) [![License](https://img.shields.io/npm/l/@checkup/cli.svg)](h

116 lines 4.71 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); const date_utils_1 = require("./date-utils"); function copyLintResult(todoDatum, unmatchedTodoData) { // this is a key transfer of information that allows us to match the identify // of the original lint result to the todo data. This is important as it allows // us to subsequently modify the severity of the original lint result. This is // only required for todo data that is generated for the stable batch, as those // are the only ones that are use to match back to the original lint result. todoDatum.originalLintResult = unmatchedTodoData.originalLintResult; } /** * Creates todo batches based on lint results. */ class TodoBatchGenerator { /** * Create a TodoBatchGenerator * * @param options - An object containing write options. */ constructor(options) { this.options = options; } /** * Matches todos to their associated {@link https://github.com/lint-todo/utils/blob/master/src/types/todo.ts#L61|TodoData} object. * * The matching algorithm uses the following logic: * * For each unmatched lint result * Find associated TodoMatcher by filePath * Try to exact match against any of the TodoMatcher's Todos * * if is exact match * if expired && shouldRemove then add to "expired" * else then add to "stable" * remove from unmatched for either case above * * For each remaining unmatched lint result * Find associated TodoMatcher by filePath * Try to fuzzy match against any of the TodoMatcher's Todos * * if is fuzzy match * if expired && shouldRemove then add to "expired" * else then add to "stable" * remove from unmatched for either case above * else * then add to "add" * * For each remaining existing todos * if shouldRemove then add to "remove * * Exact matches match on engine, ruleID, line and column * Fuzzy matches match on engine, ruleID and source * * @param maybeTodos - The linting data, converted to TodoData format. * @param existingTodos - Existing todo lint data. * @returns */ generate(maybeTodos, existingTodos) { var _a, _b, _c, _d, _e; const add = new Set(); const expired = new Set(); const stable = new Set(); let remove = new Set(); maybeTodos = new Set(maybeTodos); for (const unmatchedTodoData of maybeTodos) { const matcher = existingTodos.get(unmatchedTodoData.filePath); if (matcher) { const todoDatum = matcher.exactMatch(unmatchedTodoData); if (todoDatum) { if ((0, date_utils_1.isExpired)(todoDatum.errorDate) && ((_b = (_a = this.options) === null || _a === void 0 ? void 0 : _a.shouldRemove) === null || _b === void 0 ? void 0 : _b.call(_a, todoDatum))) { expired.add(todoDatum); } else { copyLintResult(todoDatum, unmatchedTodoData); stable.add(todoDatum); } maybeTodos.delete(unmatchedTodoData); } } } for (const unmatchedTodoData of maybeTodos) { const matcher = existingTodos.get(unmatchedTodoData.filePath); if (matcher) { const todoDatum = matcher.fuzzyMatch(unmatchedTodoData); if (todoDatum) { if ((0, date_utils_1.isExpired)(todoDatum.errorDate) && ((_d = (_c = this.options) === null || _c === void 0 ? void 0 : _c.shouldRemove) === null || _d === void 0 ? void 0 : _d.call(_c, todoDatum))) { expired.add(todoDatum); } else { copyLintResult(todoDatum, unmatchedTodoData); stable.add(todoDatum); } } else { add.add(unmatchedTodoData); } } else { add.add(unmatchedTodoData); } maybeTodos.delete(unmatchedTodoData); } for (const matcher of existingTodos.values()) { remove = new Set([...remove, ...matcher.unmatched((_e = this.options) === null || _e === void 0 ? void 0 : _e.shouldRemove)]); } return { add, expired, stable, remove, }; } } exports.default = TodoBatchGenerator; //# sourceMappingURL=todo-batch-generator.js.map