Skip to content

Commit

Permalink
Fix an issue with pruning a project with deleted files that had suppr…
Browse files Browse the repository at this point in the history
…essions
  • Loading branch information
iclanton committed Mar 28, 2024
1 parent e548e6e commit dc3ec45
Show file tree
Hide file tree
Showing 2 changed files with 38 additions and 5 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
{
"changes": [
{
"packageName": "@rushstack/eslint-patch",
"comment": "Fix an issue with running `eslint-bulk prune` in a project with suppressions that refer to deleted files.",
"type": "patch"
}
],
"packageName": "@rushstack/eslint-patch"
}
33 changes: 28 additions & 5 deletions eslint/eslint-patch/src/eslint-bulk-suppressions/cli/prune.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
// Copyright (c) Microsoft Corporation. All rights reserved. Licensed under the MIT license.
// See LICENSE in the project root for license information.

import fs from 'fs';

import { printPruneHelp } from './utils/print-help';
import { runEslintAsync } from './runEslint';
import { ESLINT_BULK_PRUNE_ENV_VAR_NAME } from '../constants';
Expand All @@ -20,13 +22,11 @@ export async function pruneAsync(): Promise<void> {

process.env[ESLINT_BULK_PRUNE_ENV_VAR_NAME] = '1';

const allFiles: string[] = getAllFilesWithExistingSuppressionsForCwd();
console.log(`Pruning suppressions for ${allFiles.length} files...`);

const allFiles: string[] = await getAllFilesWithExistingSuppressionsForCwdAsync();
await runEslintAsync(allFiles, 'prune');
}

function getAllFilesWithExistingSuppressionsForCwd(): string[] {
async function getAllFilesWithExistingSuppressionsForCwdAsync(): Promise<string[]> {
const { jsonObject: bulkSuppressionsConfigJson } = getSuppressionsConfigForEslintrcFolderPath(
process.cwd().replace(/\\/g, '/')
);
Expand All @@ -35,5 +35,28 @@ function getAllFilesWithExistingSuppressionsForCwd(): string[] {
allFiles.add(filePath);
}

return Array.from(allFiles);
const allFilesArray: string[] = Array.from(allFiles);

const allExistingFiles: string[] = [];
// TODO: limit parallelism here with something similar to `Async.forEachAsync` from `node-core-library`.
await Promise.all(
allFilesArray.map(async (filePath: string) => {
try {
await fs.promises.access(filePath, fs.constants.F_OK);
allExistingFiles.push(filePath);
} catch {
// Doesn't exist - ignore
}
})
);

console.log(`Found ${allExistingFiles.length} files with existing suppressions.`);
const deletedCount: number = allFilesArray.length - allExistingFiles.length;
if (deletedCount > 0) {
console.log(`${deletedCount} files with suppressions were deleted.`);
}

console.log(`Pruning suppressions for ${allExistingFiles.length} files...`);

return allExistingFiles;
}

0 comments on commit dc3ec45

Please sign in to comment.