Skip to content

Commit 5ac6188

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

File tree

2 files changed

+54
-8
lines changed

2 files changed

+54
-8
lines changed

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

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

88
import '../analyzer.dart';
9+
import '../extensions.dart';
910

1011
const _desc = r'Prefer typing uninitialized variables and fields.';
1112

@@ -88,16 +89,13 @@ class _Visitor extends SimpleAstVisitor<void> {
8889

8990
@override
9091
void visitVariableDeclarationList(VariableDeclarationList node) {
91-
if (node.type != null) {
92-
return;
93-
}
94-
95-
var code = node.parent is FieldDeclaration
96-
? PreferTypingUninitializedVariables.forField
97-
: PreferTypingUninitializedVariables.forVariable;
92+
if (node.type != null) return;
9893

9994
for (var v in node.variables) {
100-
if (v.initializer == null) {
95+
if (v.initializer == null && !v.isAugmentation) {
96+
var code = node.parent is FieldDeclaration
97+
? PreferTypingUninitializedVariables.forField
98+
: PreferTypingUninitializedVariables.forVariable;
10199
rule.reportLint(v, errorCode: code);
102100
}
103101
}

pkg/linter/test/rules/prefer_typing_uninitialized_variables_test.dart

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

20+
test_field_augmented() async {
21+
var a = newFile('$testPackageLibPath/a.dart', r'''
22+
import augment 'b.dart';
23+
24+
class A {
25+
var x;
26+
}
27+
''');
28+
29+
var b = newFile('$testPackageLibPath/b.dart', r'''
30+
augment library 'a.dart';
31+
32+
augment class A {
33+
augment var x;
34+
}
35+
''');
36+
37+
result = await resolveFile(a.path);
38+
await assertDiagnosticsIn(errors, [
39+
lint(42, 1),
40+
]);
41+
42+
result = await resolveFile(b.path);
43+
await assertNoDiagnosticsIn(errors);
44+
}
45+
2046
test_field_final_noInitializer() async {
2147
await assertDiagnostics(r'''
2248
class C {
@@ -105,6 +131,28 @@ void f() {
105131
]);
106132
}
107133

134+
test_topLevelVariable_augmented() async {
135+
var a = newFile('$testPackageLibPath/a.dart', r'''
136+
import augment 'b.dart';
137+
138+
var x;
139+
''');
140+
141+
var b = newFile('$testPackageLibPath/b.dart', r'''
142+
augment library 'a.dart';
143+
144+
augment var x;
145+
''');
146+
147+
result = await resolveFile(a.path);
148+
await assertDiagnosticsIn(errors, [
149+
lint(30, 1),
150+
]);
151+
152+
result = await resolveFile(b.path);
153+
await assertNoDiagnosticsIn(errors);
154+
}
155+
108156
test_topLevelVariable_var_initializer() async {
109157
await assertNoDiagnostics(r'''
110158
var x = 4;

0 commit comments

Comments
 (0)