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,
) {
  final String fileSource = context.fileContent;

  context.addInstanceCreationExpression((InstanceCreationExpression node) {
    final String typeName = node.typeName;

    // Check for icon buttons without semantics
    if (typeName == 'IconButton') {
      final String argSource = node.argumentList.toSource();
      if (!argSource.contains('tooltip') &&
          !fileSource.contains('Semantics')) {
        reporter.atNode(node);
      }
    }

    // Check for GestureDetector on images
    if (typeName == 'GestureDetector') {
      final String argSource = node.argumentList.toSource();
      if (argSource.contains('Image') &&
          !argSource.contains('semanticLabel') &&
          !fileSource.contains('Semantics')) {
        reporter.atNode(node);
      }
    }
  });
}