Skip to content

Commit 7536d51

Browse files
pqCommit Queue
authored and
Commit Queue
committed
augmentation support for always_declare_return_types
Fixes: https://github.com/dart-lang/linter/issues/4951 Change-Id: I451195fd32ffd69f5e88c3587dc5bc0a28815fc3 Reviewed-on: https://dart-review.googlesource.com/c/sdk/+/363943 Reviewed-by: Brian Wilkerson <[email protected]> Commit-Queue: Phil Quitslund <[email protected]>
1 parent 22da6ed commit 7536d51

File tree

3 files changed

+141
-2
lines changed

3 files changed

+141
-2
lines changed

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

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

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

1112
const _desc = r'Declare method return types.';
1213

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

8990
@override
9091
void visitFunctionDeclaration(FunctionDeclaration node) {
91-
if (!node.isSetter && node.returnType == null) {
92+
if (!node.isSetter && node.returnType == null && !node.isAugmentation) {
9293
rule.reportLintForToken(node.name,
9394
arguments: [node.name.lexeme], errorCode: functionCode);
9495
}
@@ -106,7 +107,8 @@ class _Visitor extends SimpleAstVisitor<void> {
106107
void visitMethodDeclaration(MethodDeclaration node) {
107108
if (!node.isSetter &&
108109
node.returnType == null &&
109-
node.name.type != TokenType.INDEX_EQ) {
110+
node.name.type != TokenType.INDEX_EQ &&
111+
!node.isAugmentation) {
110112
rule.reportLintForToken(node.name,
111113
arguments: [node.name.lexeme], errorCode: methodCode);
112114
}

pkg/linter/test/rules/all.dart

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44

55
// ignore_for_file: library_prefixes
66

7+
import 'always_declare_return_types_test.dart' as always_declare_return_types;
78
import 'always_specify_types_test.dart' as always_specify_types;
89
import 'always_use_package_imports_test.dart' as always_use_package_imports;
910
import 'annotate_overrides_test.dart' as annotate_overrides;
@@ -267,6 +268,7 @@ import 'valid_regexps_test.dart' as valid_regexps;
267268
import 'void_checks_test.dart' as void_checks;
268269

269270
void main() {
271+
always_declare_return_types.main();
270272
always_specify_types.main();
271273
always_use_package_imports.main();
272274
annotate_overrides.main();
Lines changed: 135 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,135 @@
1+
// Copyright (c) 2024, the Dart project authors. Please see the AUTHORS file
2+
// for details. All rights reserved. Use of this source code is governed by a
3+
// BSD-style license that can be found in the LICENSE file.
4+
5+
import 'package:test_reflective_loader/test_reflective_loader.dart';
6+
7+
import '../rule_test_support.dart';
8+
9+
main() {
10+
defineReflectiveSuite(() {
11+
defineReflectiveTests(AlwaysDeclareReturnTypesTest);
12+
});
13+
}
14+
15+
@reflectiveTest
16+
class AlwaysDeclareReturnTypesTest extends LintRuleTest {
17+
@override
18+
String get lintRule => 'always_declare_return_types';
19+
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+
f() { }
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(47, 1),
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+
f() { }
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(27, 1),
61+
]);
62+
}
63+
64+
/// Augmentation target chain variations tested in `augmentedTopLevelFunction{*}`.
65+
test_augmentedMethod() async {
66+
var a = newFile('$testPackageLibPath/a.dart', r'''
67+
import augment 'b.dart';
68+
69+
class A {
70+
f() { }
71+
}
72+
''');
73+
74+
var b = newFile('$testPackageLibPath/b.dart', r'''
75+
augment library 'a.dart';
76+
77+
augment class A {
78+
augment f() { }
79+
}
80+
''');
81+
82+
result = await resolveFile(a.path);
83+
await assertDiagnosticsIn(errors, [
84+
lint(38, 1),
85+
]);
86+
87+
result = await resolveFile(b.path);
88+
await assertNoDiagnosticsIn(errors);
89+
}
90+
91+
test_augmentedTopLevelFunction() async {
92+
var a = newFile('$testPackageLibPath/a.dart', r'''
93+
import augment 'b.dart';
94+
95+
f() { }
96+
''');
97+
98+
var b = newFile('$testPackageLibPath/b.dart', r'''
99+
augment library 'a.dart';
100+
101+
augment f() { }
102+
''');
103+
104+
result = await resolveFile(a.path);
105+
await assertDiagnosticsIn(errors, [
106+
lint(26, 1),
107+
]);
108+
109+
result = await resolveFile(b.path);
110+
await assertNoDiagnosticsIn(errors);
111+
}
112+
113+
test_augmentedTopLevelFunction_chain() async {
114+
var a = newFile('$testPackageLibPath/a.dart', r'''
115+
import augment 'b.dart';
116+
117+
f() { }
118+
''');
119+
120+
var b = newFile('$testPackageLibPath/b.dart', r'''
121+
augment library 'a.dart';
122+
123+
augment dynamic f() { }
124+
augment f() { }
125+
''');
126+
127+
result = await resolveFile(a.path);
128+
await assertDiagnosticsIn(errors, [
129+
lint(26, 1),
130+
]);
131+
132+
result = await resolveFile(b.path);
133+
await assertNoDiagnosticsIn(errors);
134+
}
135+
}

0 commit comments

Comments
 (0)