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,
) {
  bool isBoolParam(FormalParameter p) {
    final TypeAnnotation? t = _getTypeAnnotation(p);
    if (t == null || t is! NamedType) return false;
    final String name = t.name.lexeme;
    return name == 'bool' || name == 'bool?';
  }

  void check(FormalParameterList? parameters, AstNode reportNode) {
    if (parameters == null) return;
    for (final FormalParameter p in parameters.parameters) {
      if (p.isNamed && isBoolParam(p)) {
        reporter.atNode(reportNode);
        return;
      }
    }
  }

  context.addMethodDeclaration((MethodDeclaration node) {
    check(node.parameters, node);
  });
  context.addFunctionDeclaration((FunctionDeclaration node) {
    check(node.functionExpression.parameters, node);
  });
  context.addConstructorDeclaration((ConstructorDeclaration node) {
    check(node.parameters, node);
  });
}