check method

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

Implementation

@override
void check(DcqRegistry registry) {
  var hasFlutterImport = false;
  registry.addImportDirective((node) {
    final uri = node.uri.stringValue;
    if (uri != null && uri.startsWith('package:flutter/')) {
      hasFlutterImport = true;
    }
  });

  final pendingNodes = <InstanceCreationExpression>[];

  registry.addInstanceCreationExpression((node) {
    final name = node.constructorName.type.name.lexeme;
    if (!_flexTypes.contains(name)) return;

    for (final arg in node.argumentList.arguments) {
      if (arg is! NamedExpression) continue;
      if (arg.name.label.name != 'children') continue;

      final expr = arg.expression;
      if (expr is! ListLiteral) continue;

      // Collect all SizedBox spacers and their size values.
      final spacers = <InstanceCreationExpression>[];
      final sizeValues = <String>{};

      for (final child in expr.elements) {
        if (child is! InstanceCreationExpression) continue;
        final childName = child.constructorName.type.name.lexeme;
        if (childName != 'SizedBox') continue;

        final childArgs = child.argumentList.arguments;
        final hasOnlySize = childArgs.every((a) {
          if (a is! NamedExpression) return false;
          final n = a.name.label.name;
          return n == 'height' || n == 'width' || n == 'key';
        });
        if (!hasOnlySize || childArgs.isEmpty) continue;

        spacers.add(child);

        // Extract the size dimension value for uniformity check.
        for (final a in childArgs) {
          if (a is! NamedExpression) continue;
          final n = a.name.label.name;
          if (n == 'height' || n == 'width') {
            final value = _numericValue(a.expression);
            sizeValues.add('$n:${value ?? a.expression.toSource()}');
          }
        }
      }

      // Only flag if all spacers use the same dimension and value.
      if (spacers.isEmpty || sizeValues.length != 1) continue;

      pendingNodes.addAll(spacers);
    }
  });

  registry.afterLibrary(() {
    if (!hasFlutterImport) return;
    for (final node in pendingNodes) {
      reportAtNode(node);
    }
  });
}