@@ -53,15 +53,25 @@ class EnumClassGenerator extends Generator {
5353 _checkConstructor (classElement),
5454 _checkValuesGetter (classElement),
5555 _checkValueOf (classElement)
56- ]);
56+ ]).toList ();
57+
58+ final mixinElement = classElement.library.getType (enumName + 'Mixin' );
59+ final shouldGenerateMixin = mixinElement != null ;
60+ if (shouldGenerateMixin) {
61+ final expectedCode =
62+ 'abstract class ${enumName }Mixin = Object with _\$ ${enumName }Mixin;' ;
63+ if (mixinElement.computeNode ().toString () != expectedCode) {
64+ errors.add ('Mixin: $expectedCode ' );
65+ }
66+ }
5767
5868 if (errors.isNotEmpty) {
5969 throw new InvalidGenerationSourceError (
6070 'Please make changes to use EnumClass.' ,
6171 todo: errors.join (' ' ));
6272 }
6373
64- return _generateCode (classElement, enumName, fields);
74+ return _generateCode (classElement, enumName, fields, shouldGenerateMixin );
6575 }
6676
6777 Iterable <String > _checkPart (ClassElement classElement) {
@@ -81,8 +91,9 @@ class EnumClassGenerator extends Generator {
8191 final result = < FieldElement > [];
8292 for (final field in classElement.fields) {
8393 final type = field.getter.returnType.displayName;
84- if (! field.isSynthetic && (type == enumName || type == 'dynamic' )) result
85- .add (field);
94+ if (! field.isSynthetic && (type == enumName || type == 'dynamic' )) {
95+ result.add (field);
96+ }
8697 }
8798 return result;
8899 }
@@ -171,7 +182,7 @@ class EnumClassGenerator extends Generator {
171182 }
172183
173184 String _generateCode (ClassElement classElement, String enumName,
174- Iterable <FieldElement > fields) {
185+ Iterable <FieldElement > fields, bool generateMixin ) {
175186 final result = new StringBuffer ();
176187
177188 for (final field in fields) {
@@ -205,6 +216,28 @@ class EnumClassGenerator extends Generator {
205216 }
206217 result.writeln (']);' );
207218
219+ if (generateMixin) result.write (_generateMixin (enumName, fields));
220+
221+ return result.toString ();
222+ }
223+
224+ String _generateMixin (String enumName, Iterable <FieldElement > fields) {
225+ final result = new StringBuffer ();
226+
227+ result.writeln ('class _\$ ${enumName }Meta {' );
228+ result.writeln ('const _\$ ${enumName }Meta();' );
229+ for (final field in fields) {
230+ final fieldName = field.displayName;
231+ result
232+ .writeln ('$enumName get $fieldName => _\$ ${_getGeneratedIdentifier (
233+ field )};' );
234+ }
235+ result.writeln ('}' );
236+ result.writeln ('abstract class _\$ ${enumName }Mixin {' );
237+ result.writeln (
238+ '_\$ ${enumName }Meta get $enumName => const _\$ ${enumName }Meta();' );
239+ result.writeln ('}' );
240+
208241 return result.toString ();
209242 }
210243
@@ -215,8 +248,9 @@ class EnumClassGenerator extends Generator {
215248
216249 String _getValueOfIdentifier (String source, String enumName) {
217250 final matches = new RegExp (r'static ' +
218- enumName +
219- r' valueOf\(String name\) \=\> \_\$(\w+)\(name\)\;' ).allMatches (source);
251+ enumName +
252+ r' valueOf\(String name\) \=\> \_\$(\w+)\(name\)\;' )
253+ .allMatches (source);
220254 return matches.isEmpty ? null : matches.first.group (1 );
221255 }
222256
0 commit comments