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.addInstanceCreationExpression((node) {
    final constructorName = node.constructorName.toString();

    if (constructorName.contains('StatefulSheetDelegate')) {
      // Walk up to see if we're inside a build() method
      AstNode? parent = node;
      while (parent != null && parent is! MethodDeclaration) {
        parent = parent.parent;
      }

      if (parent is MethodDeclaration && parent.name.lexeme == 'build') {
        reporter.atEntity(node, code);
      }
    }
  });
  context.registry.addMethodInvocation((node) {
    final methodName = node.methodName.name;

    // Check if the static method name is `func`
    if (methodName == 'func') {
      final target = node.target;

      // Check if it's called on `StatefulSheetDelegate`
      final isDelegateCall = target != null && target is Identifier && target.name == 'StatefulSheetDelegate';

      if (isDelegateCall) {
        // Check if inside a build() method
        AstNode? parent = node;
        while (parent != null && parent is! MethodDeclaration) {
          parent = parent.parent;
        }

        if (parent is MethodDeclaration && parent.name.lexeme == 'build') {
          reporter.atEntity(node, code);
        }
      }
    }
  });
}