-
Notifications
You must be signed in to change notification settings - Fork 16
Expand file tree
/
Copy pathissues.ts
More file actions
198 lines (178 loc) · 5.17 KB
/
Copy pathissues.ts
File metadata and controls
198 lines (178 loc) · 5.17 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
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
import type {
Audit,
AuditReport,
CategoryRef,
FileIssue,
PluginMeta,
Report,
ReportsDiff,
} from '@code-pushup/models';
import { isFileIssue } from '@code-pushup/utils';
import {
type ChangedFiles,
adjustFileName,
adjustLine,
isFileChanged,
} from './git.js';
export type SourceFileIssue = FileIssue & IssueContext;
type IssueContext = {
audit: Pick<Audit, 'slug' | 'title'>;
plugin: Pick<PluginMeta, 'slug' | 'title'>;
};
type IssueCompareFn = (a: SourceFileIssue, b: SourceFileIssue) => number;
export function filterRelevantIssues({
currReport,
prevReport,
reportsDiff,
changedFiles,
}: {
currReport: Report;
prevReport: Report;
reportsDiff: ReportsDiff;
changedFiles: ChangedFiles;
}): SourceFileIssue[] {
const auditsWithPlugin = [
...reportsDiff.audits.changed,
...reportsDiff.audits.added,
]
.map((auditLink): [PluginMeta, AuditReport] | undefined => {
const plugin = currReport.plugins.find(
({ slug }) => slug === auditLink.plugin.slug,
);
const audit = plugin?.audits.find(({ slug }) => slug === auditLink.slug);
return plugin && audit && [plugin, audit];
})
.filter((ctx): ctx is [PluginMeta, AuditReport] => ctx != null);
const issues = auditsWithPlugin.flatMap(([plugin, audit]) =>
getAuditIssues(audit, plugin),
);
const prevIssues = prevReport.plugins.flatMap(plugin =>
plugin.audits.flatMap(audit => getAuditIssues(audit, plugin)),
);
return issues
.filter(
issue =>
isFileChanged(changedFiles, issue.source.file) &&
!prevIssues.some(prevIssue =>
issuesMatch(prevIssue, issue, changedFiles),
),
)
.sort(createIssuesSortCompareFn(currReport));
}
function getAuditIssues(
audit: AuditReport,
plugin: PluginMeta,
): SourceFileIssue[] {
return (
audit.details?.issues
?.filter(isFileIssue)
.map(issue => ({ ...issue, audit, plugin })) ?? []
);
}
export function issuesMatch(
prev: SourceFileIssue,
curr: SourceFileIssue,
changedFiles: ChangedFiles,
): boolean {
return (
prev.plugin.slug === curr.plugin.slug &&
prev.audit.slug === curr.audit.slug &&
prev.severity === curr.severity &&
removeDigits(prev.message) === removeDigits(curr.message) &&
adjustFileName(changedFiles, prev.source.file) === curr.source.file &&
positionsMatch(prev.source, curr.source, changedFiles)
);
}
function removeDigits(message: string): string {
return message.replace(/\d+/g, '');
}
function positionsMatch(
prev: SourceFileIssue['source'],
curr: SourceFileIssue['source'],
changedFiles: ChangedFiles,
): boolean {
if (!hasPosition(prev) || !hasPosition(curr)) {
return hasPosition(prev) === hasPosition(curr);
}
return (
adjustedStartLinesMatch(prev, curr, changedFiles) ||
adjustedLineSpansMatch(prev, curr, changedFiles)
);
}
function hasPosition(
source: SourceFileIssue['source'],
): source is Required<SourceFileIssue['source']> {
return source.position != null;
}
function adjustedStartLinesMatch(
prev: Required<SourceFileIssue['source']>,
curr: Required<SourceFileIssue['source']>,
changedFiles: ChangedFiles,
): boolean {
return (
adjustLine(changedFiles, prev.file, prev.position.startLine) ===
curr.position.startLine
);
}
function adjustedLineSpansMatch(
prev: SourceFileIssue['source'],
curr: SourceFileIssue['source'],
changedFiles: ChangedFiles,
): boolean {
if (prev.position?.endLine == null || curr.position?.endLine == null) {
return false;
}
const prevLineCount = prev.position.endLine - prev.position.startLine;
const currLineCount = curr.position.endLine - curr.position.startLine;
const currStartLineOffset =
adjustLine(changedFiles, curr.file, curr.position.startLine) -
curr.position.startLine;
return prevLineCount === currLineCount - currStartLineOffset;
}
function createIssuesSortCompareFn(report: Report): IssueCompareFn {
return (a, b) =>
getAuditImpactValue(b, report) - getAuditImpactValue(a, report);
}
export function getAuditImpactValue(
{ audit, plugin }: IssueContext,
report: Report,
): number {
if (!report.categories) {
return 0;
}
return report.categories
.map((category): number => {
const weights = category.refs.map((ref): number => {
if (ref.plugin !== plugin.slug) {
return 0;
}
switch (ref.type) {
case 'audit':
return ref.slug === audit.slug ? ref.weight : 0;
case 'group':
return calculateGroupImpact(ref, audit, report);
}
});
return (
weights.reduce((acc, weight) => acc + weight, 0) /
category.refs.reduce((acc, { weight }) => acc + weight, 0)
);
})
.reduce((acc, value) => acc + value, 0);
}
export function calculateGroupImpact(
ref: CategoryRef,
audit: Audit,
report: Report,
): number {
const group = report.plugins
.find(({ slug }) => slug === ref.plugin)
?.groups?.find(({ slug }) => slug === ref.slug);
if (!group?.refs.length) {
return 0;
}
const groupRatio =
(group.refs.find(({ slug }) => slug === audit.slug)?.weight ?? 0) /
group.refs.reduce((acc, { weight }) => acc + weight, 0);
return ref.weight * groupRatio;
}