runWithReporter method

  1. @override
void runWithReporter(
  1. SaropaDiagnosticReporter reporter,
  2. SaropaContext context
)
override

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,
) {
  // Only check test files
  if (!context.filePath.contains('_test.dart')) return;

  context.addMethodInvocation((node) {
    if (node.methodName.name != 'test') return;

    final args = node.argumentList.arguments;
    if (args.length < 2) return;

    final callback = args[1];
    if (callback is! FunctionExpression) return;

    final body = callback.body;
    if (body is! BlockFunctionBody) return;

    // Check if body contains AAA or GWT comments
    final source = context.fileContent;
    final bodySource = source.substring(body.offset, body.end);

    final hasStructure =
        RegExp(r'// Arrange').hasMatch(bodySource) ||
        RegExp(r'// Act').hasMatch(bodySource) ||
        RegExp(r'// Assert').hasMatch(bodySource) ||
        RegExp(r'// Given').hasMatch(bodySource) ||
        RegExp(r'// When').hasMatch(bodySource) ||
        RegExp(r'// Then').hasMatch(bodySource);

    // Only flag if there are multiple statements (complex enough to warrant structure)
    if (!hasStructure && body.block.statements.length >= 3) {
      reporter.atNode(node);
    }
  });
}