Skip to content
This repository was archived by the owner on May 25, 2022. It is now read-only.

Commit b3881c2

Browse files
committed
Merge pull request #17 from davidmorgan/master
Add mixin generation for use with Angular templates.
2 parents d782219 + e92c51b commit b3881c2

File tree

8 files changed

+125
-22
lines changed

8 files changed

+125
-22
lines changed

CHANGELOG.md

Lines changed: 13 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,28 +1,32 @@
11
# Changelog
22

3+
## 0.2.0
4+
5+
- Add mixin generation for use with Angular templates.
6+
37
## 0.1.0
48

5-
- Upgrade to source_gen 0.5.0.
6-
- Breaking change; see example for required changes to build.dart.
9+
- Upgrade to source_gen 0.5.0.
10+
- Breaking change; see example for required changes to build.dart.
711

812
## 0.0.6
913

10-
- Check for missing import statement.
11-
- Fix constraints for source_gen.
14+
- Check for missing import statement.
15+
- Fix constraints for source_gen.
1216

1317
## 0.0.5
1418

15-
- Fix generation across multiple files, allow reuse of generated identifiers.
19+
- Fix generation across multiple files, allow reuse of generated identifiers.
1620

1721
## 0.0.4
1822

19-
- Fail on dynamic fields.
20-
- Export BuiltSet.
21-
- Allow part statements with double quote.
23+
- Fail on dynamic fields.
24+
- Export BuiltSet.
25+
- Allow part statements with double quote.
2226

2327
## 0.0.3
2428

25-
- Support multiple enums in one file by allowing arbitrary generated identifiers.
29+
- Support multiple enums in one file by allowing arbitrary generated identifiers.
2630

2731
## 0.0.2
2832

enum_class/pubspec.yaml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
name: enum_class
2-
version: 0.1.0
2+
version: 0.2.0
33
description: >
44
Dart classes as enums. This library is the runtime dependency.
55
authors:

enum_class_generator/lib/enum_class_generator.dart

Lines changed: 41 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -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

enum_class_generator/pubspec.yaml

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
name: enum_class_generator
2-
version: 0.1.0
2+
version: 0.2.0
33
description: >
44
Dart classes as enums. This library is the dev dependency.
55
authors:
@@ -13,7 +13,7 @@ dependencies:
1313
analyzer: '>=0.26.1 <1.0.0'
1414
build: '^0.3.0'
1515
built_collection: '^1.0.0'
16-
enum_class: '^0.1.0'
16+
enum_class: '^0.2.0'
1717
source_gen: '>=0.5.0 <0.6.0'
1818
quiver: '>=0.21.0 <0.22.0'
1919

enum_class_generator/test/enum_class_generator_test.dart

Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,8 @@ class TestEnum extends EnumClass {
2727
static BuiltSet<TestEnum> get values => _$values;
2828
static TestEnum valueOf(String name) => _$valueOf(name);
2929
}
30+
31+
abstract class TestEnumMixin = Object with _$TestEnumMixin;
3032
''';
3133

3234
final String correctOutput = r'''
@@ -56,6 +58,17 @@ TestEnum _$valueOf(String name) {
5658
5759
final BuiltSet<TestEnum> _$values =
5860
new BuiltSet<TestEnum>(const [_$yes, _$no, _$maybe,]);
61+
62+
class _$TestEnumMeta {
63+
const _$TestEnumMeta();
64+
TestEnum get yes => _$yes;
65+
TestEnum get no => _$no;
66+
TestEnum get maybe => _$maybe;
67+
}
68+
69+
abstract class _$TestEnumMixin {
70+
_$TestEnumMeta get TestEnum => const _$TestEnumMeta();
71+
}
5972
''';
6073

6174
void main() {
@@ -247,6 +260,8 @@ class TestEnum extends EnumClass {
247260
static BuiltSet<TestEnum> get values => _$values;
248261
static TestEnum valueOf(String name) => _$valueOf(name);
249262
}
263+
264+
abstract class TestEnumMixin = Object with _$TestEnumMixin;
250265
'''), endsWith(correctOutput));
251266
});
252267

@@ -560,6 +575,37 @@ part of test_enum;
560575
561576
// Error: Please make changes to use EnumClass.
562577
// TODO: Method: static TestEnum valueOf(String name) => _$valueOf(name)
578+
'''));
579+
});
580+
581+
test('fails with error on wrong mixin declaration', () async {
582+
expect(await generate(r'''
583+
library test_enum;
584+
585+
import 'package:enum_class/enum_class.dart';
586+
587+
part 'test_enum.g.dart';
588+
589+
class TestEnum extends EnumClass {
590+
static const TestEnum yes = _$yes;
591+
592+
const TestEnum._(String name) : super(name);
593+
594+
static BuiltSet<TestEnum> get values => _$values;
595+
static TestEnum valueOf(String name) => _$valueOf(name);
596+
}
597+
598+
class TestEnumMixin = Object with _$TestEnumMixin;
599+
'''), endsWith(r'''
600+
part of test_enum;
601+
602+
// **************************************************************************
603+
// Generator: EnumClassGenerator
604+
// Target: class TestEnum
605+
// **************************************************************************
606+
607+
// Error: Please make changes to use EnumClass.
608+
// TODO: Mixin: abstract class TestEnumMixin = Object with _$TestEnumMixin;
563609
'''));
564610
});
565611
});

example/lib/test_enum.dart

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,14 @@ class TestEnum extends EnumClass {
2828
static TestEnum valueOf(String name) => _$valueOf(name);
2929
}
3030

31+
/// Optionally, enum_class can generate a mixin to go with your enum for use
32+
/// with Angular. It exposes your enum constants as getters. So, if you mix it
33+
/// in to your Dart component class, the values become available to the
34+
/// corresponding Angular template.
35+
///
36+
/// Trigger mixin generation by writing a line like this one next to your enum.
37+
abstract class TestEnumMixin = Object with _$TestEnumMixin;
38+
3139
/// It's possible to have multiple enums in the same file.
3240
///
3341
/// For this to work, you need to change any generated names that clash. For

example/lib/test_enum.g.dart

Lines changed: 11 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

example/pubspec.yaml

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
name: example
2-
version: 0.1.0
2+
version: 0.2.0
33
description: >
44
Just an example, not for publishing.
55
authors:
@@ -10,7 +10,7 @@ environment:
1010
sdk: '>=1.8.0 <2.0.0'
1111

1212
dependencies:
13-
enum_class: '^0.1.0'
13+
enum_class: '^0.2.0'
1414

1515
dev_dependencies:
16-
enum_class_generator: '^0.1.0'
16+
enum_class_generator: '^0.2.0'

0 commit comments

Comments
 (0)