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) {
    if (node.methodName.name != 'customStatement') return;

    final args = node.argumentList.arguments;
    if (args.isEmpty) return;

    final sqlArg = args.first.toSource().toLowerCase();
    if (!RegExp(r'foreign_keys').hasMatch(sqlArg)) return;

    // Check if inside onCreate or onUpgrade
    AstNode? current = node.parent;
    while (current != null) {
      if (current is NamedExpression) {
        final label = current.name.label.name;
        if (_migrationCallbacks.contains(label)) {
          reporter.atNode(node);
          return;
        }
      }
      if (current is ClassDeclaration) return;
      current = current.parent;
    }
  });
}