extractMethodInvocationArgs function

JSONString? extractMethodInvocationArgs(
  1. String filePath,
  2. String? methodName
)

Extracts the argument list of a method invocation.

If methodName is provided, it finds the first invocation of that method. Returns a JSON string with the argument list source, e.g., {"data": "(id, name: event.name)"}.

Implementation

JSONString? extractMethodInvocationArgs(String filePath, String? methodName) {
  if (methodName == null) {
    return null;
  }
  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 = _MethodInvocationVisitor(methodName);
  unit.accept(visitor);

  if (visitor.arguments == null) {
    return null;
  }

  return JSONStringResult(data: visitor.arguments).toString();
}