Skip to content

Commit 1b46b26

Browse files
pqCommit Queue
authored and
Commit Queue
committed
augmentation support for avoid_classes_with_only_static_members
Fixes: https://github.com/dart-lang/linter/issues/4931 Change-Id: I4194dd6e4d8790998b202bdb654b9a9d7a114104 Reviewed-on: https://dart-review.googlesource.com/c/sdk/+/363422 Reviewed-by: Brian Wilkerson <[email protected]> Commit-Queue: Phil Quitslund <[email protected]>
1 parent defa4d7 commit 1b46b26

File tree

2 files changed

+88
-15
lines changed

2 files changed

+88
-15
lines changed

pkg/linter/lib/src/rules/avoid_classes_with_only_static_members.dart

Lines changed: 15 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ import 'package:analyzer/dart/ast/visitor.dart';
77
import 'package:analyzer/dart/element/element.dart';
88

99
import '../analyzer.dart';
10+
import '../extensions.dart';
1011

1112
const _desc = r'Avoid defining a class that contains only static members.';
1213

@@ -43,18 +44,6 @@ const _favoriteMammal = 'weasel';
4344
4445
''';
4546

46-
bool _isNonConst(FieldElement element) => !element.isConst;
47-
48-
bool _isStaticMember(ClassMember classMember) {
49-
if (classMember is MethodDeclaration) {
50-
return classMember.isStatic;
51-
}
52-
if (classMember is FieldDeclaration) {
53-
return classMember.isStatic;
54-
}
55-
return false;
56-
}
57-
5847
class AvoidClassesWithOnlyStaticMembers extends LintRule {
5948
static const LintCode code = LintCode(
6049
'avoid_classes_with_only_static_members',
@@ -103,9 +92,20 @@ class _Visitor extends SimpleAstVisitor<void> {
10392
}
10493
}
10594

106-
if (node.members.isNotEmpty &&
107-
node.members.every(_isStaticMember) &&
108-
(element.methods.isNotEmpty || element.fields.any(_isNonConst))) {
95+
var declaredElement = node.declaredElement;
96+
if (declaredElement == null) return;
97+
98+
var constructors = declaredElement.allConstructors;
99+
if (constructors.isNotEmpty &&
100+
constructors.any((c) => !c.isDefaultConstructor)) {
101+
return;
102+
}
103+
104+
var methods = declaredElement.allMethods;
105+
if (methods.isNotEmpty && !methods.every((m) => m.isStatic)) return;
106+
107+
if (methods.isNotEmpty ||
108+
declaredElement.allFields.any((f) => !f.isConst)) {
109109
rule.reportLint(node);
110110
}
111111
}

pkg/linter/test/rules/avoid_classes_with_only_static_members_test.dart

Lines changed: 73 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,79 @@ class AvoidClassesWithOnlyStaticMembers extends LintRuleTest {
1717
@override
1818
String get lintRule => 'avoid_classes_with_only_static_members';
1919

20+
test_augmentationClass_nonStaticField() async {
21+
var a = newFile('$testPackageLibPath/a.dart', r'''
22+
import augment 'b.dart';
23+
24+
class A {
25+
static int f = 1;
26+
}
27+
''');
28+
29+
// The added field should prevent a lint above.
30+
var b = newFile('$testPackageLibPath/b.dart', r'''
31+
augment library 'a.dart';
32+
33+
augment class A {
34+
int a = 1;
35+
}
36+
''');
37+
38+
result = await resolveFile(a.path);
39+
await assertNoDiagnosticsIn(errors);
40+
41+
result = await resolveFile(b.path);
42+
await assertNoDiagnosticsIn(errors);
43+
}
44+
45+
test_augmentationClass_staticField() async {
46+
var a = newFile('$testPackageLibPath/a.dart', r'''
47+
import augment 'b.dart';
48+
49+
class A {}
50+
''');
51+
52+
var b = newFile('$testPackageLibPath/b.dart', r'''
53+
augment library 'a.dart';
54+
55+
augment class A {
56+
static int f = 1;
57+
}
58+
''');
59+
60+
result = await resolveFile(a.path);
61+
await assertDiagnosticsIn(errors, [
62+
lint(26, 10),
63+
]);
64+
65+
result = await resolveFile(b.path);
66+
await assertNoDiagnosticsIn(errors);
67+
}
68+
69+
test_augmentationClass_staticMethod() async {
70+
var a = newFile('$testPackageLibPath/a.dart', r'''
71+
import augment 'b.dart';
72+
73+
class A {}
74+
''');
75+
76+
var b = newFile('$testPackageLibPath/b.dart', r'''
77+
augment library 'a.dart';
78+
79+
augment class A {
80+
static void m() {}
81+
}
82+
''');
83+
84+
result = await resolveFile(a.path);
85+
await assertDiagnosticsIn(errors, [
86+
lint(26, 10),
87+
]);
88+
89+
result = await resolveFile(b.path);
90+
await assertNoDiagnosticsIn(errors);
91+
}
92+
2093
test_basicClass() async {
2194
await assertDiagnostics(r'''
2295
class C {

0 commit comments

Comments
 (0)