Add option to auto-remove unused lint-deps exceptions
This commit is contained in:
parent
41b4cce6ec
commit
c2a65306e2
3 changed files with 43 additions and 39 deletions
|
@ -10,7 +10,7 @@ import FastGlob from 'fast-glob';
|
||||||
|
|
||||||
import type { ExceptionType, RuleType } from './types';
|
import type { ExceptionType, RuleType } from './types';
|
||||||
import { REASONS } from './types';
|
import { REASONS } from './types';
|
||||||
import { ENCODING, loadJSON, sortExceptions } from './util';
|
import { ENCODING, loadJSON, sortExceptions, writeExceptions } from './util';
|
||||||
|
|
||||||
const ALL_REASONS = REASONS.join('|');
|
const ALL_REASONS = REASONS.join('|');
|
||||||
|
|
||||||
|
@ -321,7 +321,11 @@ function setupRules(allRules: Array<RuleType>) {
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
async function main(): Promise<void> {
|
async function main(argv: ReadonlyArray<string>): Promise<void> {
|
||||||
|
const shouldRemoveUnusedExceptions = argv.includes(
|
||||||
|
'--remove-unused-exceptions'
|
||||||
|
);
|
||||||
|
|
||||||
const now = new Date();
|
const now = new Date();
|
||||||
|
|
||||||
const rules: Array<RuleType> = loadJSON(rulesPath);
|
const rules: Array<RuleType> = loadJSON(rulesPath);
|
||||||
|
@ -393,10 +397,26 @@ async function main(): Promise<void> {
|
||||||
{ concurrency: 100 }
|
{ concurrency: 100 }
|
||||||
);
|
);
|
||||||
|
|
||||||
|
let unusedExceptionsLogMessage: string;
|
||||||
|
|
||||||
|
if (shouldRemoveUnusedExceptions && unusedExceptions.length) {
|
||||||
|
unusedExceptionsLogMessage = `${unusedExceptions.length} unused exceptions (automatically removed),`;
|
||||||
|
|
||||||
|
const unusedExceptionsSet = new Set(unusedExceptions);
|
||||||
|
const newExceptions = exceptions.filter(
|
||||||
|
exception => !unusedExceptionsSet.has(exception)
|
||||||
|
);
|
||||||
|
writeExceptions(exceptionsPath, newExceptions);
|
||||||
|
|
||||||
|
unusedExceptions = [];
|
||||||
|
} else {
|
||||||
|
unusedExceptionsLogMessage = `${unusedExceptions.length} unused exceptions,`;
|
||||||
|
}
|
||||||
|
|
||||||
console.log(
|
console.log(
|
||||||
`${scannedCount} files scanned.`,
|
`${scannedCount} files scanned.`,
|
||||||
`${results.length} questionable lines,`,
|
`${results.length} questionable lines,`,
|
||||||
`${unusedExceptions.length} unused exceptions,`,
|
unusedExceptionsLogMessage,
|
||||||
`${exceptions.length} total exceptions.`
|
`${exceptions.length} total exceptions.`
|
||||||
);
|
);
|
||||||
|
|
||||||
|
@ -410,14 +430,16 @@ async function main(): Promise<void> {
|
||||||
|
|
||||||
if (unusedExceptions.length) {
|
if (unusedExceptions.length) {
|
||||||
console.log();
|
console.log();
|
||||||
console.log('Unused exceptions!');
|
console.log(
|
||||||
|
'Unused exceptions! Run with --remove-unused-exceptions to automatically remove them.'
|
||||||
|
);
|
||||||
console.log(JSON.stringify(sortExceptions(unusedExceptions), null, ' '));
|
console.log(JSON.stringify(sortExceptions(unusedExceptions), null, ' '));
|
||||||
}
|
}
|
||||||
|
|
||||||
process.exit(1);
|
process.exit(1);
|
||||||
}
|
}
|
||||||
|
|
||||||
main().catch(err => {
|
main(process.argv).catch(err => {
|
||||||
console.error(err);
|
console.error(err);
|
||||||
process.exit(1);
|
process.exit(1);
|
||||||
});
|
});
|
||||||
|
|
|
@ -1,15 +1,12 @@
|
||||||
// Copyright 2018-2020 Signal Messenger, LLC
|
// Copyright 2018-2022 Signal Messenger, LLC
|
||||||
// SPDX-License-Identifier: AGPL-3.0-only
|
// SPDX-License-Identifier: AGPL-3.0-only
|
||||||
|
|
||||||
import { join } from 'path';
|
import { join } from 'path';
|
||||||
import { writeFileSync } from 'fs';
|
|
||||||
|
|
||||||
import type { ExceptionType } from './types';
|
import type { ExceptionType } from './types';
|
||||||
import { loadJSON, sortExceptions } from './util';
|
import { loadJSON, writeExceptions } from './util';
|
||||||
|
|
||||||
const exceptionsPath = join(__dirname, 'exceptions.json');
|
const exceptionsPath = join(__dirname, 'exceptions.json');
|
||||||
const exceptions: Array<ExceptionType> = loadJSON(exceptionsPath);
|
const exceptions: Array<ExceptionType> = loadJSON(exceptionsPath);
|
||||||
|
|
||||||
const sorted = sortExceptions(exceptions);
|
writeExceptions(exceptionsPath, exceptions);
|
||||||
|
|
||||||
writeFileSync(exceptionsPath, JSON.stringify(sorted, null, ' '));
|
|
||||||
|
|
|
@ -1,43 +1,28 @@
|
||||||
// Copyright 2018-2021 Signal Messenger, LLC
|
// Copyright 2018-2022 Signal Messenger, LLC
|
||||||
// SPDX-License-Identifier: AGPL-3.0-only
|
// SPDX-License-Identifier: AGPL-3.0-only
|
||||||
|
|
||||||
/* eslint-disable no-console */
|
import { readJsonSync, writeJsonSync } from 'fs-extra';
|
||||||
|
|
||||||
import { readFileSync } from 'fs';
|
import { orderBy } from 'lodash';
|
||||||
|
|
||||||
import { omit, orderBy } from 'lodash';
|
|
||||||
|
|
||||||
import type { ExceptionType } from './types';
|
import type { ExceptionType } from './types';
|
||||||
|
|
||||||
export const ENCODING = 'utf8';
|
export const ENCODING = 'utf8';
|
||||||
|
|
||||||
export function loadJSON<T>(target: string): T {
|
export const loadJSON = <T>(path: string): T => readJsonSync(path);
|
||||||
try {
|
|
||||||
const contents = readFileSync(target, ENCODING);
|
|
||||||
|
|
||||||
return JSON.parse(contents);
|
export const writeExceptions = (
|
||||||
} catch (error) {
|
path: string,
|
||||||
console.log(`Error loading JSON from ${target}: ${error.stack}`);
|
exceptions: ReadonlyArray<ExceptionType>
|
||||||
throw error;
|
): void => writeJsonSync(path, sortExceptions(exceptions), { spaces: 2 });
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
export function sortExceptions(
|
export const sortExceptions = (
|
||||||
exceptions: Array<ExceptionType>
|
exceptions: ReadonlyArray<ExceptionType>
|
||||||
): Array<ExceptionType> {
|
): Array<ExceptionType> =>
|
||||||
return orderBy(exceptions, [
|
orderBy(exceptions, [
|
||||||
'path',
|
'path',
|
||||||
'rule',
|
'rule',
|
||||||
'reasonCategory',
|
'reasonCategory',
|
||||||
'updated',
|
'updated',
|
||||||
'reasonDetail',
|
'reasonDetail',
|
||||||
]).map(removeLegacyAttributes);
|
]);
|
||||||
}
|
|
||||||
|
|
||||||
// This is here in case any open changesets still touch `lineNumber`. We should remove
|
|
||||||
// this after 2021-06-01 to be conservative.
|
|
||||||
function removeLegacyAttributes(
|
|
||||||
exception: Readonly<ExceptionType>
|
|
||||||
): ExceptionType {
|
|
||||||
return omit(exception, ['lineNumber']);
|
|
||||||
}
|
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue