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_checkstyle_reporter.dart
133 lines (115 loc) · 3.84 KB
/
lint_checkstyle_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
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
import 'dart:io';
import 'package:xml/xml.dart';
import '../../../../../reporters/models/checkstyle_reporter.dart';
import '../../../metrics/models/metric_value_level.dart';
import '../../../models/lint_file_report.dart';
import '../../../models/severity.dart';
import '../../../models/summary_lint_report_record.dart';
import '../../lint_report_params.dart';
/// Lint Checkstyle reporter.
///
/// Use it to create reports in Checkstyle format.
class LintCheckstyleReporter extends CheckstyleReporter<LintFileReport,
SummaryLintReportRecord<Object>, LintReportParams> {
LintCheckstyleReporter(IOSink output) : super(output);
@override
Future<void> report(
Iterable<LintFileReport> records, {
Iterable<SummaryLintReportRecord<Object>> summary = const [],
LintReportParams? additionalParams,
}) async {
if (records.isEmpty) {
return;
}
final builder = XmlBuilder();
builder
..processing('xml', 'version="1.0"')
..element('checkstyle', attributes: {'version': '10.0'}, nest: () {
for (final record in records) {
if (!_needToReport(record)) {
continue;
}
builder.element(
'file',
attributes: {'name': record.relativePath},
nest: () {
_reportIssues(builder, record);
_reportMetrics(builder, record);
},
);
}
});
output.writeln(builder.buildDocument().toXmlString(pretty: true));
}
void _reportIssues(XmlBuilder builder, LintFileReport report) {
final issues = [...report.issues, ...report.antiPatternCases];
for (final issue in issues) {
final locationStart = issue.location.start;
builder.element(
'error',
attributes: {
'line': '${locationStart.line}',
if (locationStart.column > 0) 'column': '${locationStart.column}',
'severity': _issueSeverityMapping[issue.severity] ?? 'ignore',
'message': issue.message,
'source': issue.ruleId,
},
);
}
}
void _reportMetrics(XmlBuilder builder, LintFileReport record) {
for (final metric in record.file.metrics) {
if (_isMetricNeedToReport(metric.level)) {
builder.element(
'error',
attributes: {
'line': '0',
'severity': _metricSeverityMapping[metric.level] ?? 'ignore',
'message': metric.comment,
'source': metric.metricsId,
},
);
}
}
final metricRecords =
{...record.classes, ...record.functions}.entries.toList();
for (final record in metricRecords) {
if (!_isMetricNeedToReport(record.value.metricsLevel)) {
continue;
}
final location = record.value.location;
for (final metricValue in record.value.metrics) {
builder.element(
'error',
attributes: {
'line': '${location.start.line}',
if (record.value.location.start.column > 0)
'column': '${record.value.location.start.column}',
'severity': _metricSeverityMapping[metricValue.level] ?? 'ignore',
'message': metricValue.comment,
'source': metricValue.metricsId,
},
);
}
}
}
bool _needToReport(LintFileReport report) =>
report.issues.isNotEmpty ||
report.antiPatternCases.isNotEmpty ||
_isMetricNeedToReport(report.file.metricsLevel);
bool _isMetricNeedToReport(MetricValueLevel level) =>
level > MetricValueLevel.none;
}
const _issueSeverityMapping = {
Severity.error: 'error',
Severity.warning: 'warning',
Severity.style: 'info',
Severity.performance: 'warning',
Severity.none: 'ignore',
};
const _metricSeverityMapping = {
MetricValueLevel.alarm: 'error',
MetricValueLevel.warning: 'warning',
MetricValueLevel.noted: 'info',
MetricValueLevel.none: 'ignore',
};