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) {
    // Check for GoogleFonts calls
    final String? target = node.target?.toSource();
    if (target != 'GoogleFonts') return;

    // Check for fontFamilyFallback parameter
    final bool hasFallback = node.argumentList.arguments.any((
      Expression arg,
    ) {
      if (arg is NamedExpression) {
        return arg.name.label.name == 'fontFamilyFallback';
      }
      return false;
    });

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