runWithReporter method
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.addForStatement((node) {
final forParts = node.forLoopParts;
if (forParts is! ForEachPartsWithDeclaration) {
return;
}
// Check if iterable ends with toList()
final iterable = forParts.iterable;
if (iterable is! MethodInvocation) {
return;
}
if (iterable.methodName.name != 'toList' &&
iterable.methodName.name != 'toSet') {
return;
}
// Check if there's a chain before toList
final target = iterable.target;
if (target is! MethodInvocation) {
return;
}
// Common chained methods that return iterables
final chainMethods = ['map', 'where', 'expand', 'take', 'skip'];
if (chainMethods.contains(target.methodName.name)) {
reporter.atNode(iterable.methodName, code);
}
});
}