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.addClassDeclaration((ClassDeclaration classNode) {
    // Collect static field names
    final Set<String> staticFields = <String>{};
    for (final ClassMember member in classNode.bodyMembers) {
      if (member is FieldDeclaration && member.isStatic) {
        for (final VariableDeclaration field in member.fields.variables) {
          staticFields.add(field.name.lexeme);
        }
      }
    }

    if (staticFields.isEmpty) return;

    // Check instance methods
    for (final ClassMember member in classNode.bodyMembers) {
      if (member is MethodDeclaration && !member.isStatic) {
        _checkMethodBody(member.body, staticFields, reporter);
      }
    }
  });
}