runWithReporter method

  1. @override
void runWithReporter(
  1. SaropaDiagnosticReporter reporter,
  2. SaropaContext context
)
override

Override this method to implement your lint rule.

Use context to register callbacks for AST node types:

context.addMethodInvocation((node) {
  if (condition) {
    reporter.atNode(node);
  }
});

Implementation

@override
void runWithReporter(
  SaropaDiagnosticReporter reporter,
  SaropaContext context,
) {
  context.addInstanceCreationExpression((InstanceCreationExpression node) {
    final String typeName = node.constructorName.type.name.lexeme;
    final String? constructorName = node.constructorName.name?.name;

    if (typeName != 'ListView') return;
    if (constructorName != 'builder' && constructorName != 'separated') {
      return;
    }

    // Check for itemExtent, prototypeItem, or itemExtentBuilder
    bool hasItemExtent = false;
    for (final Argument arg in node.argumentList.arguments) {
      if (arg is NamedArgument) {
        final String paramName = arg.name.lexeme;
        if (paramName == 'itemExtent' ||
            paramName == 'prototypeItem' ||
            paramName == 'itemExtentBuilder') {
          hasItemExtent = true;
          break;
        }
      }
    }

    if (!hasItemExtent) {
      reporter.atNode(node.constructorName, code);
    }
  });
}