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.dart
90 lines (75 loc) · 2.91 KB
/
lint_console_reporter.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
import 'dart:io';
import '../../../../../reporters/models/console_reporter.dart';
import '../../../metrics/metric_utils.dart';
import '../../../metrics/models/metric_value.dart';
import '../../../metrics/models/metric_value_level.dart';
import '../../../models/issue.dart';
import '../../../models/lint_file_report.dart';
import '../../../models/report.dart';
import '../../../models/summary_lint_report_record.dart';
import '../../lint_report_params.dart';
import 'lint_console_reporter_helper.dart';
/// Lint console reporter.
///
/// Use it to create reports in console format.
class LintConsoleReporter extends ConsoleReporter<LintFileReport,
SummaryLintReportRecord<Object>, LintReportParams> {
/// If true will report info about all files even if they're not above warning threshold.
final bool reportAll;
final _helper = LintConsoleReporterHelper();
LintConsoleReporter(IOSink output, {this.reportAll = false}) : super(output);
@override
Future<void> report(
Iterable<LintFileReport> records, {
Iterable<SummaryLintReportRecord<Object>> summary = const [],
LintReportParams? additionalParams,
}) async {
var hasReportData = false;
for (final file in records) {
final lines = [
..._reportMetrics('', file.file),
..._reportIssues(
[...file.issues, ...file.antiPatternCases],
file.path,
),
..._reportEntityMetrics({...file.classes, ...file.functions}),
];
if (lines.isNotEmpty) {
output.writeln('${file.relativePath}:');
lines.forEach(output.writeln);
output.writeln('');
}
hasReportData |= lines.isNotEmpty;
}
if (!hasReportData) {
if (additionalParams?.congratulate ?? true) {
output.writeln('${okPen('✔')} no issues found!');
}
}
}
Iterable<String> _reportIssues(Iterable<Issue> issues, String absolutePath) =>
(issues.toList()
..sort((a, b) =>
a.location.start.offset.compareTo(b.location.start.offset)))
.map((issue) => _helper.getIssueMessage(issue, absolutePath))
.expand((lines) => lines);
Iterable<String> _reportEntityMetrics(Map<String, Report> reports) =>
(reports.entries.toList()
..sort((a, b) => a.value.location.start.offset
.compareTo(b.value.location.start.offset)))
.expand((entry) => _reportMetrics(entry.key, entry.value));
Iterable<String> _reportMetrics(String source, Report report) {
final reportLevel = report.metricsLevel;
if (reportAll || isReportLevel(reportLevel)) {
final violations = [
for (final metric in report.metrics)
if (reportAll || _isNeedToReport(metric))
_helper.getMetricReport(metric),
];
return _helper.getMetricMessage(reportLevel, source, violations);
}
return [];
}
bool _isNeedToReport(MetricValue metric) =>
metric.level > MetricValueLevel.none;
}