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.addClassDeclaration((ClassDeclaration node) {
// Check if class name ends with State
final String className = node.nameToken.lexeme;
if (!className.endsWith('State')) return;
// Exclude Flutter's State class by checking for extends StatefulWidget
final ExtendsClause? extendsClause = node.extendsClause;
if (extendsClause != null) {
final String superName = extendsClause.superclass.name.lexeme;
if (superName == 'State') return; // Flutter State, not Bloc state
}
// Check for mutable fields
for (final ClassMember member in node.bodyMembers) {
if (member is FieldDeclaration && !member.isStatic) {
final VariableDeclarationList fields = member.fields;
if (!fields.isFinal && !fields.isConst) {
reporter.atNode(member);
}
}
}
});
}