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.addMethodInvocation((MethodInvocation node) {
final String methodName = node.methodName.name;
// Only check BLE-specific methods
if (methodName != 'connect' &&
methodName != 'discoverServices' &&
methodName != 'startScan') {
return;
}
// Use type resolution to verify this is a BLE type
final Expression? target = node.target;
if (target == null) {
// Static call like FlutterBluePlus.startScan()
final String? targetName = node.realTarget?.toSource();
if (targetName != 'FlutterBluePlus' && targetName != 'FlutterBlue') {
return;
}
} else {
// Instance call - check the static type
final String? typeName = target.staticType?.element?.name;
if (typeName == null || !_bleTypeNames.contains(typeName)) {
return;
}
}
// Check if this is inside a method that checks adapter state
bool hasStateCheck = false;
AstNode? current = node.parent;
BlockFunctionBody? enclosingBody;
while (current != null) {
if (current is BlockFunctionBody) {
enclosingBody = current;
break;
}
current = current.parent;
}
if (enclosingBody != null) {
final String bodySource = enclosingBody.toSource();
if (RegExp(r'\badapterState\b').hasMatch(bodySource) ||
RegExp(r'\bisAvailable\b').hasMatch(bodySource) ||
RegExp(r'\bBluetoothAdapterState\b').hasMatch(bodySource)) {
hasStateCheck = true;
}
}
if (!hasStateCheck) {
reporter.atNode(node.methodName, code);
}
});
}