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

Commit 363b947

Browse files
committed
Merge pull request #9 from google/minor-fixes
Fail on dynamic fields. Export BuiltSet. Allow part statements with double quote.
2 parents c117e70 + 1fc3549 commit 363b947

File tree

8 files changed

+58
-25
lines changed

8 files changed

+58
-25
lines changed

CHANGELOG.md

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,11 @@
11
# Changelog
22

3+
## 0.0.4
4+
5+
- Fail on dynamic fields.
6+
- Export BuiltSet.
7+
- Allow part statements with double quote.
8+
39
## 0.0.3
410

511
- Support multiple enums in one file by allowing arbitrary generated identifiers.

enum_class/lib/enum_class.dart

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,8 @@
44

55
library enum_class;
66

7+
export 'package:built_collection/built_collection.dart' show BuiltSet;
8+
79
/// Enum Class base class.
810
///
911
/// Extend this class then use the enum_class.dart code generation

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.0.3
2+
version: 0.0.4
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: 10 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -51,7 +51,10 @@ class EnumClassGenerator extends Generator {
5151
final fileName =
5252
classElement.library.source.shortName.replaceAll('.dart', '');
5353
final expectedCode = "part '$fileName.g.dart';";
54-
return classElement.library.source.contents.data.contains(expectedCode)
54+
final alternativeExpectedCode = 'part "$fileName.g.dart";';
55+
final source = classElement.library.source.contents.data;
56+
return source.contains(expectedCode) ||
57+
source.contains(alternativeExpectedCode)
5558
? <String>[]
5659
: <String>['Import generated part: $expectedCode'];
5760
}
@@ -61,7 +64,8 @@ class EnumClassGenerator extends Generator {
6164
final result = <FieldElement>[];
6265
for (final field in classElement.fields) {
6366
final type = field.getter.returnType.displayName;
64-
if (!field.isSynthetic && type == enumName) result.add(field);
67+
if (!field.isSynthetic && (type == enumName || type == 'dynamic')) result
68+
.add(field);
6569
}
6670
return result;
6771
}
@@ -70,7 +74,10 @@ class EnumClassGenerator extends Generator {
7074
final result = <String>[];
7175
for (final field in fields) {
7276
final fieldName = field.displayName;
73-
if (!field.isConst && !field.isStatic) {
77+
if (field.getter.returnType.displayName == 'dynamic') {
78+
result.add('Specify a type for field "$fieldName".');
79+
continue;
80+
} else if (!field.isConst && !field.isStatic) {
7481
result.add('Make field "$fieldName" static const.');
7582
continue;
7683
} else if (!field.isConst) {

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.0.3
2+
version: 0.0.4
33
description: >
44
Dart classes as enums. This library is the dev dependency.
55
authors:
@@ -12,7 +12,7 @@ environment:
1212
dependencies:
1313
analyzer: '>=0.26.1 <1.0.0'
1414
built_collection: '^1.0.0'
15-
enum_class: '^0.0.3'
15+
enum_class: '^0.0.4'
1616
source_gen: '>=0.4.3 <1.0.0'
1717
quiver: '>=0.21.0 <0.22.0'
1818

enum_class_generator/test/enum_class_generator_test.dart

Lines changed: 35 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,6 @@ import 'package:test/test.dart';
1212
final String correctInput = r'''
1313
library test_enum;
1414
15-
import 'package:built_collection/built_collection.dart';
1615
import 'package:enum_class/enum_class.dart';
1716
1817
part 'test_enum.g.dart';
@@ -64,13 +63,46 @@ void main() {
6463
expect(await generate(correctInput), endsWith(correctOutput));
6564
});
6665

66+
test('allows part statement with double quotes', () async {
67+
expect(
68+
await generate(correctInput.replaceAll(
69+
"part 'test_enum.g.dart'", 'part "test_enum.g.dart"')),
70+
endsWith(correctOutput));
71+
});
72+
73+
test('ignores fields of different type', () async {
74+
expect(
75+
await generate(correctInput.replaceAll(
76+
'class TestEnum extends EnumClass {',
77+
'class TestEnum extends EnumClass {\n'
78+
' static const int anInt = 3;')),
79+
endsWith(correctOutput));
80+
});
81+
82+
test('fails on dynamic fields', () async {
83+
expect(
84+
await generate(correctInput.replaceAll(
85+
'class TestEnum extends EnumClass {',
86+
'class TestEnum extends EnumClass {\n'
87+
' static const anInt = 3;')),
88+
endsWith(r'''
89+
part of test_enum;
90+
91+
// **************************************************************************
92+
// Generator: EnumClassGenerator
93+
// Target: class TestEnum
94+
// **************************************************************************
95+
96+
// Error: Please make changes to use EnumClass.
97+
// TODO: Specify a type for field "anInt".
98+
'''));
99+
});
100+
67101
// TODO(davidmorgan): it would be better to fail with an error message.
68102
test('fails silently on missing enum_class import', () async {
69103
expect(await generate(r'''
70104
library test_enum;
71105
72-
import 'package:built_collection/built_collection.dart';
73-
74106
part 'test_enum.g.dart';
75107
76108
class TestEnum extends EnumClass {
@@ -90,7 +122,6 @@ class TestEnum extends EnumClass {
90122
expect(await generate(r'''
91123
library test_enum;
92124
93-
import 'package:built_collection/built_collection.dart';
94125
import 'package:enum_class/enum_class.dart';
95126
96127
part 'src_par.dart';
@@ -122,7 +153,6 @@ part of test_enum;
122153
expect(await generate(r'''
123154
library test_enum;
124155
125-
import 'package:built_collection/built_collection.dart';
126156
import 'package:enum_class/enum_class.dart';
127157
128158
part 'test_enum.g.dart';
@@ -154,7 +184,6 @@ part of test_enum;
154184
expect(await generate(r'''
155185
library test_enum;
156186
157-
import 'package:built_collection/built_collection.dart';
158187
import 'package:enum_class/enum_class.dart';
159188
160189
part 'test_enum.g.dart';
@@ -186,7 +215,6 @@ part of test_enum;
186215
expect(await generate(r'''
187216
library test_enum;
188217
189-
import 'package:built_collection/built_collection.dart';
190218
import 'package:enum_class/enum_class.dart';
191219
192220
part 'test_enum.g.dart';
@@ -209,7 +237,6 @@ class TestEnum extends EnumClass {
209237
expect(await generate(r'''
210238
library test_enum;
211239
212-
import 'package:built_collection/built_collection.dart';
213240
import 'package:enum_class/enum_class.dart';
214241
215242
part 'test_enum.g.dart';
@@ -258,7 +285,6 @@ final BuiltSet<TestEnum> _$values =
258285
expect(await generate(r'''
259286
library test_enum;
260287
261-
import 'package:built_collection/built_collection.dart';
262288
import 'package:enum_class/enum_class.dart';
263289
264290
part 'test_enum.g.dart';
@@ -307,7 +333,6 @@ final BuiltSet<TestEnum> _$vls =
307333
expect(await generate(r'''
308334
library test_enum;
309335
310-
import 'package:built_collection/built_collection.dart';
311336
import 'package:enum_class/enum_class.dart';
312337
313338
part 'test_enum.g.dart';
@@ -339,7 +364,6 @@ part of test_enum;
339364
expect(await generate(r'''
340365
library test_enum;
341366
342-
import 'package:built_collection/built_collection.dart';
343367
import 'package:enum_class/enum_class.dart';
344368
345369
part 'test_enum.g.dart';
@@ -371,7 +395,6 @@ part of test_enum;
371395
expect(await generate(r'''
372396
library test_enum;
373397
374-
import 'package:built_collection/built_collection.dart';
375398
import 'package:enum_class/enum_class.dart';
376399
377400
part 'test_enum.g.dart';
@@ -401,7 +424,6 @@ part of test_enum;
401424
expect(await generate(r'''
402425
library test_enum;
403426
404-
import 'package:built_collection/built_collection.dart';
405427
import 'package:enum_class/enum_class.dart';
406428
407429
part 'test_enum.g.dart';
@@ -433,7 +455,6 @@ part of test_enum;
433455
expect(await generate(r'''
434456
library test_enum;
435457
436-
import 'package:built_collection/built_collection.dart';
437458
import 'package:enum_class/enum_class.dart';
438459
439460
part 'test_enum.g.dart';
@@ -469,7 +490,6 @@ part of test_enum;
469490
expect(await generate(r'''
470491
library test_enum;
471492
472-
import 'package:built_collection/built_collection.dart';
473493
import 'package:enum_class/enum_class.dart';
474494
475495
part 'test_enum.g.dart';
@@ -500,7 +520,6 @@ part of test_enum;
500520
expect(await generate(r'''
501521
library test_enum;
502522
503-
import 'package:built_collection/built_collection.dart';
504523
import 'package:enum_class/enum_class.dart';
505524
506525
part 'test_enum.g.dart';

example/lib/test_enum.dart

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,6 @@
44

55
library test_enum;
66

7-
import 'package:built_collection/built_collection.dart';
87
import 'package:enum_class/enum_class.dart';
98

109
part 'test_enum.g.dart';

example/pubspec.yaml

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ environment:
1010
sdk: '>=1.8.0 <2.0.0'
1111

1212
dependencies:
13-
enum_class: '^0.0.3'
13+
enum_class: '^0.0.4'
1414

1515
dev_dependencies:
16-
enum_class_generator: '^0.0.3'
16+
enum_class_generator: '^0.0.4'

0 commit comments

Comments
 (0)