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

Commit 771f4f4

Browse files
authored
fix: ‘Number of Parameters’ skip copyWith methods. (#489)
1 parent de4aa2c commit 771f4f4

File tree

4 files changed

+57
-6
lines changed

4 files changed

+57
-6
lines changed

CHANGELOG.md

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -6,9 +6,10 @@
66
* feat: add static code diagnostic `prefer-const-border-radius`.
77
* feat: improve static code diagnostic `prefer-extracting-callbacks`: don't trigger on empty function blocks and ignore Flutter builder functions.
88
* feat: improve unused files check, add support for `vm:entry-point` annotation.
9-
* fix: compute NumberOfParametersMetric only for functions and methods.
10-
* fix: skip synthetic tokens while compute 'Source lines of Code'.
11-
* fix: update 'Maintainability Index' metric comment message.
9+
* fix: compute `Number of Parameters` only for functions and methods.
10+
* fix: `Number of Parameters` skip copyWith methods.
11+
* fix: skip synthetic tokens while compute `Source lines of Code`.
12+
* fix: update `Maintainability Index` metric comment message.
1213

1314
## 4.3.3
1415

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

Lines changed: 18 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
import 'package:analyzer/dart/ast/ast.dart';
2+
import 'package:collection/collection.dart';
23

34
import '../../models/entity_type.dart';
45
import '../../models/internal_resolved_unit_result.dart';
@@ -40,8 +41,23 @@ class NumberOfParametersMetric extends FunctionMetric<int> {
4041
Iterable<ScopedFunctionDeclaration> functionDeclarations,
4142
InternalResolvedUnitResult source,
4243
Iterable<MetricValue<num>> otherMetricsValues,
43-
) =>
44-
node is FunctionDeclaration || node is MethodDeclaration;
44+
) {
45+
if (node is FunctionDeclaration) {
46+
return true;
47+
} else if (node is MethodDeclaration) {
48+
final className = functionDeclarations
49+
.firstWhereOrNull((declaration) => declaration.declaration == node)
50+
?.enclosingDeclaration
51+
?.name;
52+
53+
return node.name.name != 'copyWith' ||
54+
className == null ||
55+
className !=
56+
node.returnType?.type?.getDisplayString(withNullability: true);
57+
}
58+
59+
return false;
60+
}
4561

4662
@override
4763
MetricComputationResult<int> computeImplementation(

test/analyzers/lint_analyzer/metrics/metrics_list/number_of_parameters_metric_test.dart

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -131,7 +131,17 @@ Future<void> main() async {
131131
});
132132

133133
test('supports returns only for functions and methods', () {
134-
const supports = [true, true, true, true, false, true, true];
134+
const supports = [
135+
true,
136+
true,
137+
true,
138+
true,
139+
false,
140+
true,
141+
true,
142+
false,
143+
false,
144+
];
135145

136146
assert(
137147
scopeVisitor.functions.length == supports.length,

test/resources/number_of_parameters_metric_example.dart

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -45,3 +45,27 @@ class NumberOfParametersMetric extends FunctionMetric<int> {
4545
return MetricComputationResult(value: parametersCount ?? 0);
4646
}
4747
}
48+
49+
class Foo {
50+
final String a;
51+
final String b;
52+
final String c;
53+
final String d;
54+
final String e;
55+
56+
const Foo({
57+
required this.a,
58+
required this.b,
59+
required this.c,
60+
required this.d,
61+
required this.e,
62+
});
63+
64+
Foo copyWith({String a, String b, String c, String d, String e}) => Foo(
65+
a: a ?? this.a,
66+
b: b ?? this.b,
67+
c: c ?? this.c,
68+
d: d ?? this.d,
69+
e: e ?? this.e,
70+
);
71+
}

0 commit comments

Comments
 (0)