analyze method

Future<List<EndpointDefinition>> analyze({
  1. required CodeAnalysisCollector collector,
  2. Set<String>? changedFiles,
})

Analyze all files in the AnalysisContextCollection.

changedFiles is an optional list of files that should have their context refreshed before analysis. This is useful when only a subset of files have changed since updateFileContexts was last called.

Implementation

Future<List<EndpointDefinition>> analyze({
  required CodeAnalysisCollector collector,
  Set<String>? changedFiles,
}) async {
  await _refreshContextForFiles(changedFiles);

  var endpointDefs = <EndpointDefinition>[];

  List<(ResolvedLibraryResult, String)> validLibraries = [];
  Map<String, int> endpointClassMap = {};
  await for (var (library, filePath) in _libraries) {
    var endpointClasses = _getEndpointClasses(library);
    if (endpointClasses.isEmpty) {
      continue;
    }

    var maybeDartErrors = await _getErrorsForFile(library.session, filePath);
    if (maybeDartErrors.isNotEmpty) {
      collector.addError(
        SourceSpanSeverityException(
          'Endpoint analysis skipped due to invalid Dart syntax. Please '
          'review and correct the syntax errors.'
          '\nFile: $filePath',
          null,
          severity: SourceSpanSeverity.error,
        ),
      );

      continue;
    }

    for (var endpointClass in endpointClasses) {
      var className = endpointClass.name;
      endpointClassMap.update(
        className,
        (value) => value + 1,
        ifAbsent: () => 1,
      );
    }

    validLibraries.add((library, filePath));
  }

  var duplicateEndpointClasses = endpointClassMap.entries
      .where((entry) => entry.value > 1)
      .map((entry) => entry.key)
      .toSet();

  for (var (library, filePath) in validLibraries) {
    var severityExceptions = _validateLibrary(
      library,
      filePath,
      duplicateEndpointClasses,
    );
    collector.addErrors(severityExceptions.values.expand((e) => e).toList());

    var failingExceptions = _filterNoFailExceptions(severityExceptions);

    endpointDefs.addAll(_parseLibrary(
      library,
      collector,
      filePath,
      failingExceptions,
    ));
  }

  _endpointDefinitions = endpointDefs.toSet();
  return endpointDefs;
}