Index: lib/src/rules/always_declare_return_types.dart |
diff --git a/lib/src/rules/always_declare_return_types.dart b/lib/src/rules/always_declare_return_types.dart |
new file mode 100644 |
index 0000000000000000000000000000000000000000..052e7128f5be4ba38b1adf457f769f9fd80181d5 |
--- /dev/null |
+++ b/lib/src/rules/always_declare_return_types.dart |
@@ -0,0 +1,84 @@ |
+// Copyright (c) 2015, the Dart project authors. Please see the AUTHORS file |
+// for details. All rights reserved. Use of this source code is governed by a |
+// BSD-style license that can be found in the LICENSE file. |
+ |
+library linter.src.rules.always_declare_return_types; |
+ |
+import 'package:analyzer/src/generated/ast.dart' |
+ show |
+ AstVisitor, |
+ FunctionDeclaration, |
+ FunctionTypeAlias, |
+ MethodDeclaration, |
+ SimpleAstVisitor; |
+import 'package:linter/src/linter.dart'; |
+ |
+const desc = 'Declare method return types.'; |
+ |
+const details = ''' |
+**DO** declare method return types. |
+ |
+When declaring a method or function *always* specify a return type. |
+ |
+**BAD:** |
+``` |
+main() { } |
+ |
+_bar() => new _Foo(); |
+ |
+class _Foo { |
+ _foo() => 42; |
+} |
+``` |
+ |
+**GOOD:** |
+``` |
+void main() { } |
+ |
+_Foo _bar() => new _Foo(); |
+ |
+class _Foo { |
+ int _foo() => 42; |
+} |
+ |
+typedef bool predicate(Object o); |
+``` |
+'''; |
+ |
+class AlwaysDeclareReturnTypes extends LintRule { |
+ AlwaysDeclareReturnTypes() |
+ : super( |
+ name: 'always_declare_return_types', |
+ description: desc, |
+ details: details, |
+ group: Group.style); |
+ |
+ @override |
+ AstVisitor getVisitor() => new Visitor(this); |
+} |
+ |
+class Visitor extends SimpleAstVisitor { |
+ final LintRule rule; |
+ Visitor(this.rule); |
+ |
+ @override |
+ visitFunctionDeclaration(FunctionDeclaration node) { |
+ if (node.returnType == null) { |
+ rule.reportLint(node.name); |
+ } |
+ } |
+ |
+ @override |
+ visitFunctionTypeAlias(FunctionTypeAlias node) { |
+ if (node.returnType == null) { |
+ rule.reportLint(node.name); |
+ } |
+ } |
+ |
+ @override |
+ visitMethodDeclaration(MethodDeclaration node) { |
+ if (node.returnType == null) { |
+ rule.reportLint(node.name); |
+ } |
+ } |
+} |