Line data Source code
1 : // ignore_for_file: implementation_imports, comment_references 2 : import 'package:analyzer/dart/element/element.dart'; 3 : import 'package:enum_assist/src/configs/class_config.dart'; 4 : import 'package:enum_assist/src/enum_field.dart'; 5 : import 'package:meta/meta.dart'; 6 : import 'package:source_helper/source_helper.dart'; 7 : 8 : /// {@template enum_assist.helper_core} 9 : /// A helper class that provides the core functionality for generating 10 : /// [EnumHelper]s. 11 : /// {@endtemplate} 12 : abstract class HelperCore { 13 : /// {@macro enum_assist.helper_core} 14 0 : HelperCore(this.element, this.config) : assert(element.isEnum); 15 : 16 : /// The [ClassElement] that this helper is for. 17 : final ClassElement element; 18 : 19 : /// The [ClassConfig] for this helper. 20 : final ClassConfig config; 21 : 22 : /// The method to use for generating the code 23 : Iterable<String> generate(); 24 : 25 : /// The name of the enum value for the given [field]. 26 0 : String _nameAccess(FieldElement field) => field.name; 27 : 28 : /// the name of the enum 29 0 : @protected 30 : @visibleForTesting 31 0 : String get enumName => element.name.nonPrivate; 32 : 33 : /// Returns a [Set] of all instance [FieldElement] items for [element] and 34 : /// super classes, sorted first by their location in the inheritance hierarchy 35 : /// (super first) and then by their location in the source file. 36 0 : @protected 37 : Iterable<FieldElement> get fieldElements { 38 : // Get all of the fields that need to be assigned 39 0 : final elementInstanceFields = element.fields.where((e) => e.isEnumConstant); 40 : 41 0 : return elementInstanceFields.toSet(); 42 : } 43 : 44 : /// Returns the names of all fields 45 0 : @protected 46 : @visibleForTesting 47 0 : Iterable<String> get fieldNames => fieldElements.map(_nameAccess); 48 : 49 : /// returns a list of [EnumField]s for the given [fieldElements] 50 0 : @protected 51 : @visibleForTesting 52 : Iterable<EnumField> get fields => 53 0 : fieldElements.map((e) => EnumField.config(e, config)); 54 : }