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,
) {
  context.addInstanceCreationExpression((InstanceCreationExpression node) {
    final typeName = node.constructorName.type.name.lexeme;
    final constructorName = node.constructorName.name?.name;
    if (typeName != 'NativeDatabase' || constructorName != 'memory') return;

    // Only relevant in test files
    if (!_isInTestFile(node)) return;

    // Check if wrapped in DatabaseConnection with the parameter
    final parent = node.parent;
    if (parent is ArgumentList) {
      final grandparent = parent.parent;
      if (grandparent is InstanceCreationExpression) {
        final wrapperType = grandparent.constructorName.type.name.lexeme;
        if (wrapperType == 'DatabaseConnection') {
          // Check for closeStreamsSynchronously parameter
          for (final arg in grandparent.argumentList.arguments) {
            if (arg is NamedExpression &&
                arg.name.label.name == 'closeStreamsSynchronously') {
              return; // Already wrapped correctly
            }
          }
        }
      }
    }

    reporter.atNode(node);
  });
}