Skip to content

Commit

Permalink
Fix up reporters
Browse files Browse the repository at this point in the history
  • Loading branch information
webpro committed Oct 6, 2022
1 parent ce3c42d commit f6c1d6b
Show file tree
Hide file tree
Showing 2 changed files with 35 additions and 28 deletions.
44 changes: 20 additions & 24 deletions src/reporters/compact.ts
Original file line number Diff line number Diff line change
@@ -1,25 +1,21 @@
import path from 'node:path';
import type { Issues, Configuration } from '../types';
import type { Issue, Issues, Configuration } from '../types';

const logIssueLine = (cwd: string, filePath: string, symbols: string[]) => {
console.log(`${path.relative(cwd, filePath)}: ${symbols.join(', ')}`);
const logIssueLine = (cwd: string, filePath: string, symbols?: string[]) => {
console.log(`${path.relative(cwd, filePath)}${symbols ? `: ${symbols.join(', ')}` : ''}`);
};

const logIssueGroupResult = (issues: string[], cwd: string, title: false | string) => {
title && console.log(`--- ${title} (${issues.length})`);
if (issues.length) {
const sortedByFilePath = issues.sort();
sortedByFilePath.forEach(filePath => console.log(path.relative(cwd, filePath)));
sortedByFilePath.forEach(filePath => logIssueLine(cwd, filePath));
} else {
console.log('N/A');
}
};

const logIssueGroupResults = (
issues: { filePath: string; symbols: string[] }[],
cwd: string,
title: false | string
) => {
const logIssueGroupResults = (issues: Issue[], cwd: string, title: false | string) => {
title && console.log(`--- ${title} (${issues.length})`);
if (issues.length) {
const sortedByFilePath = issues.sort((a, b) => (a.filePath > b.filePath ? 1 : -1));
Expand All @@ -34,11 +30,13 @@ export default ({ issues, config, cwd }: { issues: Issues; config: Configuration
isOnlyFiles,
isOnlyExports,
isOnlyTypes,
isOnlyNsMembers,
isOnlyDuplicates,
isFindUnusedFiles,
isFindUnusedExports,
isFindUnusedTypes,
isFindDuplicateExports
isFindNsImports,
isFindDuplicateExports,
} = config;

if (isFindUnusedFiles) {
Expand All @@ -49,33 +47,31 @@ export default ({ issues, config, cwd }: { issues: Issues; config: Configuration
if (isFindUnusedExports) {
const unusedExports = Object.values(issues.export).map(issues => {
const items = Object.values(issues);
return {
filePath: items[0].filePath,
symbols: items.map(i => i.symbol)
};
return { ...items[0], symbols: items.map(i => i.symbol) };
});
logIssueGroupResults(unusedExports, cwd, !isOnlyExports && 'UNUSED EXPORTS');
}

if (isFindUnusedTypes) {
const unusedTypes = Object.values(issues.type).map(issues => {
const items = Object.values(issues);
return {
filePath: items[0].filePath,
symbols: items.map(i => i.symbol)
};
return { ...items[0], symbols: items.map(i => i.symbol) };
});
logIssueGroupResults(unusedTypes, cwd, !isOnlyTypes && 'UNUSED TYPES');
}

if (isFindDuplicateExports) {
const unusedDuplicates = Object.values(issues.duplicate).map(issues => {
if (isFindNsImports) {
const unusedMembers = Object.values(issues.member).map(issues => {
const items = Object.values(issues);
return {
filePath: items[0].filePath,
symbols: items.map(i => i.symbols ?? []).flat()
};
return { ...items[0], symbols: items.map(i => i.symbol) };
});
logIssueGroupResults(unusedMembers, cwd, !isOnlyNsMembers && 'UNUSED NAMESPACE MEMBERS');
}

if (isFindDuplicateExports) {
const unusedDuplicates = Object.values(issues.duplicate)
.map(issues => Object.values(issues))
.flat();
logIssueGroupResults(unusedDuplicates, cwd, !isOnlyDuplicates && 'DUPLICATE EXPORTS');
}
};
19 changes: 15 additions & 4 deletions src/reporters/symbols.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,10 @@
import path from 'node:path';
import type { Issue, Issues, Configuration } from '../types';

const logIssueLine = (cwd: string, filePath: string, symbol: string, padding: number) => {
console.log(`${symbol.padEnd(padding + 2)}${path.relative(cwd, filePath)}`);
const logIssueLine = ({ issue, cwd, padding }: { issue: Issue; cwd: string; padding: number }) => {
console.log(
`${issue.symbol.padEnd(padding + 2)}${issue.symbolType?.padEnd(11) || ''}${path.relative(cwd, issue.filePath)}`
);
};

const logIssueGroupResult = (issues: string[], cwd: string, title: false | string) => {
Expand All @@ -20,7 +22,7 @@ const logIssueGroupResults = (issues: Issue[], cwd: string, title: false | strin
if (issues.length) {
const sortedByFilePath = issues.sort((a, b) => (a.filePath > b.filePath ? 1 : -1));
const padding = [...issues].sort((a, b) => b.symbol.length - a.symbol.length)[0].symbol.length;
sortedByFilePath.forEach(({ filePath, symbol }) => logIssueLine(cwd, filePath, symbol, padding));
sortedByFilePath.forEach(issue => logIssueLine({ issue, cwd, padding }));
} else {
console.log('N/A');
}
Expand All @@ -31,11 +33,13 @@ export default ({ issues, config, cwd }: { issues: Issues; config: Configuration
isOnlyFiles,
isOnlyExports,
isOnlyTypes,
isOnlyNsMembers,
isOnlyDuplicates,
isFindUnusedFiles,
isFindUnusedExports,
isFindUnusedTypes,
isFindDuplicateExports
isFindNsImports,
isFindDuplicateExports,
} = config;

if (isFindUnusedFiles) {
Expand All @@ -57,6 +61,13 @@ export default ({ issues, config, cwd }: { issues: Issues; config: Configuration
logIssueGroupResults(unusedTypes, cwd, !isOnlyTypes && 'UNUSED TYPES');
}

if (isFindNsImports) {
const unusedExports = Object.values(issues.member)
.map(issues => Object.values(issues))
.flat();
logIssueGroupResults(unusedExports, cwd, !isOnlyNsMembers && 'UNUSED NAMESPACE MEMBERS');
}

if (isFindDuplicateExports) {
const unusedDuplicates = Object.values(issues.duplicate)
.map(issues => Object.values(issues))
Expand Down

0 comments on commit f6c1d6b

Please sign in to comment.