replaceMethodInvocationArgs function
Surgically replaces the argument list of the first invocation of methodName in filePath.
Returns the new file content if found and replaced, or null otherwise.
Implementation
String? replaceMethodInvocationArgs(
String filePath,
String methodName,
String newArgsSource,
) {
final file = File(filePath);
if (!file.existsSync()) return null;
final content = file.readAsStringSync();
final result = parseString(
content: content,
featureSet: FeatureSet.latestLanguageVersion(),
throwIfDiagnostics: false,
);
final unit = result.unit;
final visitor = _MethodInvocationReplacementVisitor(methodName);
unit.accept(visitor);
if (visitor.targetNode == null) return null;
final node = visitor.targetNode!;
return content.replaceRange(
node.argumentList.offset,
node.argumentList.end,
newArgsSource,
);
}