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,
) {
final String path = context.filePath;
if (!path.endsWith('_test.dart') &&
!path.contains('/test/') &&
!path.contains(r'\test\')) {
return;
}
final List<MethodInvocation> sizeTests = <MethodInvocation>[];
context.addMethodInvocation((MethodInvocation node) {
if (node.methodName.name != 'testWidgets') return;
final ArgumentList args = node.argumentList;
if (args.arguments.length >= 2) {
final Expression callback = args.arguments[1];
if (callback is FunctionExpression) {
final String bodySource = callback.body.toSource();
// Check for screen size configuration (word-boundary to avoid FP)
if (RegExp(r'\bphysicalSizeTestValue\b').hasMatch(bodySource) ||
RegExp(r'\bdevicePixelRatio\b').hasMatch(bodySource) ||
RegExp(r'\btextScaleFactor\b').hasMatch(bodySource)) {
sizeTests.add(node);
}
}
}
});
context.addPostRunCallback(() {
// If there are multiple size-related tests, suggest variant
if (sizeTests.length >= 2) {
for (final MethodInvocation test in sizeTests) {
reporter.atNode(test.methodName, code);
}
}
});
}