knip
Version:
Find and fix unused dependencies, exports and files in your TypeScript and JavaScript projects
33 lines (32 loc) • 1.85 kB
JavaScript
import { relative } from '../util/path.js';
import { flattenIssues, getIssueTypeTitle } from './util/util.js';
export default ({ report, issues, cwd }) => {
console.log('# Knip report\n');
const getFilePath = (issue) => {
if (!(issue.line && issue.col))
return relative(cwd, issue.filePath);
return `${relative(cwd, issue.filePath)}:${issue.line}:${issue.col}`;
};
const sortLongestSymbol = (a, b) => b.symbol.length - a.symbol.length;
const sortLongestFilePath = (a, b) => getFilePath(b).length - getFilePath(a).length;
for (const [reportType, isReportType] of Object.entries(report)) {
if (isReportType) {
const title = getIssueTypeTitle(reportType);
const issuesForType = flattenIssues(issues[reportType]);
if (issuesForType.length > 0) {
console.log(`## ${title} (${issuesForType.length})\n`);
const longestSymbol = issuesForType.sort(sortLongestSymbol)[0].symbol.length;
const sortedByFilePath = issuesForType.sort(sortLongestFilePath);
const longestFilePath = getFilePath(sortedByFilePath[0]).length;
const nameWidth = Math.max(longestSymbol, 'Name'.length);
const locationWidth = Math.max(longestFilePath, 'Location'.length);
console.log(`| ${'Name'.padEnd(nameWidth)} | ${'Location'.padEnd(locationWidth)} | Severity |`);
console.log(`| :${'-'.repeat(nameWidth - 1)} | :${'-'.repeat(locationWidth - 1)} | :------- |`);
for (const issue of sortedByFilePath) {
console.log(`| ${issue.symbol.padEnd(nameWidth)} | ${getFilePath(issue).padEnd(locationWidth)} | ${(issue.severity ?? '').padEnd(8)} |`);
}
console.log('');
}
}
}
};