I am trying to make a source generator that would mimic C# anonymous objects, because they are great for when you are manipulating with collections (Select, GroupBy, etc.).
Imagine this code:
class Person {
final String firstName;
final String lastName;
final int age;
Person(this.firstName, this.age, this.lastName);
}
class TestClass {
final _data = [
Person('John', 'Doe', 51),
Person('Jane', 'Doe', 50),
Person('John', 'Smith', 40),
];
void testMethod() {
final map1 = _data.map((p) => _$$1(name: p.firstName, age: p.age));
final map2 = _data.map((p) => _$$2(fullName: '${p.firstName} ${p.lastName}', age: p.age));
}
}
Those _$$x objects are what I want to generate now. I need to somehow find them and find what is being passed into them, so my code generator would generate this:
class _$$1 {
final String name;
final int age;
const _$$1({required this.name, required this.age});
}
class _$$2 {
final String fullName;
final int age;
const _$$1({required this.fullName, required this.age});
}
but I cannot seem to even find method content:
FutureOr<String?> generate(LibraryReader library, BuildStep buildStep) {
for (final clazz in library.classes) {
final method = clazz.methods.first;
method.visitChildren(RecursiveElementVisitor<dynamic>());
}
}
it looks like the MethodElement doesn't have any children? so this doesn't look like the right way.
Is there any other way to find what I need?