forked from django/django
-
Notifications
You must be signed in to change notification settings - Fork 0
/
code-pushup.pylint.plugin.ts
257 lines (233 loc) · 6.95 KB
/
code-pushup.pylint.plugin.ts
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
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
import type {
Audit,
AuditOutput,
Group,
Issue,
IssueSeverity,
PluginConfig,
} from "@code-pushup/models";
import {
capitalize,
compareIssueSeverity,
countOccurrences,
executeProcess,
objectToEntries,
pluralizeToken,
truncateIssueMessage,
} from "@code-pushup/utils";
// FOR FUTURE REFERENCE: PyLint has a default scoring formula:
// 10.0 - ((float(5 * error + warning + refactor + convention) / statement) * 10)
// https://pylint.readthedocs.io/en/stable/user_guide/configuration/all-options.html#evaluation
export default async function pylintPlugin(
patterns: string[]
): Promise<PluginConfig> {
const enabledMessages = await findEnabledMessages(patterns);
const audits = listAudits(enabledMessages);
const groups = listGroups(enabledMessages);
return {
slug: "pylint",
title: "PyLint",
icon: "python",
audits,
groups,
runner: () => runLint(patterns, audits),
};
}
type PylintJson2 = {
messages: PylintMessage[];
statistics: PylintStatistics;
};
type PylintMessageType =
| "fatal"
| "error"
| "warning"
| "refactor"
| "convention"
| "info";
type PylintMessage = {
type: PylintMessageType;
symbol: string;
message: string;
messageId: string;
confidence: string;
module: string;
obj: string;
line: number;
column: number;
endLine: number | null;
endColumn: number | null;
path: string;
absolutePath: string;
};
type PylintStatistics = {
messageTypeCount: Record<PylintMessageType, number>;
modulesLinted: number;
score: number;
};
type EnabledMessage = {
symbol: string;
messageId: string;
};
async function findEnabledMessages(
patterns: string[]
): Promise<EnabledMessage[]> {
const { stdout } = await executeProcess({
command: "python",
args: ["-m", "pylint", "--list-msgs-enabled", ...patterns],
});
const lines = stdout.split("\n");
const enabledStart = lines.indexOf("Enabled messages:");
const enabledEnd = lines.findIndex(
(line, i) => i > enabledStart && !line.startsWith(" ")
);
const enabledLines = lines.slice(enabledStart, enabledEnd);
return enabledLines
.map((line): EnabledMessage | null => {
const match = line.match(/^ ([\w-]+) \(([A-Z]\d+)\)$/);
if (!match) {
return null;
}
const [, symbol, messageId] = match;
return { symbol, messageId };
})
.filter((msg): msg is EnabledMessage => msg != null);
}
function listAudits(enabledMessages: EnabledMessage[]): Audit[] {
return enabledMessages.map(({ symbol, messageId }): Audit => {
const type = messageIdToType(messageId);
return {
slug: symbol,
title: `${symbol} (${messageId})`,
...(type && {
docsUrl: `https://pylint.readthedocs.io/en/stable/user_guide/messages/${type}/${symbol}.html`,
}),
};
});
}
function listGroups(enabledMessages: EnabledMessage[]): Group[] {
// source: https://github.com/pylint-dev/pylint/blob/main/pylint/config/help_formatter.py#L47-L53
const descriptions: Record<PylintMessageType, string> = {
info: "for informational messages",
convention: "for programming standard violation",
refactor: "for bad code smell",
warning: "for python specific problems",
error: "for probable bugs in the code",
fatal: "if an error occurred which prevented pylint from doing further processing",
};
const categoriesMap = enabledMessages.reduce<Record<string, string[]>>(
(acc, { symbol, messageId }) => {
const type = messageIdToType(messageId);
if (!type) {
return acc;
}
return { ...acc, [type]: [...(acc[type] ?? []), symbol] };
},
{}
);
return Object.entries(categoriesMap).map(
([type, symbols]): Group => ({
slug: type,
title: capitalize(type),
description: descriptions[type],
docsUrl: `https://pylint.readthedocs.io/en/stable/user_guide/messages/messages_overview.html#${type}`,
refs: symbols.map((symbol) => ({ slug: symbol, weight: 1 })),
})
);
}
function messageIdToType(messageId: string): PylintMessageType | null {
switch (messageId[0]) {
case "F":
return "fatal";
case "E":
return "error";
case "W":
return "warning";
case "R":
return "refactor";
case "C":
return "convention";
case "I":
return "info";
default:
return null;
}
}
async function runLint(
patterns: string[],
audits: Audit[]
): Promise<AuditOutput[]> {
const { stdout, stderr } = await executeProcess({
command: "python",
args: ["-m", "pylint", "--output-format=json2", ...patterns],
ignoreExitCode: true,
});
if (stderr) {
throw new Error(stderr);
}
const result = JSON.parse(stdout) as PylintJson2;
const issuesMap = result.messages.reduce<Record<string, Issue[]>>(
(acc, message) => ({
...acc,
[message.symbol]: [
...(acc[message.symbol] ?? []),
messageToIssue(message),
],
}),
{}
);
return audits.map(({ slug }): AuditOutput => {
const issues = issuesMap[slug] ?? [];
const severityCounts = countOccurrences(
issues.map(({ severity }) => severity)
);
const severities = objectToEntries(severityCounts);
const summaryText =
[...severities]
.sort((a, b) => -compareIssueSeverity(a[0], b[0]))
.map(([severity, count = 0]) => pluralizeToken(severity, count))
.join(", ") || "passed";
return {
slug,
score: Number(issues.length === 0),
value: issues.length,
displayValue: summaryText,
details: { issues },
};
});
}
function messageToIssue({
type,
message,
path,
line,
column,
endLine,
endColumn,
}: PylintMessage): Issue {
return {
message: truncateIssueMessage(message.replace(/_/g, "\\_")),
severity: messageTypeToSeverity(type),
source: {
file: path,
position: {
startLine: line,
startColumn: column + 1,
...(endLine != null && { endLine }),
...(endColumn != null && { endColumn: endColumn + 1 }),
},
},
};
}
function messageTypeToSeverity(type: PylintMessageType): IssueSeverity {
switch (type) {
case "fatal":
case "error":
return "error";
case "warning":
return "warning";
case "refactor":
case "convention":
case "info":
return "info";
}
}