Skip to content

Instantly share code, notes, and snippets.

@aleksclark
Forked from zorji/auto-ts-ignore.ts
Last active December 21, 2023 20:10
Show Gist options
  • Save aleksclark/7020fb1df89f84a8459c40bf0cf90d78 to your computer and use it in GitHub Desktop.
Save aleksclark/7020fb1df89f84a8459c40bf0cf90d78 to your computer and use it in GitHub Desktop.
A script to auto add // eslint-disable-next-line or /* eslint ... to files that don't lint
/**
* A script to auto add // eslint-disable-next-line or /* eslint ... to files that don't lint
* Example usage:
* $ yarn eslint --rule '@typescript-eslint/ban-ts-comment: error' --quiet -f json -o eslint_errors.json src/ tests/
* $ yarn ts-node auto-eslint-ignore.ts eslint_errors.json
*/
import { readFile, writeFile } from 'fs/promises';
import { uniq } from 'lodash';
const errorLogFile = process.argv[2];
interface ErrorMessage {
ruleId: string;
severity: number;
message: string;
line: number;
column: number;
}
interface JSONLintError {
filePath: string;
messages: ErrorMessage[];
}
const GLOBAL_LIMIT = 5;
async function main() {
const errorLog = JSON.parse(await readFile(errorLogFile, { encoding: 'utf-8' })) as JSONLintError[];
for (const fileError of errorLog) {
const fileContent = (await readFile(fileError.filePath, { encoding: 'utf-8' })).split('\n');
const errorCounters: Record<string, number> = {};
fileError.messages.forEach(msg => {
errorCounters[msg.ruleId] ||= 0;
errorCounters[msg.ruleId] += 1;
});
const globalErrors: string[] = [];
for (const msg of fileError.messages.reverse()) {
if (errorCounters[msg.ruleId] < GLOBAL_LIMIT) {
const lineContent: string = fileContent[msg.line - 1];
const numLeadingSpaces = lineContent.search(/\S/);
const ignoreContent = `${' '.repeat(numLeadingSpaces)}// eslint-disable-next-line ${msg.ruleId}`;
fileContent.splice(msg.line - 1, 0, ignoreContent);
} else {
globalErrors.push(msg.ruleId);
}
}
for (const ruleId of uniq(globalErrors)) {
const ignoreContent = `/* eslint ${ruleId} : 0 */`;
fileContent.splice(0, 0, ignoreContent);
}
await writeFile(fileError.filePath, fileContent.join('\n'), { encoding: 'utf-8' });
}
}
main();
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment