check method

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

Implementation

@override
void check(DcqRegistry registry) {
  var hasHooksImport = false;

  registry.addImportDirective((node) {
    final uri = node.uri.stringValue;
    if (uri != null &&
        (uri.contains('flutter_hooks') || uri.contains('hooks_riverpod'))) {
      hasHooksImport = true;
    }
  });

  final pendingNodes = <AstNode>[];

  registry.addFunctionDeclaration((node) {
    final name = node.name.lexeme;
    if (name == 'main') return;
    if (_hasHookPrefix(name)) return;

    final visitor = _HookCallFinder();
    node.functionExpression.body.accept(visitor);

    if (visitor.found) {
      pendingNodes.add(node);
    }
  });

  registry.addMethodDeclaration((node) {
    final name = node.name.lexeme;
    if (_hasHookPrefix(name)) return;

    // Only flag methods in HookWidget classes.
    final classDecl = node.parent is ClassDeclaration
        ? node.parent! as ClassDeclaration
        : node.parent?.parent is ClassDeclaration
        ? node.parent!.parent! as ClassDeclaration
        : null;
    if (classDecl == null || !isHookWidgetClass(classDecl)) return;

    // Skip build — it's the entry point, not a custom hook.
    if (name == 'build') return;

    final visitor = _HookCallFinder();
    node.body.accept(visitor);

    if (visitor.found) {
      pendingNodes.add(node);
    }
  });

  registry.afterLibrary(() {
    if (!hasHooksImport) return;
    for (final node in pendingNodes) {
      if (node is FunctionDeclaration) {
        reportAtToken(node.name);
      } else if (node is MethodDeclaration) {
        reportAtToken(node.name);
      }
    }
  });
}