Speed up lint-deps

This commit is contained in:
Evan Hahn 2020-09-11 16:33:54 -05:00 committed by Josh Perez
parent 7ee6584d8b
commit 401cdfdb63

View file

@ -1,26 +1,27 @@
// tslint:disable no-console
import { readFileSync } from 'fs';
import * as fs from 'fs';
import { join, relative } from 'path';
import normalizePath from 'normalize-path';
import { sync as fgSync } from 'fast-glob';
import { forEach, some, values } from 'lodash';
import pMap from 'p-map';
import FastGlob from 'fast-glob';
import { ExceptionType, REASONS, RuleType } from './types';
import { ENCODING, loadJSON, sortExceptions } from './util';
const ALL_REASONS = REASONS.join('|');
const now = new Date();
function getExceptionKey(exception: any) {
function getExceptionKey(
exception: Pick<ExceptionType, 'rule' | 'path' | 'lineNumber'>
): string {
return `${exception.rule}-${exception.path}-${exception.lineNumber}`;
}
function createLookup(list: Array<any>) {
function createLookup(
list: ReadonlyArray<ExceptionType>
): { [key: string]: ExceptionType } {
const lookup = Object.create(null);
forEach(list, exception => {
list.forEach((exception: ExceptionType) => {
const key = getExceptionKey(exception);
if (lookup[key]) {
@ -39,16 +40,7 @@ const basePath = join(__dirname, '../../..');
const searchPattern = normalizePath(join(basePath, '**/*.{js,ts,tsx}'));
const rules: Array<RuleType> = loadJSON(rulesPath);
const exceptions: Array<ExceptionType> = loadJSON(exceptionsPath);
const exceptionsLookup = createLookup(exceptions);
let scannedCount = 0;
const allSourceFiles = fgSync(searchPattern, { onlyFiles: true });
const results: Array<ExceptionType> = [];
const excludedFiles = [
const excludedFilesRegexps = [
// Non-distributed files
'\\.d\\.ts$',
@ -295,10 +287,10 @@ const excludedFiles = [
'^node_modules/webpack-hot-middleware/.+',
'^node_modules/webpack-merge/.+',
'^node_modules/webpack/.+',
];
].map(str => new RegExp(str));
function setupRules(allRules: Array<RuleType>) {
forEach(allRules, (rule, index) => {
allRules.forEach((rule: RuleType, index: number) => {
if (!rule.name) {
throw new Error(`Rule at index ${index} is missing a name`);
}
@ -311,34 +303,41 @@ function setupRules(allRules: Array<RuleType>) {
});
}
async function main(): Promise<void> {
const now = new Date();
const rules: Array<RuleType> = loadJSON(rulesPath);
setupRules(rules);
forEach(allSourceFiles, file => {
const relativePath = relative(basePath, file).replace(/\\/g, '/');
if (
some(excludedFiles, excluded => {
const regex = new RegExp(excluded);
const exceptions: Array<ExceptionType> = loadJSON(exceptionsPath);
const exceptionsLookup = createLookup(exceptions);
return regex.test(relativePath);
})
) {
const results: Array<ExceptionType> = [];
let scannedCount = 0;
await pMap(
await FastGlob(searchPattern, { onlyFiles: true }),
async (file: string) => {
const relativePath = relative(basePath, file).replace(/\\/g, '/');
const isFileExcluded = excludedFilesRegexps.some(excludedRegexp =>
excludedRegexp.test(relativePath)
);
if (isFileExcluded) {
return;
}
scannedCount += 1;
const fileContents = readFileSync(file, ENCODING);
const lines = fileContents.split('\n');
const lines = (await fs.promises.readFile(file, ENCODING)).split(/\r?\n/);
forEach(rules, (rule: RuleType) => {
rules.forEach((rule: RuleType) => {
const excludedModules = rule.excludedModules || [];
if (some(excludedModules, module => relativePath.startsWith(module))) {
if (excludedModules.some(module => relativePath.startsWith(module))) {
return;
}
forEach(lines, (rawLine, lineIndex) => {
const line = rawLine.replace(/\r/g, '');
lines.forEach((line: string, lineIndex: number) => {
if (!rule.regex.test(line)) {
return;
}
@ -347,7 +346,6 @@ forEach(allSourceFiles, file => {
rule.regex = new RegExp(rule.expression, 'g');
}
const path = relativePath;
const lineNumber = lineIndex + 1;
const exceptionKey = getExceptionKey({
@ -366,7 +364,7 @@ forEach(allSourceFiles, file => {
results.push({
rule: rule.name,
path,
path: relativePath,
line: line.length < 300 ? line : undefined,
lineNumber,
reasonCategory: ALL_REASONS,
@ -375,9 +373,12 @@ forEach(allSourceFiles, file => {
});
});
});
});
},
// Without this, we may run into "too many open files" errors.
{ concurrency: 100 }
);
const unusedExceptions = values(exceptionsLookup);
const unusedExceptions = Object.values(exceptionsLookup);
console.log(
`${scannedCount} files scanned.`,
@ -401,3 +402,9 @@ if (unusedExceptions.length) {
}
process.exit(1);
}
main().catch(err => {
console.error(err);
process.exit(1);
});