Skip to content

Commit 98ebbcd

Browse files
scheglovCommit Queue
authored andcommitted
Linter. Don't report AlwaysDeclareReturnTypes for 'test_' methods.
These methods are only invoked by the reflective loader, and so the return type does not matter. Change-Id: I28089be206f1059f0786a3e43e009471bbb43b8d Reviewed-on: https://dart-review.googlesource.com/c/sdk/+/374844 Reviewed-by: Phil Quitslund <pquitslund@google.com> Commit-Queue: Konstantin Shcheglov <scheglov@google.com>
1 parent ec4ff9c commit 98ebbcd

File tree

2 files changed

+101
-9
lines changed

2 files changed

+101
-9
lines changed

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

Lines changed: 31 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -69,7 +69,7 @@ class AlwaysDeclareReturnTypes extends LintRule {
6969
@override
7070
void registerNodeProcessors(
7171
NodeLintRegistry registry, LinterContext context) {
72-
var visitor = _Visitor(this);
72+
var visitor = _Visitor(this, context);
7373
registry.addFunctionDeclaration(this, visitor);
7474
registry.addFunctionTypeAlias(this, visitor);
7575
registry.addMethodDeclaration(this, visitor);
@@ -78,8 +78,9 @@ class AlwaysDeclareReturnTypes extends LintRule {
7878

7979
class _Visitor extends SimpleAstVisitor<void> {
8080
final LintRule rule;
81+
final LinterContext context;
8182

82-
_Visitor(this.rule);
83+
_Visitor(this.rule, this.context);
8384

8485
@override
8586
void visitFunctionDeclaration(FunctionDeclaration node) {
@@ -101,13 +102,34 @@ class _Visitor extends SimpleAstVisitor<void> {
101102

102103
@override
103104
void visitMethodDeclaration(MethodDeclaration node) {
104-
if (!node.isSetter &&
105-
node.returnType == null &&
106-
node.name.type != TokenType.INDEX_EQ &&
107-
!node.isAugmentation) {
108-
rule.reportLintForToken(node.name,
109-
arguments: [node.name.lexeme],
110-
errorCode: AlwaysDeclareReturnTypes.methodCode);
105+
if (node.returnType != null) return;
106+
if (node.isAugmentation) return;
107+
if (node.isSetter) return;
108+
if (node.name.type == TokenType.INDEX_EQ) return;
109+
110+
if (_isInTestDirectory()) {
111+
if (node.name.lexeme.startsWith('test_') ||
112+
node.name.lexeme.startsWith('solo_test_')) {
113+
return;
114+
}
115+
}
116+
117+
rule.reportLintForToken(
118+
node.name,
119+
arguments: [node.name.lexeme],
120+
errorCode: AlwaysDeclareReturnTypes.methodCode,
121+
);
122+
}
123+
124+
bool _isInTestDirectory() {
125+
if (context.package case PubPackage pubPackage) {
126+
var packageRoot = pubPackage.pubspecFile.parent;
127+
var filePath = context.libraryElement?.source.fullName;
128+
if (filePath != null) {
129+
var file = packageRoot.provider.getFile(filePath);
130+
return pubPackage.isInTestDirectory(file);
131+
}
111132
}
133+
return false;
112134
}
113135
}

pkg/linter/test/rules/always_declare_return_types_test.dart

Lines changed: 70 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -162,6 +162,24 @@ class C {
162162
]);
163163
}
164164

165+
test_method_testUnderscore_notInPubPackageTest_hasReturnType() async {
166+
await assertNoDiagnostics(r'''
167+
class A {
168+
void test_foo() {}
169+
}
170+
''');
171+
}
172+
173+
test_method_testUnderscore_notInPubPackageTest_noReturnType() async {
174+
await assertDiagnostics(r'''
175+
class A {
176+
test_foo() {}
177+
}
178+
''', [
179+
lint(12, 8),
180+
]);
181+
}
182+
165183
test_method_withReturnType() async {
166184
await assertNoDiagnostics(r'''
167185
class C {
@@ -179,6 +197,58 @@ class C {
179197
''');
180198
}
181199

200+
test_pubPackageTest_method_notTest_hasReturnType() async {
201+
var file = newFile('$testPackageRootPath/test/test.dart', r'''
202+
class MyTest {
203+
void foo() {}
204+
}
205+
''');
206+
207+
await assertNoDiagnosticsInFile(file.path);
208+
}
209+
210+
test_pubPackageTest_method_notTest_noReturnType() async {
211+
var file = newFile('$testPackageRootPath/test/test.dart', r'''
212+
class MyTest {
213+
foo() {}
214+
}
215+
''');
216+
217+
await assertDiagnosticsInFile(file.path, [
218+
lint(17, 3),
219+
]);
220+
}
221+
222+
test_pubPackageTest_method_soloTest_noReturnType() async {
223+
var file = newFile('$testPackageRootPath/test/test.dart', r'''
224+
class MyTest {
225+
solo_test_foo() {}
226+
}
227+
''');
228+
229+
await assertNoDiagnosticsInFile(file.path);
230+
}
231+
232+
test_pubPackageTest_method_test_hasReturnType() async {
233+
var file = newFile('$testPackageRootPath/test/test.dart', r'''
234+
class MyTest {
235+
void test_foo() {}
236+
}
237+
''');
238+
239+
await assertNoDiagnosticsInFile(file.path);
240+
}
241+
242+
test_pubPackageTest_method_test_noReturnType() async {
243+
var file = newFile('$testPackageRootPath/test/test.dart', r'''
244+
class MyTest {
245+
test_foo() {}
246+
}
247+
''');
248+
249+
await assertNoDiagnosticsInFile(file.path);
250+
}
251+
182252
test_staticSetter() async {
183253
await assertNoDiagnostics(r'''
184254
class C {

0 commit comments

Comments
 (0)