extractMethodResponseType function

JSONString extractMethodResponseType(
  1. String filePath,
  2. String methodName
)

Extracts the return type of a given method from a Dart file.

Returns the source string of the return type. Throws an exception if the method is not found. Returns 'dynamic' if the method has no explicit return type.

Implementation

JSONString extractMethodResponseType(String filePath, String methodName) {
  final file = File(filePath);
  if (!file.existsSync()) {
    throw Exception('File not found: $filePath');
  }

  final content = file.readAsStringSync();
  final result = parseString(
    content: content,
    featureSet: FeatureSet.latestLanguageVersion(),
    throwIfDiagnostics: false,
  );

  final unit = result.unit;
  final visitor = _MethodResponseTypeVisitor(methodName);
  unit.accept(visitor);

  if (visitor.returnType == null) {
    throw Exception('Method "$methodName" not found in $filePath');
  }

  return visitor.returnType!;
}