|
| 1 | +import 'dart:io'; |
| 2 | + |
| 3 | +import 'package:xml/xml.dart'; |
| 4 | + |
| 5 | +import '../../../../../reporters/models/checkstyle_reporter.dart'; |
| 6 | +import '../../../models/lint_file_report.dart'; |
| 7 | +import '../../../models/severity.dart'; |
| 8 | +import '../../../models/summary_lint_report_record.dart'; |
| 9 | +import '../../lint_report_params.dart'; |
| 10 | + |
| 11 | +/// Lint Checkstyle reporter. |
| 12 | +/// |
| 13 | +/// Use it to create reports in Checkstyle format. |
| 14 | +class LintCheckstyleReporter extends CheckstyleReporter<LintFileReport, |
| 15 | + SummaryLintReportRecord<Object>, LintReportParams> { |
| 16 | + LintCheckstyleReporter(IOSink output) : super(output); |
| 17 | + |
| 18 | + @override |
| 19 | + Future<void> report( |
| 20 | + Iterable<LintFileReport> records, { |
| 21 | + Iterable<SummaryLintReportRecord<Object>> summary = const [], |
| 22 | + LintReportParams? additionalParams, |
| 23 | + }) async { |
| 24 | + if (records.isEmpty) { |
| 25 | + return; |
| 26 | + } |
| 27 | + |
| 28 | + final builder = XmlBuilder(); |
| 29 | + |
| 30 | + builder |
| 31 | + ..processing('xml', 'version="1.0"') |
| 32 | + ..element('checkstyle', attributes: {'version': '10.0'}, nest: () { |
| 33 | + for (final record in records) { |
| 34 | + if (!_needToReport(record)) { |
| 35 | + continue; |
| 36 | + } |
| 37 | + |
| 38 | + builder.element( |
| 39 | + 'file', |
| 40 | + attributes: {'name': record.relativePath}, |
| 41 | + nest: () { |
| 42 | + final issues = [...record.issues, ...record.antiPatternCases]; |
| 43 | + |
| 44 | + for (final issue in issues) { |
| 45 | + builder.element( |
| 46 | + 'error', |
| 47 | + attributes: { |
| 48 | + 'line': '${issue.location.start.line}', |
| 49 | + if (issue.location.start.column > 0) |
| 50 | + 'column': '${issue.location.start.column}', |
| 51 | + 'severity': _severityMapping[issue.severity] ?? 'ignore', |
| 52 | + 'message': issue.message, |
| 53 | + 'source': issue.ruleId, |
| 54 | + }, |
| 55 | + ); |
| 56 | + } |
| 57 | + }, |
| 58 | + ); |
| 59 | + } |
| 60 | + }); |
| 61 | + |
| 62 | + output.writeln(builder.buildDocument().toXmlString(pretty: true)); |
| 63 | + } |
| 64 | + |
| 65 | + bool _needToReport(LintFileReport report) => |
| 66 | + report.issues.isNotEmpty || report.antiPatternCases.isNotEmpty; |
| 67 | +} |
| 68 | + |
| 69 | +const _severityMapping = { |
| 70 | + Severity.error: 'error', |
| 71 | + Severity.warning: 'warning', |
| 72 | + Severity.style: 'info', |
| 73 | + Severity.performance: 'warning', |
| 74 | + Severity.none: 'ignore', |
| 75 | +}; |
0 commit comments