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.addMethodDeclaration((MethodDeclaration node) {
// Extensions and extension types cannot have instance fields —
// caching is impossible.
if (node.thisOrAncestorOfType<ExtensionDeclaration>() != null ||
node.thisOrAncestorOfType<ExtensionTypeDeclaration>() != null) {
return;
}
// Static methods cannot cache to instance fields either.
if (node.isStatic) return;
// Analyze the method body for repeated getter calls
final Map<String, List<AstNode>> getterCalls = <String, List<AstNode>>{};
node.body.visitChildren(_GetterCallCollector(getterCalls));
for (final MapEntry<String, List<AstNode>> entry in getterCalls.entries) {
if (entry.value.length > 1) {
// Heuristic: skip reporting if getter is an Isar/single-subscription stream
final String getterSource = entry.key;
if (_isSingleSubscriptionStream(getterSource)) {
continue;
}
reporter.atNode(entry.value[1], code);
}
}
});
}