parseFactoryFields function
Parameter name to type (nullable marker kept, required dropped) of the
first const factory X({...}) in source, or null if there is none.
Implementation
Map<String, String>? parseFactoryFields(String source) {
final match = RegExp(
r'const\s+factory\s+\w+\s*\(\s*\{(.*?)\}\s*\)\s*=',
dotAll: true,
).firstMatch(source);
if (match == null) return null;
final fields = <String, String>{};
for (var line in match[1]!.split('\n')) {
line = line
.trim()
.replaceAll(RegExp(r"@\w+(\((?:[^()']|'[^']*'|\([^()]*\))*\))?\s*"), '')
.replaceAll(RegExp(r'\brequired\s+'), '')
.trim();
final field = RegExp(r'^(.+?)\s+(\w+),?$').firstMatch(line);
if (field != null) fields[field[2]!] = field[1]!.replaceAll(' ', '');
}
return fields;
}