Skip to content
This repository has been archived by the owner on Nov 20, 2024. It is now read-only.

Commit

Permalink
special case
Browse files Browse the repository at this point in the history
  • Loading branch information
a14n committed Sep 21, 2018
1 parent f835e0c commit 2eba7b7
Show file tree
Hide file tree
Showing 2 changed files with 39 additions and 3 deletions.
20 changes: 19 additions & 1 deletion lib/src/rules/always_specify_type_arguments_for_methods.dart
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
import 'package:analyzer/dart/ast/ast.dart';
import 'package:analyzer/dart/ast/visitor.dart';
import 'package:analyzer/dart/element/element.dart';
import 'package:analyzer/dart/element/type.dart';
import 'package:linter/src/analyzer.dart';

const _desc = r'Specify type arguments for methods.';
Expand Down Expand Up @@ -80,12 +81,29 @@ class _Visitor extends SimpleAstVisitor<void> {
@override
void visitMethodInvocation(MethodInvocation node) {
if (node.typeArguments == null) {
final element = node.methodName.bestElement;
final element = node.methodName.staticElement;
if (element is FunctionTypedElement &&
element.typeParameters.isNotEmpty &&
!_isAllowed(element) &&
!element.metadata.any((a) => _isOptionalTypeArgs(a.element))) {
rule.reportLint(node.methodName);
}
}
}

bool _isAllowed(FunctionTypedElement element) {
final returnType = element.returnType;

// Special case for methods with return type and parameter types identical
// and equal to the type parameter on function
// eg. T m<T>(T p1, T p2);
if (returnType is TypeParameterType) {
if (returnType.element.enclosingElement == element &&
element.parameters.every((p) => returnType == p.type)) {
return true;
}
}

return false;
}
}
22 changes: 20 additions & 2 deletions test/rules/always_specify_type_arguments_for_methods.dart
Original file line number Diff line number Diff line change
Expand Up @@ -25,16 +25,34 @@ void test() {
g(); //OK
}

class A {
class A<R> {
m1<T>() {}
@optionalTypeArgs
m2<T>() {}
// no lint for method with return type and parameters all the same (m3 and m6)
T m3<T>() => null;
R m4<T>() => null;
T m5<T>(int i) => null;
T m6<T>(T i) => null;

@optionalTypeArgs
static A<T> f<T extends Object>() => null;

m() {
A a;
A<int> a;
a.m1(); // LINT
a.m1<int>(); // OK
a.m2(); // OK
a.m2<int>(); // OK
a.m3(); // OK
a.m3<int>(); // OK
a.m4(); // LINT
a.m4<int>(); // OK
a.m5(null); // LINT
a.m5<int>(null); // OK
a.m6(null); // OK
a.m6<int>(null); // OK
A.f(); // OK
A.f<int>(); // OK
}
}

0 comments on commit 2eba7b7

Please sign in to comment.