Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit 2fd81ca

Browse files
authored
[pigeon] Made using enums in type arguments an error, added workaround (flutter#527)
1 parent 87d6919 commit 2fd81ca

5 files changed

Lines changed: 59 additions & 7 deletions

File tree

packages/pigeon/CHANGELOG.md

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,9 @@
1-
## NEXT
1+
## 1.0.11
22

3-
* [ci] Started transitioning to a Dart test runner, added windows support.
3+
* [ci] Starts transition to a Dart test runner, adds windows support.
4+
* [front-end] Starts issuing an error if enums are used in type arguments.
5+
* [front-end] Passes through all enums, referenced or not so they can be used as
6+
a work around for direct enum support.
47

58
## 1.0.10
69

packages/pigeon/lib/generator_tools.dart

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ import 'dart:mirrors';
88
import 'ast.dart';
99

1010
/// The current version of pigeon. This must match the version in pubspec.yaml.
11-
const String pigeonVersion = '1.0.10';
11+
const String pigeonVersion = '1.0.11';
1212

1313
/// Read all the content from [stdin] to a String.
1414
String readStdin() {

packages/pigeon/lib/pigeon_lib.dart

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -423,6 +423,13 @@ List<Error> _validateAst(Root root, String source) {
423423
lineNumber: _calculateLineNumberNullable(source, field.offset),
424424
));
425425
}
426+
if (customEnums.contains(typeArgument.baseName)) {
427+
result.add(Error(
428+
message:
429+
'Enum types aren\'t supported in type arguments in "${field.name}" in class "${klass.name}".',
430+
lineNumber: _calculateLineNumberNullable(source, field.offset),
431+
));
432+
}
426433
}
427434
}
428435
if (!(validTypes.contains(field.type.baseName) ||
@@ -544,9 +551,6 @@ class _RootBuilder extends dart_ast_visitor.RecursiveAstVisitor<Object?> {
544551
.removeWhere((Class x) => !referencedTypeNames.contains(x.name));
545552

546553
final List<Enum> referencedEnums = List<Enum>.from(_enums);
547-
referencedEnums.removeWhere(
548-
(final Enum anEnum) => !referencedTypeNames.contains(anEnum.name));
549-
550554
final Root completeRoot =
551555
Root(apis: _apis, classes: referencedClasses, enums: referencedEnums);
552556

packages/pigeon/pubspec.yaml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ name: pigeon
22
description: Code generator tool to make communication between Flutter and the host platform type-safe and easier.
33
repository: https://github.com/flutter/packages/tree/master/packages/pigeon
44
issue_tracker: https://github.com/flutter/flutter/issues?q=is%3Aissue+is%3Aopen+label%3Apigeon
5-
version: 1.0.10 # This must match the version in lib/generator_tools.dart
5+
version: 1.0.11 # This must match the version in lib/generator_tools.dart
66

77
environment:
88
sdk: '>=2.12.0 <3.0.0'

packages/pigeon/test/pigeon_lib_test.dart

Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -901,4 +901,49 @@ abstract class Api {
901901
final ParseResults results = _parseSource(code);
902902
expect(results.errors.length, 0);
903903
});
904+
905+
test('Enum key not supported', () {
906+
const String code = '''
907+
enum MessageKey {
908+
title,
909+
subtitle,
910+
description,
911+
}
912+
913+
class Message {
914+
int? id;
915+
Map<MessageKey?, String?>? additionalProperties;
916+
}
917+
918+
@HostApi()
919+
abstract class HostApiBridge {
920+
void sendMessage(Message message);
921+
}
922+
''';
923+
final ParseResults results = _parseSource(code);
924+
expect(results.errors.length, 1);
925+
});
926+
927+
test('Export unreferenced enums', () {
928+
const String code = '''
929+
enum MessageKey {
930+
title,
931+
subtitle,
932+
description,
933+
}
934+
935+
class Message {
936+
int? id;
937+
Map<int?, String?>? additionalProperties;
938+
}
939+
940+
@HostApi()
941+
abstract class HostApiBridge {
942+
void sendMessage(Message message);
943+
}
944+
''';
945+
final ParseResults results = _parseSource(code);
946+
expect(results.root.enums.length, 1);
947+
expect(results.root.enums[0].name, 'MessageKey');
948+
});
904949
}

0 commit comments

Comments
 (0)