findClassDeclarationWithHttpResponse function

Future<ClassDeclaration?> findClassDeclarationWithHttpResponse({
  1. required String codePath,
  2. String? targetLibraryUri,
  3. String? targetClassName,
})

Implementation

Future<ClassDeclaration?> findClassDeclarationWithHttpResponse({
  required String codePath,
  String? targetLibraryUri,
  String? targetClassName,
}) async {
  final context = getProjectContext(codePath);
  if (context == null) {
    printError("no project context found");
    return null;
  }

  targetLibraryUri ??= 'package:http/src/response.dart';
  targetClassName ??= 'Response';

  final uriResult = context.currentSession.uriConverter.uriToPath(
    Uri.parse(targetLibraryUri),
  );
  if (uriResult == null) {
    printError('Could not find path for library "$targetLibraryUri"');
    return null;
  }
  final libraryPath = uriResult;

  final result = await context.currentSession.getResolvedUnit(libraryPath);

  if (result is ResolvedUnitResult) {
    for (final declaration in result.unit.declarations) {
      // print(declaration is ClassDeclaration);
      if (declaration is ClassDeclaration &&
          declaration.namePart.toSource() == targetClassName) {
        return declaration;
      }
    }
  }

  return null;
}