run method

  1. @override
void run(
  1. CustomLintResolver resolver,
  2. ErrorReporter reporter,
  3. CustomLintContext context
)

Emits lints for a given file.

run will only be invoked with files respecting filesToAnalyze

Implementation

@override
void run(
  CustomLintResolver resolver,
  ErrorReporter reporter,
  CustomLintContext context,
) {
  context.registry.addHookWidgetBody((node, diagnosticNode) {
    // get all hook expressions from build method
    final hookExpressions = switch (node) {
      ExpressionFunctionBody(expression: final AstNode node) ||
      BlockFunctionBody(block: final AstNode node) =>
        getAllInnerHookExpressions(node),
      _ => <InvocationExpression>[],
    };

    final returnExpressions = getAllReturnExpressions(node).nonNulls;
    // everything past that return is considered conditional
    final firstReturn = returnExpressions.isEmpty
        ? null
        : returnExpressions
            .reduce((acc, curr) => acc.offset < curr.offset ? acc : curr);

    final hooksCalledConditionally =
        hookExpressions.where((e) => _isConditional(firstReturn, e, node));

    for (final hookExpression in hooksCalledConditionally) {
      reporter.reportErrorForNode(_getLintCode(), hookExpression);
    }
  });
}