Skip to content

Commit 0b069f1

Browse files
pqCommit Queue
authored and
Commit Queue
committed
augmentation support for avoid_annotating_with_dynamic
Fixes: https://github.com/dart-lang/linter/issues/4953 Change-Id: I637682c1b6ef45668259354cafe3c6d8e4bcafda Reviewed-on: https://dart-review.googlesource.com/c/sdk/+/363945 Reviewed-by: Brian Wilkerson <[email protected]> Commit-Queue: Phil Quitslund <[email protected]>
1 parent 5ac6188 commit 0b069f1

File tree

2 files changed

+156
-0
lines changed

2 files changed

+156
-0
lines changed

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

Lines changed: 16 additions & 0 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/type.dart';
88

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

1112
const _desc = r'Avoid annotating with dynamic when not required.';
1213

@@ -82,8 +83,23 @@ class _Visitor extends SimpleAstVisitor<void> {
8283
}
8384

8485
void _checkNode(NormalFormalParameter node, TypeAnnotation? type) {
86+
if (node.inAugmentation) return;
87+
8588
if (type is NamedType && type.type is DynamicType) {
8689
rule.reportLint(node);
8790
}
8891
}
8992
}
93+
94+
extension on AstNode {
95+
bool get inAugmentation {
96+
AstNode? target = this;
97+
while (target != null) {
98+
if (target.isAugmentation) return true;
99+
if (target is Block) return false;
100+
if (target is Declaration) return false;
101+
target = target.parent;
102+
}
103+
return false;
104+
}
105+
}

pkg/linter/test/rules/avoid_annotating_with_dynamic_test.dart

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

20+
test_augmentationClass() async {
21+
var a = newFile('$testPackageLibPath/a.dart', r'''
22+
import augment 'b.dart';
23+
24+
class A { }
25+
''');
26+
27+
var b = newFile('$testPackageLibPath/b.dart', r'''
28+
augment library 'a.dart';
29+
30+
augment class A {
31+
void f(dynamic o) { }
32+
}
33+
''');
34+
35+
result = await resolveFile(a.path);
36+
await assertNoDiagnosticsIn(errors);
37+
38+
result = await resolveFile(b.path);
39+
await assertDiagnosticsIn(errors, [
40+
lint(54, 9),
41+
]);
42+
}
43+
44+
test_augmentationTopLevelFunction() async {
45+
var a = newFile('$testPackageLibPath/a.dart', r'''
46+
import augment 'b.dart';
47+
''');
48+
49+
var b = newFile('$testPackageLibPath/b.dart', r'''
50+
augment library 'a.dart';
51+
52+
void f(dynamic o) { }
53+
''');
54+
55+
result = await resolveFile(a.path);
56+
await assertNoDiagnosticsIn(errors);
57+
58+
result = await resolveFile(b.path);
59+
await assertDiagnosticsIn(errors, [
60+
lint(34, 9),
61+
]);
62+
}
63+
64+
test_augmentationTopLevelFunction_localDynamic() async {
65+
var a = newFile('$testPackageLibPath/a.dart', r'''
66+
import augment 'b.dart';
67+
68+
void f(int i) {}
69+
''');
70+
71+
var b = newFile('$testPackageLibPath/b.dart', r'''
72+
augment library 'a.dart';
73+
74+
augment void f(int i) {
75+
var g = (dynamic x) {};
76+
g(i);
77+
}
78+
''');
79+
80+
result = await resolveFile(a.path);
81+
await assertNoDiagnosticsIn(errors);
82+
83+
result = await resolveFile(b.path);
84+
await assertDiagnosticsIn(errors, [
85+
lint(62, 9),
86+
]);
87+
}
88+
89+
test_augmentedMethod() async {
90+
var a = newFile('$testPackageLibPath/a.dart', r'''
91+
import augment 'b.dart';
92+
93+
class A {
94+
void f(dynamic o) { }
95+
}
96+
''');
97+
98+
var b = newFile('$testPackageLibPath/b.dart', r'''
99+
augment library 'a.dart';
100+
101+
augment class A {
102+
augment void f(dynamic o) { }
103+
}
104+
''');
105+
106+
result = await resolveFile(a.path);
107+
await assertDiagnosticsIn(errors, [
108+
lint(45, 9),
109+
]);
110+
111+
result = await resolveFile(b.path);
112+
await assertNoDiagnosticsIn(errors);
113+
}
114+
115+
test_augmentedTopLevelFunction() async {
116+
var a = newFile('$testPackageLibPath/a.dart', r'''
117+
import augment 'b.dart';
118+
119+
void f(dynamic o) { }
120+
''');
121+
122+
var b = newFile('$testPackageLibPath/b.dart', r'''
123+
augment library 'a.dart';
124+
125+
augment void f(dynamic o) { }
126+
''');
127+
128+
result = await resolveFile(a.path);
129+
await assertDiagnosticsIn(errors, [
130+
lint(33, 9),
131+
]);
132+
133+
result = await resolveFile(b.path);
134+
await assertNoDiagnosticsIn(errors);
135+
}
136+
137+
test_augmentedTopLevelFunction_multiple() async {
138+
var a = newFile('$testPackageLibPath/a.dart', r'''
139+
import augment 'b.dart';
140+
141+
void f(dynamic o) { }
142+
''');
143+
144+
var b = newFile('$testPackageLibPath/b.dart', r'''
145+
augment library 'a.dart';
146+
147+
augment void f(dynamic o) { }
148+
augment void f(dynamic o) { }
149+
''');
150+
151+
result = await resolveFile(a.path);
152+
await assertDiagnosticsIn(errors, [
153+
lint(33, 9),
154+
]);
155+
156+
result = await resolveFile(b.path);
157+
await assertNoDiagnosticsIn(errors);
158+
}
159+
20160
// TODO(srawlins): Test parameter of function-typed typedef (both old and
21161
// new style).
22162
// Test parameter of function-typed parameter (`f(void g(dynamic x))`).

0 commit comments

Comments
 (0)