check method

  1. @override
void check(
  1. DcqRegistry registry
)

Implementation

@override
void check(
  DcqRegistry registry,
) {
  registry.addMethodInvocation((node) {
    if (node.methodName.name != 'whereType') return;

    final typeArgs = node.typeArguments?.arguments;
    if (typeArgs == null || typeArgs.length != 1) return;

    final whereTypeArg = typeArgs.first.type;
    if (whereTypeArg == null) return;

    // whereType<T> argument should be non-nullable.
    if (whereTypeArg.nullabilitySuffix != NullabilitySuffix.none) return;

    final target = node.target;
    if (target == null) return;

    final targetType = target.staticType;
    if (targetType is! InterfaceType) return;

    // Find the Iterable type in the target's type hierarchy.
    final iterableType = _findIterableType(targetType);
    if (iterableType == null) return;
    if (iterableType.typeArguments.length != 1) return;

    final elementType = iterableType.typeArguments.first;

    // The iterable element type must be nullable (T?).
    if (elementType.nullabilitySuffix != NullabilitySuffix.question) return;

    // Check that stripping nullability from the element type gives the
    // same type as the whereType argument.
    if (_isNonNullableOf(elementType, whereTypeArg)) {
      reportAtNode(node);
    }
  });
}