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 node) {
    final String className = node.nameToken.lexeme;

    // Check constructor parameters for same-package service dependencies
    for (final ClassMember member in node.bodyMembers) {
      if (member is ConstructorDeclaration) {
        for (final FormalParameter param in member.parameters.parameters) {
          String? paramType;
          if (param is SimpleFormalParameter) {
            paramType = param.type?.toSource();
          } else if (param is DefaultFormalParameter) {
            final NormalFormalParameter normalParam = param.parameter;
            if (normalParam is SimpleFormalParameter) {
              paramType = normalParam.type?.toSource();
            }
          }

          if (paramType != null &&
              _isSameLayerDependency(className, paramType)) {
            reporter.atNode(param);
          }
        }
      }
    }
  });
}