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

Commit 2d324e8

Browse files
authored
fix: improve file metrics (#579)
1 parent 05cd509 commit 2d324e8

File tree

5 files changed

+69
-5
lines changed

5 files changed

+69
-5
lines changed

CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
* feat: add alphabetical sorting by type for `member-ordering-extended` rule.
66
* feat: add support mixins, extensions and enums for `prefer-match-file-name` rule.
77
* fix: prefer conditional expressions rule breaks code with increment / decrement operators.
8+
* fix: improve file metrics.
89
* chore: restrict `analyzer` version to `>=2.4.0 <2.9.0`.
910
* chore: tune GitHub workflows.
1011

lib/src/analyzers/lint_analyzer/lint_analyzer.dart

Lines changed: 24 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -194,7 +194,7 @@ class LintAnalyzer {
194194
final classMetrics =
195195
_checkClassMetrics(visitor, internalResult, config);
196196

197-
final fileMetrics = _checkFileMetrics(internalResult);
197+
final fileMetrics = _checkFileMetrics(visitor, internalResult, config);
198198

199199
final functionMetrics =
200200
_checkFunctionMetrics(visitor, internalResult, config);
@@ -322,9 +322,31 @@ class LintAnalyzer {
322322
return classRecords;
323323
}
324324

325-
Report _checkFileMetrics(InternalResolvedUnitResult source) {
325+
Report _checkFileMetrics(
326+
ScopeVisitor visitor,
327+
InternalResolvedUnitResult source,
328+
LintAnalysisConfig config,
329+
) {
326330
final metrics = <MetricValue<num>>[];
327331

332+
for (final metric in config.fileMetrics) {
333+
if (metric.supports(
334+
source.unit,
335+
visitor.classes,
336+
visitor.functions,
337+
source,
338+
metrics,
339+
)) {
340+
metrics.add(metric.compute(
341+
source.unit,
342+
visitor.classes,
343+
visitor.functions,
344+
source,
345+
metrics,
346+
));
347+
}
348+
}
349+
328350
return Report(
329351
location: nodeLocation(node: source.unit, source: source),
330352
declaration: source.unit,

lib/src/analyzers/lint_analyzer/metrics/metric_utils.dart

Lines changed: 12 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -15,12 +15,21 @@ Uri documentation(String metricId) => Uri(
1515
T? readNullableThreshold<T extends num>(
1616
Map<String, Object?> config,
1717
String metricId,
18-
) {
18+
) =>
19+
readConfigValue<T>(config, metricId, 'threshold') ??
20+
readConfigValue<T>(config, metricId);
21+
22+
/// Returns a nullable value from [config] for metric with [metricId]
23+
T? readConfigValue<T extends Object>(
24+
Map<String, Object?> config,
25+
String metricId, [
26+
String? valueName,
27+
]) {
1928
final metricConfig = config[metricId];
2029

2130
final configValue = metricConfig is Map<String, Object?>
22-
? metricConfig['threshold']?.toString()
23-
: metricConfig?.toString();
31+
? metricConfig[valueName]?.toString()
32+
: (valueName == null ? metricConfig?.toString() : null);
2433

2534
if (configValue != null && T == int) {
2635
return int.tryParse(configValue) as T?;

test/src/analyzers/lint_analyzer/metrics/metric_utils_test.dart

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -55,6 +55,32 @@ void main() {
5555
expect(readNullableThreshold<int>(_config, metricId5), isNull);
5656
});
5757

58+
test('readConfigValue returns a value from Map based config', () {
59+
const metricId3 = 'metric-id-3';
60+
const metricId4 = 'metric-id-4';
61+
const metricId5 = 'metric-id-5';
62+
63+
const metricId1Value = 10;
64+
const metricId2Value = 0.5;
65+
66+
const _config = {
67+
metricId1: '$metricId1Value',
68+
metricId2: {'value': '$metricId1Value'},
69+
metricId3: {'value': '$metricId2Value'},
70+
metricId4: null,
71+
};
72+
73+
expect(readConfigValue<int>(_config, metricId1, 'value'), isNull);
74+
75+
expect(readConfigValue<int>(_config, metricId2, 'value'), equals(10));
76+
77+
expect(readConfigValue<double>(_config, metricId3, 'value'), equals(0.5));
78+
79+
expect(readConfigValue<double>(_config, metricId4, 'value'), isNull);
80+
81+
expect(readConfigValue<int>(_config, metricId5, 'value'), isNull);
82+
});
83+
5884
test('valueLevel returns a level of passed value', () {
5985
expect(valueLevel(null, 10), equals(MetricValueLevel.none));
6086
expect(valueLevel(30, null), equals(MetricValueLevel.none));

test/src/analyzers/lint_analyzer/rules/rules_factory_test.dart

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -40,15 +40,20 @@ void main() {
4040
'avoid-nested-conditional-expressions': <String, Object>{},
4141
'prefer-const-border-radius': <String, Object>{},
4242
'prefer-correct-identifier-length': <String, Object>{},
43+
'avoid-missing-enum-constant-in-map': <String, Object>{},
44+
'avoid-throw-in-catch-block': <String, Object>{},
45+
'prefer-correct-type-name': <String, Object>{},
4346
}).map((rule) => rule.id),
4447
equals([
4548
'always-remove-listener',
4649
'avoid-ignoring-return-values',
4750
'avoid-late-keyword',
51+
'avoid-missing-enum-constant-in-map',
4852
'avoid-nested-conditional-expressions',
4953
'avoid-non-null-assertion',
5054
'avoid-preserve-whitespace-false',
5155
'avoid-returning-widgets',
56+
'avoid-throw-in-catch-block',
5257
'avoid-unnecessary-setstate',
5358
'avoid-unused-parameters',
5459
'avoid-wrapping-in-padding',
@@ -67,6 +72,7 @@ void main() {
6772
'prefer-conditional-expressions',
6873
'prefer-const-border-radius',
6974
'prefer-correct-identifier-length',
75+
'prefer-correct-type-name',
7076
'prefer-extracting-callbacks',
7177
'prefer-intl-name',
7278
'prefer-match-file-name',

0 commit comments

Comments
 (0)