Skip to content

Commit

Permalink
Lint to check for declared return types (#146).
Browse files Browse the repository at this point in the history
BUG=146
R=brianwilkerson@google.com

Review URL: https://codereview.chromium.org//1414793007 .
  • Loading branch information
pq committed Nov 2, 2015
1 parent c5be499 commit 2689ad3
Show file tree
Hide file tree
Showing 4 changed files with 110 additions and 1 deletion.
2 changes: 2 additions & 0 deletions lib/src/rules.dart
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import 'dart:collection';

import 'package:linter/src/config.dart';
import 'package:linter/src/linter.dart';
import 'package:linter/src/rules/always_declare_return_types.dart';
import 'package:linter/src/rules/always_specify_types.dart';
import 'package:linter/src/rules/camel_case_types.dart';
import 'package:linter/src/rules/constant_identifier_names.dart';
Expand All @@ -27,6 +28,7 @@ import 'package:linter/src/rules/unnecessary_brace_in_string_interp.dart';
import 'package:linter/src/rules/unnecessary_getters_setters.dart';

final Registry ruleRegistry = new Registry()
..register(new AlwaysDeclareReturnTypes())
..register(new AlwaysSpecifyTypes())
..register(new CamelCaseTypes())
..register(new ConstantIdentifierNames())
Expand Down
84 changes: 84 additions & 0 deletions lib/src/rules/always_declare_return_types.dart
Original file line number Diff line number Diff line change
@@ -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);
}
}
}
2 changes: 1 addition & 1 deletion test/_data/p5/p5.dart
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,6 @@

import 'package:p6/p6_lib.dart';

main() {
void main() {
p6_lib();
}
23 changes: 23 additions & 0 deletions test/rules/always_declare_return_types.dart
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
// 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.

main() { } //LINT

bar() => new _Foo(); //LINT

class _Foo {
_foo() => 42; //LINT
}

typedef bad(int x); //LINT

typedef bool predicate(Object o);

void main2() { }

_Foo bar2() => new _Foo();

class _Foo2 {
int _foo() => 42;
}

0 comments on commit 2689ad3

Please sign in to comment.