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.addListLiteral((ListLiteral node) {
    final Map<String, List<AstNode>> keyValues = <String, List<AstNode>>{};

    for (final CollectionElement element in node.elements) {
      if (element is InstanceCreationExpression) {
        // Find key argument
        for (final Expression arg in element.argumentList.arguments) {
          if (arg is NamedExpression && arg.name.label.name == 'key') {
            final String? keyString = _extractKeyString(arg.expression);
            if (keyString != null) {
              keyValues.putIfAbsent(keyString, () => <AstNode>[]).add(arg);
            }
          }
        }
      }
    }

    // Report duplicates
    for (final List<AstNode> nodes in keyValues.values) {
      if (nodes.length > 1) {
        for (final AstNode keyNode in nodes) {
          reporter.atNode(keyNode);
        }
      }
    }
  });
}