check method

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

Implementation

@override
void check(DcqRegistry registry) {
  final suffix = effectiveString('suffix', 'State');
  final stateBaseNames = <String>{};
  final candidates = <(ClassDeclaration, String)>[];

  registry.addClassDeclaration((node) {
    final superclass = node.extendsClause?.superclass.type;

    // Collect state base class names from Bloc type parameters.
    if (superclass is InterfaceType && isBloc(superclass)) {
      final typeArgs =
          node.extendsClause?.superclass.typeArguments?.arguments;
      if (typeArgs != null && typeArgs.length >= 2) {
        final stateType = typeArgs[1];
        if (stateType is NamedType) {
          stateBaseNames.add(stateType.name.lexeme);
        }
      }
    }

    final className = node.namePart.typeName.lexeme;
    if (className.endsWith(suffix)) return;

    if (superclass is! InterfaceType) return;
    final superName = superclass.element.name ?? '';
    if (!superName.endsWith(suffix)) return;

    candidates.add((node, superName));
  });

  registry.afterLibrary(() {
    if (stateBaseNames.isEmpty) return;
    for (final (classNode, superName) in candidates) {
      if (stateBaseNames.contains(superName)) {
        reportAtToken(classNode.namePart.typeName);
      }
    }
  });
}