Skip to content

Commit

Permalink
fix: output ignored resolution defects
Browse files Browse the repository at this point in the history
and add profile test cases

Defects (and general results) ignored because of resolution being ignored by profile are shown with "(ignored)" qualifier.
  • Loading branch information
jason-ha committed Sep 22, 2024
1 parent a76face commit 2378133
Show file tree
Hide file tree
Showing 9 changed files with 3,200 additions and 8 deletions.
38 changes: 30 additions & 8 deletions packages/cli/src/render/typed.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,10 @@
import * as core from "@arethetypeswrong/core";
import { filterProblems, problemAffectsEntrypoint, problemKindInfo } from "@arethetypeswrong/core/problems";
import {
filterProblems,
problemAffectsEntrypoint,
problemAffectsResolutionKind,
problemKindInfo,
} from "@arethetypeswrong/core/problems";
import { allResolutionKinds, getResolutionOption, groupProblemsByKind } from "@arethetypeswrong/core/utils";
import chalk from "chalk";
import Table, { type GenericTable, type HorizontalTableRow } from "cli-table3";
Expand All @@ -17,8 +22,10 @@ export async function typed(
const problems = analysis.problems.filter(
(problem) => !ignoreRules || !ignoreRules.includes(problemFlags[problem.kind]),
);
const resolutions = allResolutionKinds.filter((kind) => !ignoreResolutions.includes(kind));
const grouped = groupProblemsByKind(problems);
// sort resolutions with required (impacts result) first and ignored after
const requiredResolutions = allResolutionKinds.filter((kind) => !ignoreResolutions.includes(kind));
const ignoredResolutions = allResolutionKinds.filter((kind) => ignoreResolutions.includes(kind));
const resolutions = requiredResolutions.concat(ignoredResolutions);
const entrypoints = Object.keys(analysis.entrypoints);
marked.setOptions({
renderer: new TerminalRenderer(),
Expand Down Expand Up @@ -52,12 +59,18 @@ export async function typed(

if (summary) {
const defaultSummary = marked(!emoji ? " No problems found" : " No problems found 🌟");
const summaryTexts = Object.keys(grouped).map((kind) => {
const grouped = groupProblemsByKind(problems);
const summaryTexts = Object.entries(grouped).map(([kind, kindProblems]) => {
const info = problemKindInfo[kind as core.ProblemKind];
const affectsRequiredResolution = kindProblems.some((p) =>
requiredResolutions.some((r) => problemAffectsResolutionKind(p, r, analysis)),
);
const description = marked(
`${info.description}${info.details ? ` Use \`-f json\` to see ${info.details}.` : ""} ${info.docsUrl}`,
);
return `${emoji ? `${info.emoji} ` : ""}${description}`;
return `${affectsRequiredResolution ? "" : "(ignored per resolution) "}${
emoji ? `${info.emoji} ` : ""
}${description}`;
});

out(summaryTexts.join("") || defaultSummary);
Expand All @@ -73,6 +86,7 @@ export async function typed(
});

const getCellContents = memo((subpath: string, resolutionKind: core.ResolutionKind) => {
const ignoredPrefix = ignoreResolutions.includes(resolutionKind) ? "(ignored) " : "";
const problemsForCell = groupProblemsByKind(
filterProblems(problems, analysis, { entrypoint: subpath, resolutionKind }),
);
Expand All @@ -81,7 +95,10 @@ export async function typed(
const kinds = Object.keys(problemsForCell) as core.ProblemKind[];
if (kinds.length) {
return kinds
.map((kind) => (emoji ? `${problemKindInfo[kind].emoji} ` : "") + problemKindInfo[kind].shortDescription)
.map(
(kind) =>
ignoredPrefix + (emoji ? `${problemKindInfo[kind].emoji} ` : "") + problemKindInfo[kind].shortDescription,
)
.join("\n");
}

Expand All @@ -93,13 +110,18 @@ export async function typed(
analysis.programInfo[getResolutionOption(resolutionKind)].moduleKinds?.[resolution?.fileName ?? ""]
?.detectedKind || ""
];
return resolution?.isJson ? jsonResult : moduleResult;
return ignoredPrefix + (resolution?.isJson ? jsonResult : moduleResult);
});

const flippedTable =
format === "auto" || format === "table-flipped"
? new Table({
head: ["", ...resolutions.map((kind) => chalk.reset(resolutionKinds[kind]))],
head: [
"",
...resolutions.map((kind) =>
chalk.reset(resolutionKinds[kind] + (ignoreResolutions.includes(kind) ? " (ignored)" : "")),
),
],
})
: undefined;
if (flippedTable) {
Expand Down
12 changes: 12 additions & 0 deletions packages/cli/test/snapshots.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,18 @@ const tests = [

["[email protected]", "--entrypoints-legacy --ignore-rules=cjs-only-exports-default"],
["@[email protected]"],

// Profile test cases
// Some ignored failures and some not - exit code should be 1 per non-node10 failures
["[email protected]", "--profile node16"],
// Explicit strict profile - exit code 1 per node10 resolution
["@[email protected]", "--profile strict -f table"],
// Profile ignoring node10 resolution - exit code 0
["@[email protected]", "--profile node16 -f table-flipped"],
// Profile ignoring node10 and CJS resolution mixed with specific entrypoint - exit code 0
["@[email protected]", "--profile esm-only -f json --entrypoints ."],
// Profile ignoring node10 and bundler resolution - exit code 0
["@[email protected]", "--profile node16-only -f ascii"],
];

const defaultOpts = "-f table-flipped";
Expand Down
Loading

0 comments on commit 2378133

Please sign in to comment.