check method

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

Implementation

@override
void check(DcqRegistry registry) {
  registry.addMethodInvocation((node) {
    if (node.methodName.name != 'read') return;
    final target = node.target;
    if (target == null) return;

    final type = target.staticType;
    if (type is! InterfaceType || type.element.name != 'BuildContext') return;

    // Walk up to check if we're inside a build method.
    var current = node.parent;
    while (current != null) {
      // If we cross into a nested function/closure before reaching build,
      // the read() call is inside a callback (e.g. onPressed) which is fine.
      if (current is FunctionExpression) return;

      if (current is MethodDeclaration && current.name.lexeme == 'build') {
        reportAtNode(node);
        return;
      }
      current = current.parent;
    }
  });
}