runCliAnalysis method

Future<Iterable<UnnecessaryNullableFileReport>> runCliAnalysis(
  1. Iterable<String> folders,
  2. String rootFolder,
  3. UnnecessaryNullableConfig config, {
  4. String? sdkPath,
})

Returns a list of unnecessary nullable parameters reports for analyzing all files in the given folders. The analysis is configured with the config.

Implementation

Future<Iterable<UnnecessaryNullableFileReport>> runCliAnalysis(
  Iterable<String> folders,
  String rootFolder,
  UnnecessaryNullableConfig config, {
  String? sdkPath,
}) async {
  final collection =
      createAnalysisContextCollection(folders, rootFolder, sdkPath);

  final invocationsUsages = InvocationsUsage();
  final declarationsUsages = <String, DeclarationUsages>{};

  for (final context in collection.contexts) {
    final unnecessaryNullableAnalysisConfig =
        _getAnalysisConfig(context, rootFolder, config);

    if (config.shouldPrintConfig) {
      _logger?.printConfig(unnecessaryNullableAnalysisConfig.toJson());
    }

    final filePaths = getFilePaths(
      folders,
      context,
      rootFolder,
      unnecessaryNullableAnalysisConfig.globalExcludes,
    );

    final analyzedFiles =
        filePaths.intersection(context.contextRoot.analyzedFiles().toSet());

    final contextsLength = collection.contexts.length;
    final filesLength = analyzedFiles.length;
    final updateMessage = contextsLength == 1
        ? 'Checking unnecessary nullable parameters for $filesLength file(s)'
        : 'Checking unnecessary nullable parameters for ${collection.contexts.indexOf(context) + 1}/$contextsLength contexts with $filesLength file(s)';
    _logger?.progress.update(updateMessage);

    for (final filePath in analyzedFiles) {
      _logger?.infoVerbose('Analyzing $filePath');

      final unit = await context.currentSession.getResolvedUnit(filePath);

      final invocationsUsage = _analyzeInvocationsUsage(unit);
      if (invocationsUsage != null) {
        _logger?.infoVerbose(
          'Found invocations: ${invocationsUsage.elements.length}',
        );

        invocationsUsages.merge(invocationsUsage);
      }

      if (!unnecessaryNullableAnalysisConfig.analyzerExcludedPatterns
          .any((pattern) => pattern.matches(filePath))) {
        _logger
            ?.infoVerbose('Found declarations: ${declarationsUsages.length}');

        declarationsUsages[filePath] = _analyzeDeclarationsUsage(unit);
      }
    }
  }

  if (!config.isMonorepo) {
    _logger?.infoVerbose(
      'Removing globally exported files with declarations from the analysis: ${invocationsUsages.exports.length}',
    );
    invocationsUsages.exports.forEach(declarationsUsages.remove);
  }

  return _getReports(invocationsUsages, declarationsUsages, rootFolder);
}