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 DateTime.parse()
    final Expression? target = node.target;
    if (target is! SimpleIdentifier) return;
    if (target.name != 'DateTime') return;
    if (node.methodName.name != 'parse') return;

    // Check if the argument is from JSON or a variable (not a literal)
    final NodeList<Expression> args = node.argumentList.arguments;
    if (args.isEmpty) return;

    final Expression firstArg = args.first;
    // If it's a string literal, it's probably fine (developer knows the format)
    if (firstArg is StringLiteral) return;

    reporter.atNode(node);
  });
}