Line data Source code
1 : // ignore_for_file: implementation_imports 2 : 3 : import 'package:analyzer/dart/element/element.dart'; 4 : import 'package:build/build.dart'; 5 : import 'package:class_fields_annotation/src/fields.dart'; 6 : import 'package:source_gen/source_gen.dart'; 7 : 8 : import 'package:class_fields/domain/class.dart'; 9 : import 'package:class_fields/templates/fields_template.dart'; 10 : 11 : /// {@template fields_generator} 12 : /// A [Generator] that generates all keys for fields 13 : /// from the [Fields] annotation 14 : /// {@endtemplate} 15 : class FieldsGenerator extends GeneratorForAnnotation<Fields> { 16 : /// {@macro fields_generator} 17 1 : const FieldsGenerator(); 18 : 19 1 : @override 20 : String generateForAnnotatedElement( 21 : Element element, 22 : ConstantReader annotation, 23 : BuildStep buildStep, 24 : ) { 25 1 : if (element is! ClassElement) { 26 0 : throw InvalidGenerationSourceError( 27 0 : 'Generator cannot target `${element.runtimeType}`.', 28 : element: element, 29 : ); 30 : } 31 : 32 1 : final buffer = StringBuffer(); 33 : 34 1 : final subject = Class.fromElement(element); 35 : 36 2 : FieldsTemplate(subject).addToBuffer(buffer); 37 : 38 1 : return buffer.toString(); 39 : } 40 : }