check method

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

Implementation

@override
void check(
  DcqRegistry registry,
) {
  registry.addClassDeclaration((node) {
    final superclassNode = node.extendsClause?.superclass;
    if (superclassNode == null) return;
    final superclass = superclassNode.name.lexeme;
    if (superclass != 'ConsumerWidget' &&
        superclass != 'ConsumerStatefulWidget') {
      return;
    }
    if (!isFromExpectedPackage(superclassNode, 'flutter_riverpod')) return;

    final finder = _RefUsageFinder();
    node.accept(finder);

    if (finder.hasRefUsage) return;

    // For ConsumerStatefulWidget, also check the associated state class.
    if (superclass == 'ConsumerStatefulWidget') {
      final widgetName = node.namePart.typeName.lexeme;
      final unit = node.parent;
      if (unit is CompilationUnit) {
        for (final decl in unit.declarations) {
          if (decl is! ClassDeclaration) continue;
          final stateSuper = decl.extendsClause?.superclass;
          if (stateSuper == null) continue;
          if (stateSuper.name.lexeme != 'ConsumerState') continue;
          if (!isFromExpectedPackage(stateSuper, 'flutter_riverpod')) {
            continue;
          }
          final typeArgs = stateSuper.typeArguments?.arguments;
          if (typeArgs == null || typeArgs.isEmpty) continue;
          final arg = typeArgs.first;
          if (arg is NamedType && arg.name.lexeme == widgetName) {
            final stateFinder = _RefUsageFinder();
            decl.accept(stateFinder);
            if (stateFinder.hasRefUsage) return;
          }
        }
      }
    }

    reportAtToken(node.namePart.typeName);
  });
}