From 71d84d018b5a487c19037841e662f0ccf6a11bd9 Mon Sep 17 00:00:00 2001 From: Alex Miller Date: Thu, 7 Dec 2023 11:25:08 +1300 Subject: [PATCH] fix: emit markdown if both verbose mode and annotations are disabled --- dist/index.mjs | 7 +++--- src/tasks/__snapshots__/knip.spec.ts.snap | 30 +++++++++++++++++++++++ src/tasks/knip.spec.ts | 10 ++++++++ src/tasks/knip.ts | 7 +++--- 4 files changed, 48 insertions(+), 6 deletions(-) diff --git a/dist/index.mjs b/dist/index.mjs index 2a10ccd..9d1ce43 100644 --- a/dist/index.mjs +++ b/dist/index.mjs @@ -27791,6 +27791,7 @@ function buildMapSection(name, rawResults, annotationsEnabled, verboseEnabled) { const annotations = []; const resultType = name === "classMembers" ? "Class" : "Enum"; const resultMetaType = name === "classMembers" ? "class" : "enum"; + const shouldBuildMarkdown = verboseEnabled || !annotationsEnabled; for (const [filename, results] of Object.entries(rawResults)) { for (const [definitionName, members] of Object.entries(results)) { const itemNames = []; @@ -27804,17 +27805,17 @@ function buildMapSection(name, rawResults, annotationsEnabled, verboseEnabled) { type: resultMetaType }); } - if (verboseEnabled) { + if (shouldBuildMarkdown) { itemNames.push(`\`${member.name}\``); } } totalUnused += members.length; - if (verboseEnabled) { + if (shouldBuildMarkdown) { tableBody.push([filename, definitionName, itemNames.join("
")]); } } } - if (verboseEnabled) { + if (shouldBuildMarkdown) { const tableHeader = ["Filename", resultType, "Member"]; const sectionHeaderName = `${resultType} Members`; const sectionHeader = `### Unused ${sectionHeaderName} (${totalUnused})`; diff --git a/src/tasks/__snapshots__/knip.spec.ts.snap b/src/tasks/__snapshots__/knip.spec.ts.snap index 1c5935c..a5c36ba 100644 --- a/src/tasks/__snapshots__/knip.spec.ts.snap +++ b/src/tasks/__snapshots__/knip.spec.ts.snap @@ -241,6 +241,21 @@ exports[`knip > buildMapSection > should transform a classMembers map section to } `; +exports[`knip > buildMapSection > should transform a classMembers map section to markdown if verbose and annotations are disabled 1`] = ` +{ + "annotations": [], + "sections": [ + "### Unused Class Members (7) + +|Filename|Class|Member| +|-|-|-| +|Qwark.ts|InsaneQwark|\`destroy\`| +|Qwark.ts|SaneQwark|\`rescue\`| +|Rivet.ts|Rivet|\`fly\`
\`swim\`
\`explode\`
\`mutate\`
\`refineGelatonium \`|", + ], +} +`; + exports[`knip > buildMapSection > should transform a enumMembers map section and annotations 1`] = ` { "annotations": [ @@ -448,6 +463,21 @@ exports[`knip > buildMapSection > should transform a enumMembers map section to } `; +exports[`knip > buildMapSection > should transform a enumMembers map section to markdown if verbose and annotations are disabled 1`] = ` +{ + "annotations": [], + "sections": [ + "### Unused Enum Members (12) + +|Filename|Enum|Member| +|-|-|-| +|DrNefarious.ts|Homeworld|\`Magmos\`
\`Aquatos\`
\`Leviathan \`
\`TombliOutpost\`
\`Zanifar \`
\`NefariousSpaceStation \`
\`NefariousCity\`
\`CorsonV\`| +|Sigmund.ts|Membership|\`ZordoomPrison\`
\`GreatClockStaff\`| +|Sigmund.ts|Residence|\`Viceron\`
\`GreatClock\`|", + ], +} +`; + exports[`knip > buildMarkdownSections > outputs only sections with defaults 1`] = ` [ "### Unused files (3) diff --git a/src/tasks/knip.spec.ts b/src/tasks/knip.spec.ts index 5673d54..af2957d 100644 --- a/src/tasks/knip.spec.ts +++ b/src/tasks/knip.spec.ts @@ -518,6 +518,11 @@ describe("knip", () => { expect(section).toMatchSnapshot(); }); + it("should transform a enumMembers map section to markdown if verbose and annotations are disabled", () => { + const section = buildMapSection("enumMembers", enumMembers, false, false); + expect(section).toMatchSnapshot(); + }); + it("should transform a enumMembers map section and annotations", () => { const section = buildMapSection("enumMembers", enumMembers, true, false); expect(section).toMatchSnapshot(); @@ -533,6 +538,11 @@ describe("knip", () => { expect(section).toMatchSnapshot(); }); + it("should transform a classMembers map section to markdown if verbose and annotations are disabled", () => { + const section = buildMapSection("classMembers", classMembers, false, false); + expect(section).toMatchSnapshot(); + }); + it("should transform a classMembers map section and annotations", () => { const section = buildMapSection("classMembers", classMembers, true, false); expect(section).toMatchSnapshot(); diff --git a/src/tasks/knip.ts b/src/tasks/knip.ts index 2c8f8f8..749862c 100644 --- a/src/tasks/knip.ts +++ b/src/tasks/knip.ts @@ -251,6 +251,7 @@ export function buildMapSection( const annotations: ItemMeta[] = []; const resultType = name === "classMembers" ? "Class" : "Enum"; const resultMetaType = name === "classMembers" ? "class" : "enum"; + const shouldBuildMarkdown = verboseEnabled || !annotationsEnabled; for (const [filename, results] of Object.entries(rawResults)) { for (const [definitionName, members] of Object.entries(results)) { @@ -265,18 +266,18 @@ export function buildMapSection( type: resultMetaType, }); } - if (verboseEnabled) { + if (shouldBuildMarkdown) { itemNames.push(`\`${member.name}\``); } } totalUnused += members.length; - if (verboseEnabled) { + if (shouldBuildMarkdown) { tableBody.push([filename, definitionName, itemNames.join("
")]); } } } - if (verboseEnabled) { + if (shouldBuildMarkdown) { const tableHeader = ["Filename", resultType, "Member"]; const sectionHeaderName = `${resultType} Members`; const sectionHeader = `### Unused ${sectionHeaderName} (${totalUnused})`;