OLD | NEW |
1 // Copyright (c) 2015, the Dart project authors. Please see the AUTHORS file | 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 | 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. | 3 // BSD-style license that can be found in the LICENSE file. |
4 | 4 |
5 library linter.src.rules.type_annotate_public_apis; | 5 library linter.src.rules.type_annotate_public_apis; |
6 | 6 |
7 import 'package:analyzer/src/generated/ast.dart'; | 7 import 'package:analyzer/src/generated/ast.dart'; |
| 8 import 'package:linter/src/ast.dart'; |
8 import 'package:linter/src/linter.dart'; | 9 import 'package:linter/src/linter.dart'; |
9 import 'package:linter/src/ast.dart'; | |
10 | 10 |
11 const desc = r'Type annotate public APIs.'; | 11 const desc = r'Type annotate public APIs.'; |
12 | 12 |
13 const details = r''' | 13 const details = r''' |
14 From [effective dart] (https://www.dartlang.org/effective-dart/design/#do-type-a
nnotate-public-apis): | 14 From [effective dart] (https://www.dartlang.org/effective-dart/design/#do-type-a
nnotate-public-apis): |
15 | 15 |
16 **DO** type annotate public APIs. | 16 **DO** type annotate public APIs. |
17 | 17 |
18 Type annotations are important documentation for how a library should be used. | 18 Type annotations are important documentation for how a library should be used. |
19 Annotating the parameter and return types of public methods and functions helps | 19 Annotating the parameter and return types of public methods and functions helps |
(...skipping 34 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
54 name: 'type_annotate_public_apis', | 54 name: 'type_annotate_public_apis', |
55 description: desc, | 55 description: desc, |
56 details: details, | 56 details: details, |
57 group: Group.style); | 57 group: Group.style); |
58 | 58 |
59 @override | 59 @override |
60 AstVisitor getVisitor() => new Visitor(this); | 60 AstVisitor getVisitor() => new Visitor(this); |
61 } | 61 } |
62 | 62 |
63 class Visitor extends SimpleAstVisitor { | 63 class Visitor extends SimpleAstVisitor { |
| 64 _VisitorHelper v; |
64 final LintRule rule; | 65 final LintRule rule; |
65 _VisitoHelper v; | |
66 Visitor(this.rule) { | 66 Visitor(this.rule) { |
67 v = new _VisitoHelper(rule); | 67 v = new _VisitorHelper(rule); |
68 } | 68 } |
69 | 69 |
70 @override | 70 @override |
71 visitFieldDeclaration(FieldDeclaration node) { | 71 visitFieldDeclaration(FieldDeclaration node) { |
72 if (node.fields.type == null) { | 72 if (node.fields.type == null) { |
73 node.fields.accept(v); | 73 node.fields.accept(v); |
74 } | 74 } |
75 } | 75 } |
76 | 76 |
77 @override | 77 @override |
78 visitTopLevelVariableDeclaration(TopLevelVariableDeclaration node) { | |
79 if (node.variables.type == null) { | |
80 node.variables.accept(v); | |
81 } | |
82 } | |
83 | |
84 @override | |
85 visitFunctionDeclaration(FunctionDeclaration node) { | 78 visitFunctionDeclaration(FunctionDeclaration node) { |
86 if (!isPrivate(node.name)) { | 79 if (!isPrivate(node.name)) { |
87 if (node.returnType == null) { | 80 if (node.returnType == null && !node.isSetter) { |
88 rule.reportLint(node.name); | 81 rule.reportLint(node.name); |
89 } else { | 82 } else { |
90 node.functionExpression.parameters.accept(v); | 83 node.functionExpression.parameters?.accept(v); |
91 } | |
92 } | |
93 } | |
94 | |
95 @override | |
96 visitMethodDeclaration(MethodDeclaration node) { | |
97 if (!isPrivate(node.name)) { | |
98 if (node.returnType == null) { | |
99 rule.reportLint(node.name); | |
100 } else { | |
101 node.parameters.accept(v); | |
102 } | 84 } |
103 } | 85 } |
104 } | 86 } |
105 | 87 |
106 @override | 88 @override |
107 visitFunctionTypeAlias(FunctionTypeAlias node) { | 89 visitFunctionTypeAlias(FunctionTypeAlias node) { |
108 if (node.returnType == null) { | 90 if (node.returnType == null) { |
109 rule.reportLint(node.name); | 91 rule.reportLint(node.name); |
110 } else { | 92 } else { |
111 node.parameters.accept(v); | 93 node.parameters.accept(v); |
112 } | 94 } |
113 } | 95 } |
| 96 |
| 97 @override |
| 98 visitMethodDeclaration(MethodDeclaration node) { |
| 99 if (!isPrivate(node.name)) { |
| 100 if (node.returnType == null && !node.isSetter) { |
| 101 rule.reportLint(node.name); |
| 102 } else { |
| 103 node.parameters?.accept(v); |
| 104 } |
| 105 } |
| 106 } |
| 107 |
| 108 @override |
| 109 visitTopLevelVariableDeclaration(TopLevelVariableDeclaration node) { |
| 110 if (node.variables.type == null) { |
| 111 node.variables.accept(v); |
| 112 } |
| 113 } |
114 } | 114 } |
115 | 115 |
116 class _VisitoHelper extends RecursiveAstVisitor { | 116 class _VisitorHelper extends RecursiveAstVisitor { |
117 final LintRule rule; | 117 final LintRule rule; |
118 _VisitoHelper(this.rule); | 118 _VisitorHelper(this.rule); |
| 119 |
| 120 @override |
| 121 visitSimpleFormalParameter(SimpleFormalParameter param) { |
| 122 if (param.type == null) { |
| 123 rule.reportLint(param); |
| 124 } |
| 125 } |
119 | 126 |
120 @override | 127 @override |
121 visitVariableDeclaration(VariableDeclaration node) { | 128 visitVariableDeclaration(VariableDeclaration node) { |
122 if (!isPrivate(node.name)) { | 129 if (!isPrivate(node.name)) { |
123 rule.reportLint(node.name); | 130 rule.reportLint(node.name); |
124 } | 131 } |
125 } | 132 } |
126 | |
127 @override | |
128 visitSimpleFormalParameter(SimpleFormalParameter param) { | |
129 if (param.type == null) { | |
130 rule.reportLint(param); | |
131 } | |
132 } | |
133 } | 133 } |
OLD | NEW |