Skip to content
This repository was archived by the owner on Jul 16, 2023. It is now read-only.
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
* chore: disable github_checks annotations from codecov
* chore: activate language strict rules
* fix: add missing severity for rules
* feat: facelift console reporters

## 4.8.1

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,14 +27,15 @@ class LintConsoleReporter
Iterable<LintFileReport> records, {
Iterable<SummaryLintReportRecord<Object>> summary = const [],
}) async {
if (records.isEmpty) {
return;
}
var hasReportData = false;

for (final file in records) {
final lines = [
..._reportMetrics('', file.file),
..._reportIssues([...file.issues, ...file.antiPatternCases]),
..._reportIssues(
[...file.issues, ...file.antiPatternCases],
file.relativePath,
),
..._reportEntityMetrics({...file.classes, ...file.functions}),
];

Expand All @@ -43,13 +44,21 @@ class LintConsoleReporter
lines.forEach(output.writeln);
output.writeln('');
}

hasReportData |= lines.isNotEmpty;
}

if (!hasReportData) {
output.writeln('${okPen('✔')} no issues found!');
}
}

Iterable<String> _reportIssues(Iterable<Issue> issues) => (issues.toList()
..sort((a, b) =>
a.location.start.offset.compareTo(b.location.start.offset)))
.map(_helper.getIssueMessage);
Iterable<String> _reportIssues(Iterable<Issue> issues, String relativePath) =>
(issues.toList()
..sort((a, b) =>
a.location.start.offset.compareTo(b.location.start.offset)))
.map((issue) => _helper.getIssueMessage(issue, relativePath))
.expand((lines) => lines);

Iterable<String> _reportEntityMetrics(Map<String, Report> reports) =>
(reports.entries.toList()
Expand All @@ -66,9 +75,7 @@ class LintConsoleReporter
_helper.getMetricReport(metric),
];

return [
_helper.getMetricMessage(reportLevel, source, violations),
];
return _helper.getMetricMessage(reportLevel, source, violations);
}

return [];
Expand Down
Original file line number Diff line number Diff line change
@@ -1,52 +1,74 @@
import 'package:ansicolor/ansicolor.dart';

import '../../../../../utils/string_extensions.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 _warnigPen = 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: AnsiPen()..red(bold: true),
MetricValueLevel.warning: AnsiPen()..yellow(bold: true),
MetricValueLevel.noted: AnsiPen()..blue(),
MetricValueLevel.none: AnsiPen()..white(),
MetricValueLevel.alarm: _alarmPen,
MetricValueLevel.warning: _warnigPen,
MetricValueLevel.noted: _bluePen,
MetricValueLevel.none: _whitePen,
};

final _severityPens = {
Severity.error: AnsiPen()..red(bold: true),
Severity.warning: AnsiPen()..yellow(bold: true),
Severity.performance: AnsiPen()..cyan(),
Severity.style: AnsiPen()..blue(),
Severity.none: AnsiPen()..white(),
Severity.error: _alarmPen,
Severity.warning: _warnigPen,
Severity.performance: _bluePen,
Severity.style: _bluePen,
Severity.none: _whitePen,
};

/// Converts an [issue] to the issue message string.
String getIssueMessage(Issue issue) {
Iterable<String> getIssueMessage(Issue issue, String relativePath) {
final severity = _getSeverity(issue.severity);
final location =
'${issue.location.start.line}:${issue.location.start.column}';

return '$severity${[issue.message, location, issue.ruleId].join(' : ')}';
final location = _linkPen(
'$relativePath:${issue.location.start.line}:${issue.location.start.column}',
);
final tabulation = _normalize('');

return [
'$severity${issue.message}',
'$tabulation$location',
'$tabulation${issue.ruleId} : ${issue.documentation}',
'',
];
}

/// Creates a message for [violations] based on given [violationLevel].
String getMetricMessage(
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 != MetricValueLevel.none
? violationLevel.toString().capitalize()
: '',
));

return '$normalizedLabel${source.isNotEmpty ? '$source - ' : ''}${violations.join(', ')}';
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.');
Expand All @@ -70,7 +92,7 @@ class LintConsoleReporterHelper {

if (color != null) {
return color(_normalize(
severity != Severity.none ? severity.toString().capitalize() : '',
severity != Severity.none ? severity.toString().toUpperCase() : '',
));
}

Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,5 @@
import 'dart:io';

import 'package:ansicolor/ansicolor.dart';

import '../../../../../reporters/models/console_reporter.dart';
import '../../../models/unused_files_file_report.dart';

Expand All @@ -18,7 +16,7 @@ class UnusedFilesConsoleReporter
Iterable<void> summary = const [],
}) async {
if (records.isEmpty) {
output.writeln('No unused files found!');
output.writeln('${okPen('✔')} no unused files found!');

return;
}
Expand All @@ -27,13 +25,15 @@ class UnusedFilesConsoleReporter
..sort((a, b) => a.relativePath.compareTo(b.relativePath));

for (final analysisRecord in sortedRecords) {
output.writeln('Unused file: ${analysisRecord.relativePath}');
output.writeln(
'${warnigPen('⚠')} unused file: ${analysisRecord.relativePath}',
);
}

final color = AnsiPen()..yellow();

output
..writeln('')
..writeln('Total unused files - ${color(sortedRecords.length)}');
..writeln(
'${alarmPen('✖')} total unused files - ${alarmPen(sortedRecords.length)}',
);
}
}
Original file line number Diff line number Diff line change
@@ -1,7 +1,5 @@
import 'dart:io';

import 'package:ansicolor/ansicolor.dart';

import '../../../../../reporters/models/console_reporter.dart';
import '../../../models/unused_l10n_file_report.dart';

Expand All @@ -10,10 +8,6 @@ import '../../../models/unused_l10n_file_report.dart';
/// Use it to create reports in console format.
class UnusedL10nConsoleReporter
extends ConsoleReporter<UnusedL10nFileReport, void> {
final _errorColor = AnsiPen()..red(bold: true);
final _warningColor = AnsiPen()..yellow(bold: true);
final _successColor = AnsiPen()..green();

UnusedL10nConsoleReporter(IOSink output) : super(output);

@override
Expand All @@ -22,7 +16,7 @@ class UnusedL10nConsoleReporter
Iterable<void> summary = const [],
}) async {
if (records.isEmpty) {
output.writeln('${_successColor('✔')} no unused localization found!');
output.writeln('${okPen('✔')} no unused localization found!');

return;
}
Expand All @@ -44,7 +38,7 @@ class UnusedL10nConsoleReporter
final pathOffset = offset.padRight(5);

output
..writeln('$offset ${_warningColor('⚠')} unused ${issue.memberName}')
..writeln('$offset ${warnigPen('⚠')} unused ${issue.memberName}')
..writeln('$pathOffset at $path:$line:$column');
}

Expand All @@ -54,7 +48,7 @@ class UnusedL10nConsoleReporter
}

output.writeln(
'${_errorColor('✖')} total unused localization class fields, getters and methods - ${_errorColor(warnings)}',
'${alarmPen('✖')} total unused localization class fields, getters and methods - ${alarmPen(warnings)}',
);
}
}
7 changes: 6 additions & 1 deletion lib/src/reporters/models/console_reporter.dart
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import 'dart:io';

import 'package:ansicolor/ansicolor.dart';
import 'package:meta/meta.dart';

import 'file_report.dart';
Expand All @@ -10,8 +11,12 @@ abstract class ConsoleReporter<T extends FileReport, S> extends Reporter<T, S> {
static const String id = 'console';
static const String verboseId = 'console-verbose';

final AnsiPen alarmPen = AnsiPen()..rgb(r: 0.88, g: 0.32, b: 0.36);
final AnsiPen warnigPen = AnsiPen()..rgb(r: 0.98, g: 0.68, b: 0.4);
final AnsiPen okPen = AnsiPen()..rgb(r: 0.08, g: 0.11, b: 0.81);

@protected
final IOSink output;

const ConsoleReporter(this.output);
ConsoleReporter(this.output);
}
Original file line number Diff line number Diff line change
Expand Up @@ -36,8 +36,14 @@ void main() {
message: 'Issue message',
verboseMessage: 'Issue verbose message',
),
'lib/src/my_source.dart',
),
equals('\x1B[38;5;11mWarning \x1B[0mIssue message : 1:2 : rule'),
equals([
'\x1B[38;5;180mWARNING \x1B[0mIssue message',
' \x1B[38;5;39mlib/src/my_source.dart:1:2\x1B[0m',
' rule : https://dartcodemetrics/rules/rule',
'',
]),
);

expect(
Expand All @@ -50,8 +56,14 @@ void main() {
message: 'Issue message',
verboseMessage: 'Issue verbose message',
),
'lib/src/my_source.dart',
),
equals('\x1B[38;5;7m \x1B[0mIssue message : 1:2 : rule'),
equals([
'\x1B[38;5;7m \x1B[0mIssue message',
' \x1B[38;5;39mlib/src/my_source.dart:1:2\x1B[0m',
' rule : https://dartcodemetrics/rules/rule',
'',
]),
);
});

Expand All @@ -62,9 +74,12 @@ void main() {
'Class.method',
['violation1', 'violation2'],
),
equals(
'\x1B[38;5;9mAlarm \x1B[0mClass.method - violation1, violation2',
),
equals([
'\x1B[38;5;167mALARM \x1B[0mClass.method',
' violation1',
' violation2',
'',
]),
);

expect(
Expand All @@ -73,9 +88,12 @@ void main() {
'Class.method',
['violation1', 'violation2'],
),
equals(
'\x1B[38;5;7m \x1B[0mClass.method - violation1, violation2',
),
equals([
'\x1B[38;5;7mNONE \x1B[0mClass.method',
' violation1',
' violation2',
'',
]),
);
});

Expand Down
Loading