This repository has been archived by the owner on Jul 16, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 258
/
lint_console_reporter_helper.dart
104 lines (84 loc) · 3 KB
/
lint_console_reporter_helper.dart
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
import 'package:ansicolor/ansicolor.dart';
import '../../../metrics/models/metric_value.dart';
import '../../../metrics/models/metric_value_level.dart';
import '../../../models/issue.dart';
import '../../../models/severity.dart';
final _alarmPen = AnsiPen()..rgb(r: 0.88, g: 0.32, b: 0.36);
final _warningPen = AnsiPen()..rgb(r: 0.98, g: 0.68, b: 0.4);
final _bluePen = AnsiPen()..rgb(r: 0.08, g: 0.11, b: 0.81);
final _whitePen = AnsiPen()..white();
final _linkPen = AnsiPen()..rgb(r: 0.0, g: 0.78, b: 1.0);
/// Helper for building lint console reports.
class LintConsoleReporterHelper {
static final _colorPens = {
MetricValueLevel.alarm: _alarmPen,
MetricValueLevel.warning: _warningPen,
MetricValueLevel.noted: _bluePen,
MetricValueLevel.none: _whitePen,
};
final _severityPens = {
Severity.error: _alarmPen,
Severity.warning: _warningPen,
Severity.performance: _bluePen,
Severity.style: _bluePen,
Severity.none: _whitePen,
};
/// Converts an [issue] to the issue message string.
Iterable<String> getIssueMessage(Issue issue, String absolutePath) {
final severity = _getSeverity(issue.severity);
final locationStart = issue.location.start;
final location = _linkPen(
'$absolutePath:${locationStart.line}:${locationStart.column}',
);
final tabulation = _normalize('');
return [
'$severity${issue.message}',
'${tabulation}at $location',
'$tabulation${issue.ruleId} : ${issue.documentation}',
'',
];
}
/// Creates a message for [violations] based on given [violationLevel].
Iterable<String> getMetricMessage(
MetricValueLevel violationLevel,
String source,
Iterable<String> violations,
) {
if (violations.isEmpty) {
return [];
}
final color = _colorPens[violationLevel];
if (color != null) {
final normalizedLabel =
color(_normalize(violationLevel.toString().toUpperCase()));
final firstLine = source.isNotEmpty ? source : violations.first;
final records = source.isNotEmpty ? violations : violations.skip(1);
final tabulation = _normalize('');
return [
'$normalizedLabel$firstLine',
for (final record in records) '$tabulation$record',
'',
];
}
throw StateError('Unexpected violation level.');
}
/// Converts a [metric] to the metric message string.
String getMetricReport(MetricValue metric) {
final color = _colorPens[metric.level];
if (color != null) {
final value = '${metric.value.toInt()} ${metric.unitType ?? ''}'.trim();
return '${metric.documentation.name.toLowerCase()}: ${color(value)}';
}
throw StateError('Unexpected violation level.');
}
String _getSeverity(Severity severity) {
final color = _severityPens[severity];
if (color != null) {
return color(_normalize(
severity != Severity.none ? severity.toString().toUpperCase() : '',
));
}
throw StateError('Unexpected severity.');
}
String _normalize(String s) => s.padRight(8);
}