Skip to content
This repository was archived by the owner on Jul 16, 2023. It is now read-only.

Commit db4df25

Browse files
committed
feat: add cli options for fatal exit if unused files or l10n are found
1 parent f35888e commit db4df25

File tree

11 files changed

+93
-6
lines changed

11 files changed

+93
-6
lines changed

CHANGELOG.md

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,9 @@
11
# Changelog
22

3+
## Unreleased
4+
5+
* feat: add cli options for fatal exit if unused files or l10n are found
6+
37
## 4.8.0
48

59
* feat: add alphabetical sorting by type for `member-ordering-extended` rule.

lib/src/analyzers/lint_analyzer/lint_analyzer.dart

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -157,6 +157,10 @@ class LintAnalyzer {
157157
violations:
158158
metricViolations(records, SourceLinesOfCodeMetric.metricId),
159159
),
160+
SummaryLintReportRecord<String>(
161+
title: 'Total tech debt',
162+
value: totalTechDebt(records),
163+
),
160164
];
161165

162166
LintFileReport? _analyzeFile(

lib/src/analyzers/lint_analyzer/metrics/metrics_list/technical_debt/technical_debt_metric.dart

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -54,13 +54,13 @@ class TechnicalDebtMetric extends FileMetric<double> {
5454
_deprecatedAnnotationsCost = readConfigValue<double>(
5555
config,
5656
metricId,
57-
'deprecated-annotations',
57+
'deprecated-annotations-cost',
5858
) ??
5959
0.0,
6060
_fileNullSafetyMigrationCost = readConfigValue<double>(
6161
config,
6262
metricId,
63-
'file-nullsafety-migration',
63+
'file-nullsafety-migration-cost',
6464
) ??
6565
0.0,
6666
_unitType = readConfigValue<String>(config, metricId, 'unit-type'),

lib/src/analyzers/lint_analyzer/utils/report_utils.dart

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ import 'package:path/path.dart' as p;
44
import '../metrics/metric_utils.dart';
55
import '../metrics/metrics_list/cyclomatic_complexity/cyclomatic_complexity_metric.dart';
66
import '../metrics/metrics_list/source_lines_of_code/source_lines_of_code_metric.dart';
7+
import '../metrics/metrics_list/technical_debt/technical_debt_metric.dart';
78
import '../metrics/models/metric_value_level.dart';
89
import '../models/lint_file_report.dart';
910
import '../models/severity.dart';
@@ -47,6 +48,27 @@ int totalSLOC(Iterable<LintFileReport> records) => records.fold(
4748
),
4849
);
4950

51+
String totalTechDebt(Iterable<LintFileReport> records) {
52+
final debtValue = records.fold<num>(
53+
0,
54+
(prevValue, fileReport) =>
55+
prevValue +
56+
(fileReport.file.metric(TechnicalDebtMetric.metricId)?.value ?? 0),
57+
);
58+
59+
final debtUnitType = records
60+
.firstWhereOrNull(
61+
(record) =>
62+
record.file.metric(TechnicalDebtMetric.metricId) != null,
63+
)
64+
?.file
65+
.metric(TechnicalDebtMetric.metricId)
66+
?.unitType ??
67+
'';
68+
69+
return debtValue > 0 ? '$debtValue $debtUnitType'.trim() : 'not found';
70+
}
71+
5072
int totalClasses(Iterable<LintFileReport> records) => records.fold(
5173
0,
5274
(prevValue, fileReport) => prevValue + fileReport.classes.keys.length,

lib/src/cli/commands/check_unused_files_command.dart

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -46,11 +46,17 @@ class CheckUnusedFilesCommand extends BaseCommand {
4646
output: stdout,
4747
)
4848
?.report(unusedFilesResult);
49+
50+
if (unusedFilesResult.isNotEmpty &&
51+
(argResults[FlagNames.fatalOnUnused] as bool)) {
52+
exit(1);
53+
}
4954
}
5055

5156
void _addFlags() {
5257
_usesReporterOption();
5358
addCommonFlags();
59+
_usesExitOption();
5460
}
5561

5662
void _usesReporterOption() {
@@ -68,4 +74,15 @@ class CheckUnusedFilesCommand extends BaseCommand {
6874
defaultsTo: FlagNames.consoleReporter,
6975
);
7076
}
77+
78+
void _usesExitOption() {
79+
argParser
80+
..addSeparator('')
81+
..addFlag(
82+
FlagNames.fatalOnUnused,
83+
help: 'Treat find unused file as fatal.',
84+
// TODO(dkrutrkikh): activate on next major version
85+
// defaultsTo: true,
86+
);
87+
}
7188
}

lib/src/cli/commands/check_unused_l10n_command.dart

Lines changed: 18 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -45,18 +45,24 @@ class CheckUnusedL10nCommand extends BaseCommand {
4545
sdkPath: findSdkPath(),
4646
);
4747

48-
return _analyzer
48+
await _analyzer
4949
.getReporter(
5050
name: reporterName,
5151
output: stdout,
5252
)
5353
?.report(unusedL10nResult);
54+
55+
if (unusedL10nResult.isNotEmpty &&
56+
(argResults[FlagNames.fatalOnUnused] as bool)) {
57+
exit(1);
58+
}
5459
}
5560

5661
void _addFlags() {
5762
_usesL10nClassPatternOption();
5863
_usesReporterOption();
5964
addCommonFlags();
65+
_usesExitOption();
6066
}
6167

6268
void _usesReporterOption() {
@@ -86,4 +92,15 @@ class CheckUnusedL10nCommand extends BaseCommand {
8692
defaultsTo: r'I18n$',
8793
);
8894
}
95+
96+
void _usesExitOption() {
97+
argParser
98+
..addSeparator('')
99+
..addFlag(
100+
FlagNames.fatalOnUnused,
101+
help: 'Treat find unused l10n as fatal.',
102+
// TODO(dkrutrkikh): activate on next major version
103+
// defaultsTo: true,
104+
);
105+
}
89106
}

lib/src/cli/models/flag_names.dart

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,4 +26,6 @@ class FlagNames {
2626
static const fatalWarnings = 'fatal-warnings';
2727

2828
static const l10nClassPattern = 'class-pattern';
29+
30+
static const fatalOnUnused = 'fatal-unused';
2931
}

test/src/analyzers/lint_analyzer/lint_analyzer_test.dart

Lines changed: 16 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -165,14 +165,25 @@ void main() {
165165
result.firstWhere((r) => r.title == 'Total scanned files').value,
166166
isZero,
167167
);
168+
169+
expect(
170+
result.firstWhere((r) => r.title == 'Total tech debt').value,
171+
equals('not found'),
172+
);
168173
});
169174

170175
test('collect summary for passed report', () {
171176
final result = analyzer.getSummary([
172177
LintFileReport(
173178
path: '/home/dev/project/bin/example.dart',
174179
relativePath: 'bin/example.dart',
175-
file: buildReportStub(),
180+
file: buildReportStub(metrics: [
181+
buildMetricValueStub(
182+
id: 'technical-debt',
183+
value: 10,
184+
unitType: 'USD',
185+
),
186+
]),
176187
classes: Map.unmodifiable(<String, Report>{}),
177188
functions: Map.unmodifiable(<String, Report>{}),
178189
issues: const [],
@@ -197,6 +208,10 @@ void main() {
197208
result.firstWhere((r) => r.title == 'Total scanned files').value,
198209
equals(2),
199210
);
211+
expect(
212+
result.firstWhere((r) => r.title == 'Total tech debt').value,
213+
equals('10 USD'),
214+
);
200215
});
201216
},
202217
testOn: 'posix',

test/src/analyzers/lint_analyzer/metrics/metrics_list/technical_debt/technical_debt_metric_test.dart

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -22,8 +22,8 @@ Future<void> main() async {
2222
'ignore-cost': 320,
2323
'ignore-for-file-cost': 396,
2424
'as-dynamic-cost': 322,
25-
'deprecated-annotations': 37,
26-
'file-nullsafety-migration': 41,
25+
'deprecated-annotations-cost': 37,
26+
'file-nullsafety-migration-cost': 41,
2727
},
2828
},
2929
);

test/src/cli/commands/check_unused_files_command_test.dart

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,9 @@ const _usage = 'Check unused *.dart files.\n'
1919
' --exclude=<{/**.g.dart,/**.template.dart}> File paths in Glob syntax to be exclude.\n'
2020
' (defaults to "{/**.g.dart,/**.template.dart}")\n'
2121
'\n'
22+
'\n'
23+
' --[no-]fatal-unused Treat find unused file as fatal.\n'
24+
'\n'
2225
'Run "metrics help" to see global options.';
2326

2427
void main() {

0 commit comments

Comments
 (0)