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.addInstanceCreationExpression((InstanceCreationExpression node) {
final typeName = node.constructorName.type.name.lexeme;
if (typeName != 'TextFormField') return;
// Check if the TextFormField has a validator parameter (indicates form usage)
bool hasValidator = false;
for (final arg in node.argumentList.arguments) {
if (arg is NamedExpression && arg.name.label.name == 'validator') {
hasValidator = true;
break;
}
}
if (!hasValidator) return; // No validator = probably OK to not have Form
// Walk up looking for Form constructor
AstNode? current = node.parent;
bool foundForm = false;
int depth = 0;
const maxDepth = 50; // Reasonable widget tree depth
while (current != null && depth < maxDepth) {
if (current is InstanceCreationExpression) {
final parentType = current.constructorName.type.name.lexeme;
if (parentType == 'Form') {
foundForm = true;
break;
}
}
current = current.parent;
depth++;
}
if (!foundForm) {
reporter.atNode(node.constructorName, code);
}
});
}