Skip to content

Commit

Permalink
fix: mapped entries not correctly collecting name
Browse files Browse the repository at this point in the history
  • Loading branch information
Codex- committed Oct 31, 2023
1 parent 78b306f commit 9355e00
Show file tree
Hide file tree
Showing 4 changed files with 270 additions and 5 deletions.
2 changes: 1 addition & 1 deletion dist/index.mjs

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

99 changes: 99 additions & 0 deletions src/tasks/__snapshots__/knip.spec.ts.snap
Original file line number Diff line number Diff line change
Expand Up @@ -104,6 +104,105 @@ exports[`knip > buildFilesSection > should return markdown for a collection of f
\`Ratchet.ts\`, \`Clank.ts\`"
`;

exports[`knip > buildMapSection > should transform a classMembers map section to markdown 1`] = `
[
"### Unused Class Members (7)
|Filename|Class|Member|
|-|-|-|
|Qwark.ts|InsaneQwark|\`destroy\`|
|Qwark.ts|SaneQwark|\`rescue\`|
|Rivet.ts|Rivet|\`fly\`<br/>\`swim\`<br/>\`explode\`<br/>\`mutate\`<br/>\`refineGelatonium \`|",
]
`;

exports[`knip > buildMapSection > should transform a enumMembers map section to markdown 1`] = `
[
"### Unused Enum Members (12)
|Filename|Enum|Member|
|-|-|-|
|DrNefarious.ts|Homeworld|\`Magmos\`<br/>\`Aquatos\`<br/>\`Leviathan \`<br/>\`TombliOutpost\`<br/>\`Zanifar \`<br/>\`NefariousSpaceStation \`<br/>\`NefariousCity\`<br/>\`CorsonV\`|
|Sigmund.ts|Membership|\`ZordoomPrison\`<br/>\`GreatClockStaff\`|
|Sigmund.ts|Residence|\`Viceron\`<br/>\`GreatClock\`|",
]
`;

exports[`knip > buildMarkdownSections > processes all parsed sections 1`] = `
[
"### Unused files (3)
\`PolarisGalaxy.ts\`, \`Savali.ts\`, \`PocketDimension.tsx\`",
"### Unused dependencies (3)
|Filename|dependencies|
|-|-|
|packages/a/package.json|\`react\`<br/>\`react-dom\`|
|packages/d/package.json|\`react\`|",
"### Unused devDependencies (5)
|Filename|devDependencies|
|-|-|
|packages/a/package.json|\`dotenv\`<br/>\`eslint-plugin-jest-dom\`|
|packages/b/package.json|\`dotenv\`<br/>\`eslint-plugin-jest-dom\`|
|packages/d/package.json|\`dotenv\`|",
"### Unused optionalPeerDependencies (2)
|Filename|optionalPeerDependencies|
|-|-|
|packages/c/package.json|\`dotenv\`|
|packages/d/package.json|\`@mui/material\`|",
"### Unlisted dependencies (2)
|Filename|unlisted|
|-|-|
|.eslintrc.cjs|\`@emotion/eslint-plugin\`<br/>\`eslint-plugin-jest-dom\`|",
"### Unlisted binaries (1)
|Filename|binaries|
|-|-|
|packages/e/package.json|\`apollo\`|",
"### Unresolved imports (2)
|Filename|unresolved|
|-|-|
|jest.config.js|\`packages/a/src/setupTests.ts\`<br/>\`packages/a/../b/src\`|",
"### Unused exports (3)
|Filename|exports|
|-|-|
|packages/a/src/Utils.ts|\`specialFunc\`<br/>\`uselessFunc\`|
|packages/a/src/Weapons.ts|\`sheepinator\`|",
"### Unused types (5)
|Filename|types|
|-|-|
|packages/a/src/Weapons.ts|\`cowinator\`|
|packages/b/Clank.ts|\`SpecialAgent\`<br/>\`Zoni\`|
|packages/b/Ratchet.ts|\`Lombax\`<br/>\`Homeworld\`|",
"### Unused Enum Members (12)
|Filename|Enum|Member|
|-|-|-|
|DrNefarious.ts|Homeworld|\`Magmos\`<br/>\`Aquatos\`<br/>\`Leviathan \`<br/>\`TombliOutpost\`<br/>\`Zanifar \`<br/>\`NefariousSpaceStation \`<br/>\`NefariousCity\`<br/>\`CorsonV\`|
|Sigmund.ts|Residence|\`Viceron\`<br/>\`GreatClock\`|
|Sigmund.ts|Membership|\`ZordoomPrison\`<br/>\`GreatClockStaff\`|",
"### Unused Class Members (7)
|Filename|Class|Member|
|-|-|-|
|Rivet.ts|Rivet|\`fly\`<br/>\`swim\`<br/>\`explode\`<br/>\`mutate\`<br/>\`refineGelatonium \`|
|Qwark.ts|SaneQwark|\`rescue\`|
|Qwark.ts|InsaneQwark|\`destroy\`|",
"### Duplicates (2)
|Filename|duplicates|
|-|-|
|Lombax.ts|\`Ratchet\`, \`default\`|
|WarBot.ts|\`Kit\`, \`default\`|",
]
`;

exports[`knip > parseJsonReport > should not attempt to handle an undefined or null key 1`] = `
{
"binaries": {},
Expand Down
167 changes: 167 additions & 0 deletions src/tasks/knip.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,8 @@ import { invalidReportJson, reportJson } from "./__fixtures__/knip.fixture.ts";
import {
buildArraySection,
buildFilesSection,
buildMapSection,
buildMarkdownSections,
buildRunKnipCommand,
buildSectionName,
parseJsonReport,
Expand Down Expand Up @@ -348,4 +350,169 @@ describe("knip", () => {
expect(section).toMatchSnapshot();
});
});

describe("buildMapSection", () => {
it("should transform a enumMembers map section to markdown", () => {
const enumMembers = {
"DrNefarious.ts": {
Homeworld: [
{
col: 5,
line: 37,
name: "Magmos",
pos: 1273,
},
{
col: 5,
line: 38,
name: "Aquatos",
pos: 1300,
},
{
col: 5,
line: 39,
name: "Leviathan ",
pos: 1317,
},
{
col: 5,
line: 40,
name: "TombliOutpost",
pos: 1317,
},
{
col: 5,
line: 41,
name: "Zanifar ",
pos: 1317,
},
{
col: 5,
line: 42,
name: "NefariousSpaceStation ",
pos: 1317,
},
{
col: 5,
line: 43,
name: "NefariousCity",
pos: 1317,
},
{
col: 5,
line: 44,
name: "CorsonV",
pos: 1317,
},
],
},
"Sigmund.ts": {
Membership: [
{
col: 5,
line: 37,
name: "ZordoomPrison",
pos: 1273,
},
{
col: 5,
line: 38,
name: "GreatClockStaff",
pos: 1300,
},
],
Residence: [
{
col: 5,
line: 37,
name: "Viceron",
pos: 1273,
},
{
col: 5,
line: 38,
name: "GreatClock",
pos: 1300,
},
],
},
};

const section = buildMapSection("enumMembers", enumMembers);
expect(section).toMatchSnapshot();
});

it("should transform a classMembers map section to markdown", () => {
const classMembers = {
"Qwark.ts": {
InsaneQwark: [
{
col: 9,
line: 327,
name: "destroy",
pos: 12268,
},
],
SaneQwark: [
{
col: 9,
line: 327,
name: "rescue",
pos: 12268,
},
],
},
"Rivet.ts": {
Rivet: [
{
col: 9,
line: 327,
name: "fly",
pos: 12268,
},
{
col: 3,
line: 353,
name: "swim",
pos: 12977,
},
{
col: 3,
line: 357,
name: "explode",
pos: 13056,
},
{
col: 3,
line: 381,
name: "mutate",
pos: 13810,
},
{
col: 3,
line: 388,
name: "refineGelatonium ",
pos: 13987,
},
],
},
};

const section = buildMapSection("classMembers", classMembers);
expect(section).toMatchSnapshot();
});
});

describe("buildMarkdownSections", () => {
it("processes all parsed sections", () => {
const parsedReport = parseJsonReport(JSON.stringify(reportJson));
const mdSections = buildMarkdownSections(parsedReport);

expect(mdSections).toHaveLength(12);
for (const section of mdSections) {
expect(section).toBeTypeOf("string");
}
expect(mdSections).toMatchSnapshot();
});
});
});
7 changes: 3 additions & 4 deletions src/tasks/knip.ts
Original file line number Diff line number Diff line change
Expand Up @@ -201,7 +201,6 @@ export function buildSectionName(name: string): string {
}
}

// TODO the interface for rawResults has changed, adding the line data
/**
* Build a section where the result is a collection of strings
*/
Expand Down Expand Up @@ -236,7 +235,7 @@ export function buildArraySection(
/**
* Build a section where the result is a ma
*/
function buildMapSection(
export function buildMapSection(
name: string,
rawResults: Record<string, Record<string, Item[]>>,
): string[] {
Expand All @@ -250,7 +249,7 @@ function buildMapSection(
tableBody.push([
filename,
definitionName,
members.map((member) => `\`${member}\``).join("<br/>"),
members.map((member) => `\`${member.name}\``).join("<br/>"),
]);
}
}
Expand Down Expand Up @@ -309,7 +308,7 @@ function processSectionToMessage(
return output;
}

function buildMarkdownSections(report: ParsedReport): string[] {
export function buildMarkdownSections(report: ParsedReport): string[] {
const output: string[] = [];
for (const key of Object.keys(report)) {
switch (key) {
Expand Down

0 comments on commit 9355e00

Please sign in to comment.