-
Notifications
You must be signed in to change notification settings - Fork 15
Expand file tree
/
Copy pathutils.ts
More file actions
126 lines (118 loc) · 3.34 KB
/
utils.ts
File metadata and controls
126 lines (118 loc) · 3.34 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
import { type SourceFile, SyntaxKind } from 'ts-morph';
import {
type FileCoverage,
aggregateCoverageStats,
capitalize,
formatAsciiTable,
formatCoveragePercentage,
logger,
objectToEntries,
pluralize,
pluralizeToken,
toArray,
} from '@code-pushup/utils';
import type { JsDocsPluginTransformedConfig } from '../config.js';
import { SYNTAX_COVERAGE_MAP } from './constants.js';
import type { CoverageType } from './models.js';
/**
* Creates an empty unprocessed coverage report.
* @param initialValue - Initial value for each coverage type
* @returns The empty unprocessed coverage report.
*/
export function createInitialCoverageTypesRecord<T>(
initialValue: T,
): Record<CoverageType, T> {
return {
enums: initialValue,
interfaces: initialValue,
types: initialValue,
functions: initialValue,
variables: initialValue,
classes: initialValue,
methods: initialValue,
properties: initialValue,
};
}
/**
* Converts the coverage type to the audit slug.
* @param type - The coverage type.
* @returns The audit slug.
*/
export function coverageTypeToAuditSlug(type: CoverageType) {
return `${type}-coverage`;
}
/**
* Maps the SyntaxKind from the library ts-morph to the coverage type.
* @param kind - The SyntaxKind from the library ts-morph.
* @returns The coverage type.
*/
export function getCoverageTypeFromKind(kind: SyntaxKind): CoverageType {
const type = SYNTAX_COVERAGE_MAP.get(kind);
if (!type) {
throw new Error(`Unsupported syntax kind: ${kind}`);
}
return type;
}
/**
* Convert plural coverage type to singular form
* @param type Coverage type (plural)
* @returns Singular form of coverage type
*/
export function singularCoverageType(type: CoverageType): string {
switch (type) {
case 'classes':
return 'class';
case 'enums':
return 'enum';
case 'functions':
return 'function';
case 'interfaces':
return 'interface';
case 'methods':
return 'method';
case 'properties':
return 'property';
case 'types':
return 'type';
case 'variables':
return 'variable';
}
}
export function logSourceFiles(
sourceFiles: SourceFile[],
config: JsDocsPluginTransformedConfig,
): void {
const patterns = toArray(config.patterns);
logger.info(
`Found ${pluralizeToken('source file', sourceFiles.length)} matching ${pluralize('pattern', patterns.length)} ${patterns.join(' ')}`,
);
}
export function logReport(report: Record<CoverageType, FileCoverage[]>): void {
const typesCount = Object.keys(report).length;
logger.info(
`Collected documentation coverage for ${pluralizeToken('type', typesCount)} of ${pluralize('entity', typesCount)}`,
);
if (!logger.isVerbose()) {
return;
}
logger.debug(
formatAsciiTable({
columns: [
{ key: 'type', label: 'Entity', align: 'left' },
{ key: 'covered', label: 'Hits', align: 'right' },
{ key: 'total', label: 'Found', align: 'right' },
{ key: 'coverage', label: 'Coverage', align: 'right' },
],
rows: objectToEntries(report)
.map(([type, files]) => {
const stats = aggregateCoverageStats(files);
return {
...stats,
type: capitalize(type),
coverage: formatCoveragePercentage(stats),
};
})
.toSorted((a, b) => b.total - a.total),
}),
);
}