Skip to content
This repository was archived by the owner on Nov 20, 2024. It is now read-only.

Commit 2689ad3

Browse files
committed
Lint to check for declared return types (#146).
BUG=146 [email protected] Review URL: https://codereview.chromium.org//1414793007 .
1 parent c5be499 commit 2689ad3

File tree

4 files changed

+110
-1
lines changed

4 files changed

+110
-1
lines changed

lib/src/rules.dart

+2
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ import 'dart:collection';
88

99
import 'package:linter/src/config.dart';
1010
import 'package:linter/src/linter.dart';
11+
import 'package:linter/src/rules/always_declare_return_types.dart';
1112
import 'package:linter/src/rules/always_specify_types.dart';
1213
import 'package:linter/src/rules/camel_case_types.dart';
1314
import 'package:linter/src/rules/constant_identifier_names.dart';
@@ -27,6 +28,7 @@ import 'package:linter/src/rules/unnecessary_brace_in_string_interp.dart';
2728
import 'package:linter/src/rules/unnecessary_getters_setters.dart';
2829

2930
final Registry ruleRegistry = new Registry()
31+
..register(new AlwaysDeclareReturnTypes())
3032
..register(new AlwaysSpecifyTypes())
3133
..register(new CamelCaseTypes())
3234
..register(new ConstantIdentifierNames())
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,84 @@
1+
// Copyright (c) 2015, 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+
library linter.src.rules.always_declare_return_types;
6+
7+
import 'package:analyzer/src/generated/ast.dart'
8+
show
9+
AstVisitor,
10+
FunctionDeclaration,
11+
FunctionTypeAlias,
12+
MethodDeclaration,
13+
SimpleAstVisitor;
14+
import 'package:linter/src/linter.dart';
15+
16+
const desc = 'Declare method return types.';
17+
18+
const details = '''
19+
**DO** declare method return types.
20+
21+
When declaring a method or function *always* specify a return type.
22+
23+
**BAD:**
24+
```
25+
main() { }
26+
27+
_bar() => new _Foo();
28+
29+
class _Foo {
30+
_foo() => 42;
31+
}
32+
```
33+
34+
**GOOD:**
35+
```
36+
void main() { }
37+
38+
_Foo _bar() => new _Foo();
39+
40+
class _Foo {
41+
int _foo() => 42;
42+
}
43+
44+
typedef bool predicate(Object o);
45+
```
46+
''';
47+
48+
class AlwaysDeclareReturnTypes extends LintRule {
49+
AlwaysDeclareReturnTypes()
50+
: super(
51+
name: 'always_declare_return_types',
52+
description: desc,
53+
details: details,
54+
group: Group.style);
55+
56+
@override
57+
AstVisitor getVisitor() => new Visitor(this);
58+
}
59+
60+
class Visitor extends SimpleAstVisitor {
61+
final LintRule rule;
62+
Visitor(this.rule);
63+
64+
@override
65+
visitFunctionDeclaration(FunctionDeclaration node) {
66+
if (node.returnType == null) {
67+
rule.reportLint(node.name);
68+
}
69+
}
70+
71+
@override
72+
visitFunctionTypeAlias(FunctionTypeAlias node) {
73+
if (node.returnType == null) {
74+
rule.reportLint(node.name);
75+
}
76+
}
77+
78+
@override
79+
visitMethodDeclaration(MethodDeclaration node) {
80+
if (node.returnType == null) {
81+
rule.reportLint(node.name);
82+
}
83+
}
84+
}

test/_data/p5/p5.dart

+1-1
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,6 @@
44

55
import 'package:p6/p6_lib.dart';
66

7-
main() {
7+
void main() {
88
p6_lib();
99
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
// Copyright (c) 2015, 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+
main() { } //LINT
6+
7+
bar() => new _Foo(); //LINT
8+
9+
class _Foo {
10+
_foo() => 42; //LINT
11+
}
12+
13+
typedef bad(int x); //LINT
14+
15+
typedef bool predicate(Object o);
16+
17+
void main2() { }
18+
19+
_Foo bar2() => new _Foo();
20+
21+
class _Foo2 {
22+
int _foo() => 42;
23+
}

0 commit comments

Comments
 (0)