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.addForStatement((ForStatement node) {
    final parts = node.forLoopParts;
    if (parts is! ForPartsWithDeclarations) return;
    final NodeList<VariableDeclaration> vars = parts.variables.variables;
    if (vars.length != 1) return;
    final indexVar = vars.first;
    final indexName = indexVar.name.lexeme;
    final condition = parts.condition;
    if (condition is! BinaryExpression) return;
    if (condition.operator.type != TokenType.LT) return;
    final rightOperand = condition.rightOperand;
    if (rightOperand is! PropertyAccess) return;
    if (rightOperand.propertyName.name != 'length') return;
    if (parts.updaters.length != 1) return;
    if (!_bodyUsesIndex(node.body, indexName)) return;
    reporter.atNode(node);
  });
}