Skip to content

Commit

Permalink
Delete suppressions file if all issues have been resolved.
Browse files Browse the repository at this point in the history
  • Loading branch information
iclanton committed Mar 28, 2024
1 parent dc3ec45 commit 96d5dc4
Show file tree
Hide file tree
Showing 3 changed files with 47 additions and 10 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
{
"changes": [
{
"packageName": "@rushstack/eslint-patch",
"comment": "Delete the `.eslint-bulk-suppressions.json` file during pruning if all suppressions have been eliminated.",
"type": "minor"
}
],
"packageName": "@rushstack/eslint-patch"
}
Original file line number Diff line number Diff line change
Expand Up @@ -25,11 +25,18 @@ const IS_RUNNING_IN_VSCODE: boolean = process.env[VSCODE_PID_ENV_VAR_NAME] !== u
const TEN_SECONDS_MS: number = 10 * 1000;
const SUPPRESSIONS_JSON_FILENAME: string = '.eslint-bulk-suppressions.json';

function throwIfAnythingOtherThanNotExistError(e: NodeJS.ErrnoException): void | never {
if (e?.code !== 'ENOENT') {
// Throw an error if any other error than file not found
throw e;
}
}

interface ICachedBulkSuppressionsConfig {
readTime: number;
suppressionsConfig: IBulkSuppressionsConfig;
}
export const suppressionsJsonByFolderPath: Map<string, ICachedBulkSuppressionsConfig> = new Map();
const suppressionsJsonByFolderPath: Map<string, ICachedBulkSuppressionsConfig> = new Map();
export function getSuppressionsConfigForEslintrcFolderPath(
eslintrcFolderPath: string
): IBulkSuppressionsConfig {
Expand All @@ -51,10 +58,7 @@ export function getSuppressionsConfigForEslintrcFolderPath(
try {
rawJsonFile = fs.readFileSync(suppressionsPath).toString();
} catch (e) {
// Throw an error if any other error than file not found
if (e.code !== 'ENOENT') {
throw e;
}
throwIfAnythingOtherThanNotExistError(e);
}

if (!rawJsonFile) {
Expand Down Expand Up @@ -87,14 +91,31 @@ export function getSuppressionsConfigForEslintrcFolderPath(
return suppressionsConfig!;
}

export function getAllBulkSuppressionsConfigsByEslintrcFolderPath(): [string, IBulkSuppressionsConfig][] {
const result: [string, IBulkSuppressionsConfig][] = [];
for (const [eslintrcFolderPath, { suppressionsConfig }] of suppressionsJsonByFolderPath) {
result.push([eslintrcFolderPath, suppressionsConfig]);
}

return result;
}

export function writeSuppressionsJsonToFile(
eslintrcDirectory: string,
suppressionsConfig: IBulkSuppressionsConfig
): void {
suppressionsJsonByFolderPath.set(eslintrcDirectory, { readTime: Date.now(), suppressionsConfig });
const suppressionsPath: string = `${eslintrcDirectory}/${SUPPRESSIONS_JSON_FILENAME}`;
suppressionsConfig.jsonObject.suppressions.sort(compareSuppressions);
fs.writeFileSync(suppressionsPath, JSON.stringify(suppressionsConfig.jsonObject, undefined, 2));
if (suppressionsConfig.jsonObject.suppressions.length === 0) {
try {
fs.unlinkSync(suppressionsPath);
} catch (e) {
throwIfAnythingOtherThanNotExistError(e);
}
} else {
suppressionsConfig.jsonObject.suppressions.sort(compareSuppressions);
fs.writeFileSync(suppressionsPath, JSON.stringify(suppressionsConfig.jsonObject, undefined, 2));
}
}

export function serializeSuppression({ file, scopeId, rule }: ISuppression): string {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ import {
type IBulkSuppressionsConfig,
type ISuppression,
writeSuppressionsJsonToFile,
suppressionsJsonByFolderPath
getAllBulkSuppressionsConfigsByEslintrcFolderPath
} from './bulk-suppressions-file';

const ESLINTRC_FILENAMES: string[] = [
Expand Down Expand Up @@ -169,7 +169,10 @@ export function shouldBulkSuppress(params: {
}

export function prune(): void {
for (const [eslintrcFolderPath, { suppressionsConfig }] of suppressionsJsonByFolderPath) {
for (const [
eslintrcFolderPath,
suppressionsConfig
] of getAllBulkSuppressionsConfigsByEslintrcFolderPath()) {
if (suppressionsConfig) {
const { newSerializedSuppressions, newJsonObject } = suppressionsConfig;
const newSuppressionsConfig: IBulkSuppressionsConfig = {
Expand All @@ -185,7 +188,10 @@ export function prune(): void {
}

export function write(): void {
for (const [eslintrcFolderPath, { suppressionsConfig }] of suppressionsJsonByFolderPath) {
for (const [
eslintrcFolderPath,
suppressionsConfig
] of getAllBulkSuppressionsConfigsByEslintrcFolderPath()) {
if (suppressionsConfig) {
writeSuppressionsJsonToFile(eslintrcFolderPath, suppressionsConfig);
}
Expand Down

0 comments on commit 96d5dc4

Please sign in to comment.