run method

  1. @override
void run(
  1. CustomLintResolver resolver,
  2. DiagnosticReporter 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,
  DiagnosticReporter reporter,
  CustomLintContext context,
) {
  context.registry.addInstanceCreationExpression((node) {
    final type = node.staticType;
    if (type == null) {
      return;
    }

    final typeName = type.element?.name;

    // Only check CustomPaint widgets
    if (typeName != 'CustomPaint') {
      return;
    }

    bool hasSemantics = false;
    bool isExcludedFromSemantics = false;

    // Check if it has semanticsLabel parameter (CustomPaint has this built-in)
    for (final arg in node.argumentList.arguments) {
      if (arg is NamedExpression) {
        final paramName = arg.name.label.name;

        if (paramName == 'semanticsLabel') {
          hasSemantics = hasNonEmptyStringValue(arg.expression);
        }

        if (paramName == 'excludeFromSemantics') {
          final expr = arg.expression;
          if (expr is BooleanLiteral && expr.value) {
            isExcludedFromSemantics = true;
          }
        }
      }
    }

    // Check if wrapped in Semantics widget with label or ExcludeSemantics
    if (!hasSemantics) {
      final wrappingInfo = getSemanticWrappingInfo(node);
      hasSemantics = wrappingInfo.hasSemantics;
      isExcludedFromSemantics = wrappingInfo.isExcluded;
    }

    // Report if neither semantics nor exclusion is found
    if (!hasSemantics && !isExcludedFromSemantics) {
      reporter.atNode(node, code);
    }
  });
}