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 projectInfo = ProjectContext.getProjectInfo(context.filePath);
  if (projectInfo == null || !projectInfo.isFlutterProject) return;

  final path = context.filePath;
  if (!ProjectContext.hasDependency(path, 'just_audio') &&
      !ProjectContext.hasDependency(path, 'audio_service')) {
    return;
  }

  final root = ProjectContext.findProjectRoot(path);
  if (root == null) return;

  final hasIos = Directory('$root/ios').existsSync();
  final hasAndroid = Directory('$root/android').existsSync();
  final plist = InfoPlistChecker.forFile(path);
  final manifest = AndroidManifestChecker.forFile(path);

  context.addInstanceCreationExpression((InstanceCreationExpression node) {
    final typeName = node.constructorName.type.name.lexeme;
    if (typeName != 'AudioPlayer') return;

    final iosMisconfigured =
        hasIos &&
        plist != null &&
        plist.hasInfoPlist &&
        !plist.hasIosBackgroundAudioConfigured;
    final androidMisconfigured =
        hasAndroid &&
        manifest != null &&
        manifest.hasManifest &&
        !manifest.containsRaw('FOREGROUND_SERVICE');

    if (iosMisconfigured || androidMisconfigured) {
      reporter.atNode(node);
    }
  });
}