-
Notifications
You must be signed in to change notification settings - Fork 172
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Lint to check for declared return types (#146).
BUG=146 R=brianwilkerson@google.com Review URL: https://codereview.chromium.org//1414793007 .
- Loading branch information
Showing
4 changed files
with
110 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -4,6 +4,6 @@ | |
|
||
import 'package:p6/p6_lib.dart'; | ||
|
||
main() { | ||
void main() { | ||
p6_lib(); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
} |