Remove lineNumber from lint exceptions

This commit is contained in:
Evan Hahn 2021-05-04 11:41:59 -05:00 committed by GitHub
parent fb00464033
commit 36d8ef9678
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
4 changed files with 50 additions and 2096 deletions

File diff suppressed because it is too large Load Diff

View File

@ -1,4 +1,4 @@
// Copyright 2018-2020 Signal Messenger, LLC
// Copyright 2018-2021 Signal Messenger, LLC
// SPDX-License-Identifier: AGPL-3.0-only
/* eslint-disable no-console */
@ -13,30 +13,6 @@ import { ENCODING, loadJSON, sortExceptions } from './util';
const ALL_REASONS = REASONS.join('|');
function getExceptionKey(
exception: Pick<ExceptionType, 'rule' | 'path' | 'lineNumber'>
): string {
return `${exception.rule}-${exception.path}-${exception.lineNumber}`;
}
function createLookup(
list: ReadonlyArray<ExceptionType>
): { [key: string]: ExceptionType } {
const lookup = Object.create(null);
list.forEach((exception: ExceptionType) => {
const key = getExceptionKey(exception);
if (lookup[key]) {
throw new Error(`Duplicate exception found for key ${key}`);
}
lookup[key] = exception;
});
return lookup;
}
const rulesPath = join(__dirname, 'rules.json');
const exceptionsPath = join(__dirname, 'exceptions.json');
const basePath = join(__dirname, '../../..');
@ -324,7 +300,7 @@ async function main(): Promise<void> {
setupRules(rules);
const exceptions: Array<ExceptionType> = loadJSON(exceptionsPath);
const exceptionsLookup = createLookup(exceptions);
let unusedExceptions = exceptions;
const results: Array<ExceptionType> = [];
let scannedCount = 0;
@ -351,7 +327,7 @@ async function main(): Promise<void> {
return;
}
lines.forEach((line: string, lineIndex: number) => {
lines.forEach((line: string) => {
if (!rule.regex.test(line)) {
return;
}
@ -361,30 +337,29 @@ async function main(): Promise<void> {
rule.regex = new RegExp(rule.expression, 'g');
}
const lineNumber = lineIndex + 1;
const matchedException = unusedExceptions.find(
exception =>
exception.rule === rule.name &&
exception.path === relativePath &&
(line.length < 300
? exception.line === line
: exception.line === undefined)
);
const exceptionKey = getExceptionKey({
rule: rule.name,
path: relativePath,
lineNumber,
});
const exception = exceptionsLookup[exceptionKey];
if (exception && (!exception.line || exception.line === line)) {
delete exceptionsLookup[exceptionKey];
return;
if (matchedException) {
unusedExceptions = unusedExceptions.filter(
exception => exception !== matchedException
);
} else {
results.push({
rule: rule.name,
path: relativePath,
line: line.length < 300 ? line : undefined,
reasonCategory: ALL_REASONS,
updated: now.toJSON(),
reasonDetail: '<optional>',
});
}
results.push({
rule: rule.name,
path: relativePath,
line: line.length < 300 ? line : undefined,
lineNumber,
reasonCategory: ALL_REASONS,
updated: now.toJSON(),
reasonDetail: '<optional>',
});
});
});
},
@ -392,8 +367,6 @@ async function main(): Promise<void> {
{ concurrency: 100 }
);
const unusedExceptions = Object.values(exceptionsLookup);
console.log(
`${scannedCount} files scanned.`,
`${results.length} questionable lines,`,

View File

@ -1,4 +1,4 @@
// Copyright 2018-2020 Signal Messenger, LLC
// Copyright 2018-2021 Signal Messenger, LLC
// SPDX-License-Identifier: AGPL-3.0-only
// Tool requirements:
@ -59,7 +59,6 @@ export type ExceptionType = {
rule: string;
path: string;
line?: string;
lineNumber: number;
reasonCategory: string;
updated: string;
reasonDetail: string;

View File

@ -1,11 +1,11 @@
// Copyright 2018-2020 Signal Messenger, LLC
// Copyright 2018-2021 Signal Messenger, LLC
// SPDX-License-Identifier: AGPL-3.0-only
/* eslint-disable no-console */
import { readFileSync } from 'fs';
import { orderBy } from 'lodash';
import { omit, orderBy } from 'lodash';
import { ExceptionType } from './types';
@ -25,5 +25,19 @@ export function loadJSON<T>(target: string): T {
export function sortExceptions(
exceptions: Array<ExceptionType>
): Array<ExceptionType> {
return orderBy(exceptions, ['path', 'lineNumber', 'rule']);
return orderBy(exceptions, [
'path',
'rule',
'reasonCategory',
'updated',
'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']);
}