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.addMethodInvocation((MethodInvocation node) {
    final String methodName = node.methodName.name;
    if (!_configMethods.contains(methodName)) return;

    // Check if target looks like config/env
    final Expression? target = node.target;
    if (target == null) return;

    final String targetSource = target.toSource().toLowerCase();
    if (!RegExp(r'\bdotenv\b').hasMatch(targetSource) &&
        !RegExp(r'\bconfig\b').hasMatch(targetSource) &&
        !RegExp(r'\benv\b').hasMatch(targetSource) &&
        !RegExp(r'\bprefs\b').hasMatch(targetSource)) {
      return;
    }

    // Check if a default/fallback is provided
    final ArgumentList args = node.argumentList;
    final bool hasDefault = args.arguments.any((Expression arg) {
      if (arg is NamedExpression) {
        final String name = arg.name.label.name;
        return name == 'fallback' ||
            name == 'defaultValue' ||
            name == 'orElse';
      }
      return false;
    });

    if (!hasDefault) {
      reporter.atNode(node);
    }
  });
}