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

Commit 0173cf8

Browse files
authored
Merge 24dba48 into 2d324e8
2 parents 2d324e8 + 24dba48 commit 0173cf8

File tree

18 files changed

+62
-9
lines changed

18 files changed

+62
-9
lines changed

CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
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.
88
* fix: improve file metrics.
9+
* feat: add metric value unit type.
910
* chore: restrict `analyzer` version to `>=2.4.0 <2.9.0`.
1011
* chore: tune GitHub workflows.
1112

lib/src/analyzers/lint_analyzer/metrics/metrics_list/lines_of_code_metric.dart

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -62,4 +62,7 @@ class LinesOfCodeMetric extends FunctionMetric<int> {
6262
(threshold != null && value > threshold)
6363
? 'Consider breaking this $nodeType up into smaller parts.'
6464
: null;
65+
66+
@override
67+
String? unitType(int value) => value == 1 ? 'line' : 'lines';
6568
}

lib/src/analyzers/lint_analyzer/metrics/metrics_list/number_of_methods_metric.dart

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -77,4 +77,7 @@ class NumberOfMethodsMetric extends ClassMetric<int> {
7777
location: nodeLocation(node: func.declaration, source: source),
7878
))
7979
.toList(growable: false);
80+
81+
@override
82+
String? unitType(int value) => value == 1 ? 'method' : 'methods';
8083
}

lib/src/analyzers/lint_analyzer/metrics/metrics_list/source_lines_of_code/source_lines_of_code_metric.dart

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -71,6 +71,9 @@ class SourceLinesOfCodeMetric extends FunctionMetric<int> {
7171
? 'Consider breaking this $nodeType up into smaller parts.'
7272
: null;
7373

74+
@override
75+
String? unitType(int value) => value == 1 ? 'line' : 'lines';
76+
7477
Iterable<ContextMessage> _context(
7578
Iterable<int> linesWithCode,
7679
InternalResolvedUnitResult source,

lib/src/analyzers/lint_analyzer/metrics/models/metric.dart

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -65,6 +65,7 @@ abstract class Metric<T extends num> {
6565
metricsId: id,
6666
documentation: documentation,
6767
value: result.value,
68+
unitType: unitType(result.value),
6869
level: _levelComputer(result.value, threshold),
6970
comment: commentMessage(type, result.value, threshold),
7071
recommendation: recommendationMessage(type, result.value, threshold),
@@ -98,4 +99,8 @@ abstract class Metric<T extends num> {
9899
Iterable<ScopedClassDeclaration> classDeclarations,
99100
Iterable<ScopedFunctionDeclaration> functionDeclarations,
100101
);
102+
103+
/// Returns the human readable unit type.
104+
@protected
105+
String? unitType(T value) => null;
101106
}

lib/src/analyzers/lint_analyzer/metrics/models/metric_value.dart

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,9 @@ class MetricValue<T> {
1616
/// The actual value computed by the metric.
1717
final T value;
1818

19+
/// The human readable unit type.
20+
final String? unitType;
21+
1922
/// The level of this value computed by the metric.
2023
final MetricValueLevel level;
2124

@@ -41,6 +44,7 @@ class MetricValue<T> {
4144
required this.metricsId,
4245
required this.documentation,
4346
required this.value,
47+
this.unitType,
4448
required this.level,
4549
required this.comment,
4650
this.recommendation,

lib/src/analyzers/lint_analyzer/reporters/reporters_list/console/lint_console_reporter_helper.dart

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -57,9 +57,9 @@ class LintConsoleReporterHelper {
5757
final color = _colorPens[metric.level];
5858

5959
if (color != null) {
60-
final value = metric.value.toInt();
60+
final value = '${metric.value.toInt()} ${metric.unitType ?? ''}'.trim();
6161

62-
return '${metric.documentation.name.toLowerCase()}: ${color('$value')}';
62+
return '${metric.documentation.name.toLowerCase()}: ${color(value)}';
6363
}
6464

6565
throw StateError('Unexpected violation level.');

lib/src/analyzers/lint_analyzer/reporters/reporters_list/html/components/report_details_tooltip.dart

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,8 @@ Element renderDetailsTooltipMetric(MetricValue<num> metric) {
3838
..attributes['rel'] = 'noopener noreferrer'
3939
..attributes['title'] = metricName
4040
..text = '$metricName:&nbsp;')
41-
..append(Element.tag('span')..text = metric.value.round().toString()))
41+
..append(Element.tag('span')
42+
..text = '${metric.value.round()} ${metric.unitType ?? ''}'.trim()))
4243
..append(Element.tag('p')
4344
..classes.add('metrics-source-code__tooltip-text')
4445
..append(Element.tag('span')

lib/src/analyzers/lint_analyzer/reporters/reporters_list/json/lint_json_reporter.dart

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -100,11 +100,13 @@ class LintJsonReporter
100100
Iterable<MetricValue<num>> metrics,
101101
) =>
102102
metrics.map((metric) {
103+
final unitType = metric.unitType;
103104
final recommendation = metric.recommendation;
104105

105106
return {
106107
'metricsId': metric.metricsId,
107108
'value': metric.value,
109+
if (unitType != null) 'unitType': unitType,
108110
'level': metric.level.toString(),
109111
'comment': metric.comment,
110112
if (recommendation != null) 'recommendation': recommendation,

test/src/analyzers/lint_analyzer/metrics/metrics_list/lines_of_code_metric_test.dart

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,7 @@ Future<void> main() async {
3030

3131
expect(metricValue.metricsId, equals(metric.id));
3232
expect(metricValue.value, equals(5));
33+
expect(metricValue.unitType, equals('lines'));
3334
expect(metricValue.level, equals(MetricValueLevel.noted));
3435
expect(
3536
metricValue.comment,
@@ -50,6 +51,7 @@ Future<void> main() async {
5051

5152
expect(metricValue.metricsId, equals(metric.id));
5253
expect(metricValue.value, equals(2));
54+
expect(metricValue.unitType, equals('lines'));
5355
expect(metricValue.level, equals(MetricValueLevel.none));
5456
expect(
5557
metricValue.comment,
@@ -70,6 +72,7 @@ Future<void> main() async {
7072

7173
expect(metricValue.metricsId, equals(metric.id));
7274
expect(metricValue.value, equals(12));
75+
expect(metricValue.unitType, equals('lines'));
7376
expect(metricValue.level, equals(MetricValueLevel.warning));
7477
expect(
7578
metricValue.comment,

test/src/analyzers/lint_analyzer/metrics/metrics_list/source_lines_of_code/source_lines_of_code_metric_test.dart

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,7 @@ Future<void> main() async {
3131

3232
expect(metricValue.metricsId, equals(metric.id));
3333
expect(metricValue.value, equals(1));
34+
expect(metricValue.unitType, equals('line'));
3435
expect(metricValue.level, equals(MetricValueLevel.none));
3536
expect(
3637
metricValue.comment,
@@ -56,6 +57,7 @@ Future<void> main() async {
5657

5758
expect(metricValue.metricsId, equals(metric.id));
5859
expect(metricValue.value, equals(6));
60+
expect(metricValue.unitType, equals('lines'));
5961
expect(metricValue.level, equals(MetricValueLevel.warning));
6062
expect(
6163
metricValue.comment,
@@ -81,6 +83,7 @@ Future<void> main() async {
8183

8284
expect(metricValue.metricsId, equals(metric.id));
8385
expect(metricValue.value, equals(4));
86+
expect(metricValue.unitType, equals('lines'));
8487
expect(metricValue.level, equals(MetricValueLevel.none));
8588
expect(
8689
metricValue.comment,

test/src/analyzers/lint_analyzer/reporters/reporters_list/console/lint_console_reporter_helper_test.dart

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -81,8 +81,12 @@ void main() {
8181

8282
test('getMetricReport returns formatted message', () {
8383
expect(
84-
helper.getMetricReport(buildMetricValueStub(id: 'metricId', value: 12)),
85-
equals('metricid: \x1B[38;5;7m12\x1B[0m'),
84+
helper.getMetricReport(buildMetricValueStub(
85+
id: 'metricId',
86+
value: 12,
87+
unitType: 'units',
88+
)),
89+
equals('metricid: \x1B[38;5;7m12 units\x1B[0m'),
8690
);
8791
});
8892
});

test/src/analyzers/lint_analyzer/reporters/reporters_list/console/lint_console_reporter_test.dart

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -52,13 +52,13 @@ void main() {
5252
equals(
5353
[
5454
'test/resources/abstract_class.dart:',
55-
'\x1B[38;5;11mWarning \x1B[0mmetric1: \x1B[38;5;11m100\x1B[0m',
55+
'\x1B[38;5;11mWarning \x1B[0mmetric1: \x1B[38;5;11m100 units\x1B[0m',
5656
'\x1B[38;5;9mAlarm \x1B[0mclass.constructor - metric2: \x1B[38;5;9m10\x1B[0m',
5757
'',
5858
'test/resources/class_with_factory_constructors.dart:',
5959
'\x1B[38;5;11mWarning \x1B[0msimple message : 0:0 : id',
6060
'\x1B[38;5;4mStyle \x1B[0msimple design message : 0:0 : designId',
61-
'\x1B[38;5;11mWarning \x1B[0mfunction - metric4: \x1B[38;5;11m5\x1B[0m',
61+
'\x1B[38;5;11mWarning \x1B[0mfunction - metric4: \x1B[38;5;11m5 units\x1B[0m',
6262
'',
6363
],
6464
),
@@ -68,7 +68,7 @@ void main() {
6868
equals(
6969
[
7070
'test/resources/abstract_class.dart:',
71-
'\x1B[38;5;11mWarning \x1B[0mmetric1: \x1B[38;5;11m100\x1B[0m',
71+
'\x1B[38;5;11mWarning \x1B[0mmetric1: \x1B[38;5;11m100 units\x1B[0m',
7272
'\x1B[38;5;7m \x1B[0mclass - metric1: \x1B[38;5;7m0\x1B[0m',
7373
'\x1B[38;5;9mAlarm \x1B[0mclass.constructor - metric2: \x1B[38;5;9m10\x1B[0m',
7474
'\x1B[38;5;7m \x1B[0mclass.method - metric3: \x1B[38;5;7m1\x1B[0m',
@@ -77,7 +77,7 @@ void main() {
7777
'\x1B[38;5;7m \x1B[0mmetric1: \x1B[38;5;7m0\x1B[0m, metric2: \x1B[38;5;7m1\x1B[0m',
7878
'\x1B[38;5;11mWarning \x1B[0msimple message : 0:0 : id',
7979
'\x1B[38;5;4mStyle \x1B[0msimple design message : 0:0 : designId',
80-
'\x1B[38;5;11mWarning \x1B[0mfunction - metric4: \x1B[38;5;11m5\x1B[0m',
80+
'\x1B[38;5;11mWarning \x1B[0mfunction - metric4: \x1B[38;5;11m5 units\x1B[0m',
8181
'',
8282
],
8383
),

test/src/analyzers/lint_analyzer/reporters/reporters_list/html/components/report_details_tooltip_test.dart

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -50,6 +50,20 @@ void main() {
5050
'<div class="metrics-source-code__tooltip-section"><p class="metrics-source-code__tooltip-text"><a class="metrics-source-code__tooltip-link" href="https://dartcodemetrics.dev/docs/metrics/metric" target="_blank" rel="noopener noreferrer" title="metric">metric:&amp;nbsp;</a><span>10</span></p><p class="metrics-source-code__tooltip-text"><span class="metrics-source-code__tooltip-label">metric violation level:&amp;nbsp;</span><span class="metrics-source-code__tooltip-level metrics-source-code__tooltip-level--warning">warning</span></p></div>',
5151
),
5252
);
53+
54+
expect(
55+
renderDetailsTooltipMetric(
56+
buildMetricValueStub(
57+
id: 'metric',
58+
value: 10,
59+
unitType: 'units',
60+
level: MetricValueLevel.warning,
61+
),
62+
).outerHtml,
63+
equals(
64+
'<div class="metrics-source-code__tooltip-section"><p class="metrics-source-code__tooltip-text"><a class="metrics-source-code__tooltip-link" href="https://dartcodemetrics.dev/docs/metrics/metric" target="_blank" rel="noopener noreferrer" title="metric">metric:&amp;nbsp;</a><span>10 units</span></p><p class="metrics-source-code__tooltip-text"><span class="metrics-source-code__tooltip-label">metric violation level:&amp;nbsp;</span><span class="metrics-source-code__tooltip-level metrics-source-code__tooltip-level--warning">warning</span></p></div>',
65+
),
66+
);
5367
},
5468
);
5569
});

test/src/analyzers/lint_analyzer/reporters/reporters_list/json/lint_json_reporter_test.dart

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -44,6 +44,7 @@ void main() {
4444
{
4545
'metricsId': 'file-metric-id',
4646
'value': 100,
47+
'unitType': 'units',
4748
'level': 'warning',
4849
'comment': 'metric comment',
4950
'context': <String>[],

test/src/analyzers/lint_analyzer/reporters/reporters_list/report_example.dart

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,7 @@ final _file1Report = Report(
3434
recomendedThreshold: 0,
3535
),
3636
value: 100,
37+
unitType: 'units',
3738
level: MetricValueLevel.warning,
3839
comment: 'metric comment',
3940
),
@@ -119,6 +120,7 @@ final _function3Report = Report(
119120
recomendedThreshold: 0,
120121
),
121122
value: 5,
123+
unitType: 'units',
122124
level: MetricValueLevel.warning,
123125
comment: 'metric comment',
124126
),

test/stubs_builders.dart

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@ class _DeclarationMock extends Mock implements Declaration {}
1818
MetricValue<T> buildMetricValueStub<T>({
1919
required String id,
2020
required T value,
21+
String? unitType,
2122
EntityType type = EntityType.methodEntity,
2223
MetricValueLevel level = MetricValueLevel.none,
2324
}) =>
@@ -30,6 +31,7 @@ MetricValue<T> buildMetricValueStub<T>({
3031
recomendedThreshold: 0,
3132
),
3233
value: value,
34+
unitType: unitType,
3335
level: level,
3436
comment: '',
3537
);

website/docs/cli/analyze.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -206,6 +206,7 @@ The reporter prints a single JSON object containing meta information and the vio
206206

207207
- `metricsId` - an id of the computed metric
208208
- `value` - an actual value computed by the metric
209+
- `unitType` - a human readable unit type _(optional)_
209210
- `level` - a level of the value computed by the metric
210211
- `comment` - a message with information about the value
211212
- `recommendation` - a message with information about how the user can improve the value _(optional)_
@@ -215,6 +216,7 @@ The reporter prints a single JSON object containing meta information and the vio
215216
{
216217
"metricsId": "number-of-methods",
217218
"value": 14,
219+
"unitType": "methods",
218220
"level": "warning",
219221
"comment": "This class has 14 methods, which exceeds the maximum of 10 allowed.",
220222
"recommendation": "Consider breaking this class up into smaller parts.",

0 commit comments

Comments
 (0)