UNPKG

loose-ts-check

Version:

Run TS type-check and ignore certain errors in some files

60 lines (59 loc) 2.9 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.FilePathMatcher = void 0; const minimatch_1 = require("minimatch"); const ts_invariant_1 = require("ts-invariant"); class FilePathMatcher { constructor(filePaths) { this.regularFilePaths = new Set(); this.patternMatchersMap = new Map(); this.filePathMatchCountMap = new Map(filePaths.map((filePath) => [filePath, 0])); for (const filePath of filePaths) { // NOTE: different handling of paths is an optimization. // Having regular full file paths in a set lets us // check if a path matches any path in the set using O(1) // instead of O(n). // We only need to go through the patterns using O(n). if (containsMagicCharacters(filePath)) { const m = new minimatch_1.Minimatch(filePath); this.patternMatchersMap.set(filePath, m.match.bind(m)); } else { this.regularFilePaths.add(filePath); } } } matches(filePath) { const matchesRegularFilePath = this.regularFilePaths.has(filePath); if (matchesRegularFilePath) { const previousMatchesCount = this.filePathMatchCountMap.get(filePath); (0, ts_invariant_1.default)(typeof previousMatchesCount === 'number', `File path ${filePath} is in the regular file paths set but not in the match count map`); this.filePathMatchCountMap.set(filePath, previousMatchesCount + 1); } // NOTE: we need to go through all the matches anyway to check // if they match some files. This is necessary so we do not assume // these wildcard matches are useless when they matched files that // are also matched by regular full paths to these files. let somePatternMatched = false; for (const [pattern, patternMatches] of this.patternMatchersMap.entries()) { if (patternMatches(filePath)) { somePatternMatched = true; const previousMatchesCount = this.filePathMatchCountMap.get(pattern); (0, ts_invariant_1.default)(typeof previousMatchesCount === 'number', `Pttern ${pattern} is not in the match count map`); this.filePathMatchCountMap.set(pattern, previousMatchesCount + 1); } } return matchesRegularFilePath || somePatternMatched; } getUnusedFilePaths() { return Array.from(this.filePathMatchCountMap.entries()) .filter(([_name, matchesCount]) => matchesCount === 0) .map(([name]) => name); } } exports.FilePathMatcher = FilePathMatcher; function containsMagicCharacters(filePath) { // NOTE: magic characters interpreted by minimatch // @see https://www.npmjs.com/package/minimatch return /[\{\}\?\*]/.test(filePath); }