Skip to content

Commit

Permalink
feat: implement list view
Browse files Browse the repository at this point in the history
  • Loading branch information
sushichan044 committed Mar 6, 2025
1 parent fc78ce0 commit 5e7093d
Show file tree
Hide file tree
Showing 2 changed files with 72 additions and 0 deletions.
66 changes: 66 additions & 0 deletions src/cli/action/list.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,66 @@
import { table } from "table";

import type { TodoModuleLike } from "../../todofile/types";
import type { TodoModuleV2 } from "../../todofile/v2";

import { TodoModuleV2Handler } from "../../todofile/v2";
import { defineAction } from "./index";

export const listAction = defineAction(async ({ core, logger }) => {
const todoModule = getTodoModuleV2(await core.readTodoModule());

if (todoModule === false) {
throw new Error("Unsupported todo file version");
}

const todoTable = generateTodoTable(todoModule);

if (todoTable.length === 0) {
logger.success("No rules found in the todo file! Good job!");
return;
}

const tableData = [
["Rule", "Files", "Violations", "AutoFixable"],
...todoTable,
];

logger.log.raw(table(tableData));

return;
});

const getTodoModuleV2 = (module: TodoModuleLike): false | TodoModuleV2 => {
if (TodoModuleV2Handler.isVersion(module)) {
return module;
}

return false;
};

type TableFormat = [
ruleId: string,
violatedFiles: number,
totalViolations: number,
autoFixableEmoji: string,
];

const generateTodoTable = (todoModule: TodoModuleV2): TableFormat[] => {
const tableData: TableFormat[] = [];

for (const [ruleId, { autoFix, violations }] of Object.entries(
todoModule.todo,
)) {
const violatedFiles = Object.keys(violations).length;
const totalViolations = Object.values(violations).reduce(
(sum, count) => sum + count,
0,
);

const autoFixEmoji = autoFix ? "✓" : "";

tableData.push([ruleId, violatedFiles, totalViolations, autoFixEmoji]);
}

return tableData;
};
6 changes: 6 additions & 0 deletions src/cli/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import { ESLintTodoCore } from "../index";
import { runAction } from "./action";
import { deleteRuleAction } from "./action/delete-rule";
import { genAction } from "./action/gen";
import { listAction } from "./action/list";
import { selectRulesToFixAction } from "./action/select-rule";
import { updateAction } from "./action/update";
import { resolveCLIContext } from "./context";
Expand Down Expand Up @@ -135,6 +136,11 @@ const cli = defineCommand({

await runAction(updateAction, { consola, options });

if (context.mode === "list") {
await runAction(listAction, { consola, options });
return;
}

if (context.mode === "generate") {
await runAction(genAction, { consola, options });
consola.success(`ESLint todo file generated at ${context.todoFilePath}!`);
Expand Down

0 comments on commit 5e7093d

Please sign in to comment.