diff --git a/common/changes/@rushstack/eslint-patch/user-ianc-fix-pruning-issue_2024-03-29-04-53.json b/common/changes/@rushstack/eslint-patch/user-ianc-fix-pruning-issue_2024-03-29-04-53.json new file mode 100644 index 00000000000..251809ba7fc --- /dev/null +++ b/common/changes/@rushstack/eslint-patch/user-ianc-fix-pruning-issue_2024-03-29-04-53.json @@ -0,0 +1,10 @@ +{ + "changes": [ + { + "packageName": "@rushstack/eslint-patch", + "comment": "Fix an issue where the `eslint-bulk prune` command would crash if a bulk suppressions file exists that speicifies no suppressions.", + "type": "patch" + } + ], + "packageName": "@rushstack/eslint-patch" +} \ No newline at end of file diff --git a/eslint/eslint-patch/src/eslint-bulk-suppressions/bulk-suppressions-file.ts b/eslint/eslint-patch/src/eslint-bulk-suppressions/bulk-suppressions-file.ts index eccc49418ef..240d4ad541f 100644 --- a/eslint/eslint-patch/src/eslint-bulk-suppressions/bulk-suppressions-file.ts +++ b/eslint/eslint-patch/src/eslint-bulk-suppressions/bulk-suppressions-file.ts @@ -101,23 +101,32 @@ export function getAllBulkSuppressionsConfigsByEslintrcFolderPath(): [string, IB } export function writeSuppressionsJsonToFile( - eslintrcDirectory: string, + eslintrcFolderPath: string, suppressionsConfig: IBulkSuppressionsConfig ): void { - suppressionsJsonByFolderPath.set(eslintrcDirectory, { readTime: Date.now(), suppressionsConfig }); - const suppressionsPath: string = `${eslintrcDirectory}/${SUPPRESSIONS_JSON_FILENAME}`; + suppressionsJsonByFolderPath.set(eslintrcFolderPath, { readTime: Date.now(), suppressionsConfig }); + const suppressionsPath: string = `${eslintrcFolderPath}/${SUPPRESSIONS_JSON_FILENAME}`; if (suppressionsConfig.jsonObject.suppressions.length === 0) { - try { - fs.unlinkSync(suppressionsPath); - } catch (e) { - throwIfAnythingOtherThanNotExistError(e); - } + deleteFile(suppressionsPath); } else { suppressionsConfig.jsonObject.suppressions.sort(compareSuppressions); fs.writeFileSync(suppressionsPath, JSON.stringify(suppressionsConfig.jsonObject, undefined, 2)); } } +export function deleteBulkSuppressionsFileInEslintrcFolder(eslintrcFolderPath: string): void { + const suppressionsPath: string = `${eslintrcFolderPath}/${SUPPRESSIONS_JSON_FILENAME}`; + deleteFile(suppressionsPath); +} + +function deleteFile(filePath: string): void { + try { + fs.unlinkSync(filePath); + } catch (e) { + throwIfAnythingOtherThanNotExistError(e); + } +} + export function serializeSuppression({ file, scopeId, rule }: ISuppression): string { return `${file}|${scopeId}|${rule}`; } diff --git a/eslint/eslint-patch/src/eslint-bulk-suppressions/cli/prune.ts b/eslint/eslint-patch/src/eslint-bulk-suppressions/cli/prune.ts index 9a3b727091a..5869b810b13 100755 --- a/eslint/eslint-patch/src/eslint-bulk-suppressions/cli/prune.ts +++ b/eslint/eslint-patch/src/eslint-bulk-suppressions/cli/prune.ts @@ -6,7 +6,10 @@ import fs from 'fs'; import { printPruneHelp } from './utils/print-help'; import { runEslintAsync } from './runEslint'; import { ESLINT_BULK_PRUNE_ENV_VAR_NAME } from '../constants'; -import { getSuppressionsConfigForEslintrcFolderPath } from '../bulk-suppressions-file'; +import { + deleteBulkSuppressionsFileInEslintrcFolder, + getSuppressionsConfigForEslintrcFolderPath +} from '../bulk-suppressions-file'; export async function pruneAsync(): Promise { const args: string[] = process.argv.slice(3); @@ -20,16 +23,21 @@ export async function pruneAsync(): Promise { throw new Error(`@rushstack/eslint-bulk: Unknown arguments: ${args.join(' ')}`); } - process.env[ESLINT_BULK_PRUNE_ENV_VAR_NAME] = '1'; - - const allFiles: string[] = await getAllFilesWithExistingSuppressionsForCwdAsync(); - await runEslintAsync(allFiles, 'prune'); + const normalizedCwd: string = process.cwd().replace(/\\/g, '/'); + const allFiles: string[] = await getAllFilesWithExistingSuppressionsForCwdAsync(normalizedCwd); + if (allFiles.length > 0) { + process.env[ESLINT_BULK_PRUNE_ENV_VAR_NAME] = '1'; + console.log(`Pruning suppressions for ${allFiles.length} files...`); + await runEslintAsync(allFiles, 'prune'); + } else { + console.log('No files with existing suppressions found. Deleting the suppressions file, if one exists.'); + deleteBulkSuppressionsFileInEslintrcFolder(normalizedCwd); + } } -async function getAllFilesWithExistingSuppressionsForCwdAsync(): Promise { - const { jsonObject: bulkSuppressionsConfigJson } = getSuppressionsConfigForEslintrcFolderPath( - process.cwd().replace(/\\/g, '/') - ); +async function getAllFilesWithExistingSuppressionsForCwdAsync(normalizedCwd: string): Promise { + const { jsonObject: bulkSuppressionsConfigJson } = + getSuppressionsConfigForEslintrcFolderPath(normalizedCwd); const allFiles: Set = new Set(); for (const { file: filePath } of bulkSuppressionsConfigJson.suppressions) { allFiles.add(filePath); @@ -56,7 +64,5 @@ async function getAllFilesWithExistingSuppressionsForCwdAsync(): Promise