Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(932)

Unified Diff: lib/src/rules/always_declare_return_types.dart

Issue 1414793007: Lint to check for declared return types (#146). (Closed) Base URL: https://github.com/dart-lang/linter.git@master
Patch Set: Created 5 years, 1 month ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View side-by-side diff with in-line comments
Download patch
« no previous file with comments | « lib/src/rules.dart ('k') | test/_data/p5/p5.dart » ('j') | no next file with comments »
Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
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);
+ }
+ }
+}
« no previous file with comments | « lib/src/rules.dart ('k') | test/_data/p5/p5.dart » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698