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,
) {
  // Skip config files
  final String path = context.filePath;
  if (path.contains('config') || path.contains('constants')) return;

  context.addSimpleStringLiteral((SimpleStringLiteral node) {
    final String value = node.value;

    if (!_apiUrlPattern.hasMatch(value)) return;

    // The rule's own remediation is "extract the URL to a configuration
    // constant", so a literal that is ALREADY the value of a const / static
    // const declaration, a const collection entry, or a const-context enum
    // default has satisfied the rule — flagging it forces ignores on the very
    // files that are the correct home for these endpoints. Only fire on URLs
    // that are not bound to a constant (inline call-site literals, mutable
    // locals). See bugs/avoid_hardcoded_api_urls_false_positive_*.md.
    if (_isAlreadyExtractedToConstant(node)) return;

    reporter.atNode(node);
  });
}