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) {
final String methodName = node.name.lexeme;
if (!_lifecycleMethods.contains(methodName)) return;
// Check if in a State class
final ClassDeclaration? parent = node
.thisOrAncestorOfType<ClassDeclaration>();
if (parent == null) return;
final ExtendsClause? extendsClause = parent.extendsClause;
if (extendsClause == null) return;
if (extendsClause.superclass.element?.name != 'State') return;
// Find setState calls in this method
node.body.visitChildren(
_SetStateCallFinder((MethodInvocation setStateCall) {
reporter.atNode(setStateCall);
}),
);
});
}