Skip to content

Commit

Permalink
Fix an issue where 'eslint-bulk prune' crashes if there aren't any su…
Browse files Browse the repository at this point in the history
…ppressions.
  • Loading branch information
iclanton committed Mar 29, 2024
1 parent f6563eb commit d7fb39a
Show file tree
Hide file tree
Showing 3 changed files with 44 additions and 19 deletions.
Original file line number Diff line number Diff line change
@@ -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"
}
Original file line number Diff line number Diff line change
Expand Up @@ -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}`;
}
Expand Down
28 changes: 17 additions & 11 deletions eslint/eslint-patch/src/eslint-bulk-suppressions/cli/prune.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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<void> {
const args: string[] = process.argv.slice(3);
Expand All @@ -20,16 +23,21 @@ export async function pruneAsync(): Promise<void> {
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<string[]> {
const { jsonObject: bulkSuppressionsConfigJson } = getSuppressionsConfigForEslintrcFolderPath(
process.cwd().replace(/\\/g, '/')
);
async function getAllFilesWithExistingSuppressionsForCwdAsync(normalizedCwd: string): Promise<string[]> {
const { jsonObject: bulkSuppressionsConfigJson } =
getSuppressionsConfigForEslintrcFolderPath(normalizedCwd);
const allFiles: Set<string> = new Set();
for (const { file: filePath } of bulkSuppressionsConfigJson.suppressions) {
allFiles.add(filePath);
Expand All @@ -56,7 +64,5 @@ async function getAllFilesWithExistingSuppressionsForCwdAsync(): Promise<string[
console.log(`${deletedCount} files with suppressions were deleted.`);
}

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

return allExistingFiles;
}

0 comments on commit d7fb39a

Please sign in to comment.