Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[pigeon] fix named parameters in flutter api #5663

Closed
wants to merge 7 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions packages/pigeon/CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,8 @@
## 15.0.2

* [dart] Adds named parameters to flutter API methods.
* [dart] Fixes named parameters in test output of host API methods.

## 15.0.1

* [java] Adds @CanIgnoreReturnValue annotation to class builder.
Expand Down
2 changes: 1 addition & 1 deletion packages/pigeon/lib/ast.dart
Original file line number Diff line number Diff line change
Expand Up @@ -277,7 +277,7 @@ class Parameter extends NamedType {
bool? isPositional,
bool? isRequired,
super.documentationComments,
}) : isNamed = isNamed ?? true,
Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This fixes existing tests that don't specify this but still assume it's false.

Since this wasn't used anywhere besides toString, I don't consider this a breaking change.

}) : isNamed = isNamed ?? false,
isOptional = isOptional ?? false,
isPositional = isPositional ?? true,
isRequired = isRequired ?? true;
Expand Down
4 changes: 2 additions & 2 deletions packages/pigeon/lib/dart_generator.dart
Original file line number Diff line number Diff line change
Expand Up @@ -418,9 +418,9 @@ $resultAt != null
}
});
final Iterable<String> argNames =
indexMap(func.parameters, (int index, NamedType field) {
indexMap(func.parameters, (int index, Parameter field) {
final String name = _getSafeArgumentName(index, field);
return '$name${field.type.isNullable ? '' : '!'}';
return '${field.isNamed ? '${field.name}: ' : ''}$name${field.type.isNullable ? '' : '!'}';
});
call = 'api.${func.name}(${argNames.join(', ')})';
}
Expand Down
2 changes: 1 addition & 1 deletion packages/pigeon/lib/generator_tools.dart
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ import 'ast.dart';
/// The current version of pigeon.
///
/// This must match the version in pubspec.yaml.
const String pigeonVersion = '15.0.1';
const String pigeonVersion = '15.0.2';

/// Read all the content from [stdin] to a String.
String readStdin() {
Expand Down
8 changes: 4 additions & 4 deletions packages/pigeon/lib/pigeon_lib.dart
Original file line number Diff line number Diff line change
Expand Up @@ -1132,10 +1132,10 @@ class _RootBuilder extends dart_ast_visitor.RecursiveAstVisitor<Object?> {
),
name: formalParameter.name?.lexeme ?? '',
offset: formalParameter.offset,
isNamed: isNamed,
isOptional: isOptional,
isPositional: isPositional,
isRequired: isRequired,
isNamed: isNamed ?? formalParameter.isNamed,
isOptional: isOptional ?? formalParameter.isOptional,
isPositional: isPositional ?? formalParameter.isPositional,
isRequired: isRequired ?? formalParameter.isRequired,
defaultValue: defaultValue,
);
} else if (simpleFormalParameter != null) {
Expand Down
20 changes: 20 additions & 0 deletions packages/pigeon/pigeons/core_tests.dart
Original file line number Diff line number Diff line change
Expand Up @@ -473,6 +473,11 @@ abstract class HostIntegrationCoreApi {
@SwiftFunction('callFlutterEcho(_:)')
AnEnum callFlutterEchoEnum(AnEnum anEnum);

@async
@ObjCSelector('callFlutterEchoNamedString:')
@SwiftFunction('callFlutterEchoNamed(_:)')
String callFlutterEchoNamedString({String aString = 'default'});

@async
@ObjCSelector('callFlutterEchoNullableBool:')
@SwiftFunction('callFlutterEchoNullable(_:)')
Expand Down Expand Up @@ -513,6 +518,11 @@ abstract class HostIntegrationCoreApi {
@ObjCSelector('callFlutterEchoNullableEnum:')
@SwiftFunction('callFlutterNullableEcho(_:)')
AnEnum? callFlutterEchoNullableEnum(AnEnum? anEnum);

@async
@ObjCSelector('callFlutterEchoNullableNamedString:')
@SwiftFunction('callFlutterEchoNullableNamed(_:)')
String callFlutterEchoNullableNamedString({String? aString});
}

/// The core interface that the Dart platform_test code implements for host
Expand Down Expand Up @@ -589,6 +599,11 @@ abstract class FlutterIntegrationCoreApi {
@SwiftFunction('echo(_:)')
AnEnum echoEnum(AnEnum anEnum);

/// Returns the default string.
@ObjCSelector('echoNamedDefaultString:')
@SwiftFunction('echoNamedDefault(_:)')
String echoNamedDefaultString({String aString = 'default'});

// ========== Nullable argument/return type tests ==========

/// Returns the passed boolean, to test serialization and deserialization.
Expand Down Expand Up @@ -631,6 +646,11 @@ abstract class FlutterIntegrationCoreApi {
@SwiftFunction('echoNullable(_:)')
AnEnum? echoNullableEnum(AnEnum? anEnum);

/// Returns the passed in string.
@ObjCSelector('echoNamedNullableString:')
@SwiftFunction('echoNamed(_:)')
String? echoNamedNullableString({String? aNullableString});

// ========== Async tests ==========
// These are minimal since async FlutterApi only changes Dart generation.
// Currently they aren't integration tested, but having them here ensures
Expand Down
2 changes: 1 addition & 1 deletion packages/pigeon/pubspec.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ name: pigeon
description: Code generator tool to make communication between Flutter and the host platform type-safe and easier.
repository: https://github.com/flutter/packages/tree/main/packages/pigeon
issue_tracker: https://github.com/flutter/flutter/issues?q=is%3Aissue+is%3Aopen+label%3A%22p%3A+pigeon%22
version: 15.0.1 # This must match the version in lib/generator_tools.dart
version: 15.0.2 # This must match the version in lib/generator_tools.dart

environment:
sdk: ">=3.0.0 <4.0.0"
Expand Down
35 changes: 35 additions & 0 deletions packages/pigeon/test/dart_generator_test.dart
Original file line number Diff line number Diff line change
Expand Up @@ -1417,6 +1417,41 @@ void main() {
expect(code, contains('void doit(int? foo);'));
});

test('named argument flutter', () {
final Root root = Root(
apis: <Api>[
Api(name: 'Api', location: ApiLocation.flutter, methods: <Method>[
Method(
name: 'doit',
returnType: const TypeDeclaration.voidDeclaration(),
parameters: <Parameter>[
Parameter(
name: 'foo',
type: const TypeDeclaration(
baseName: 'int',
isNullable: false,
),
isNamed: true,
isPositional: false),
])
])
],
classes: <Class>[],
enums: <Enum>[],
);
final StringBuffer sink = StringBuffer();
const DartGenerator generator = DartGenerator();
generator.generate(
const DartOptions(),
root,
sink,
dartPackageName: DEFAULT_PACKAGE_NAME,
);
final String code = sink.toString();
expect(code, contains('void doit({required int foo});'));
expect(code, contains('api.doit(foo: arg_foo!)'));
});

test('uses output package name for imports', () {
const String overriddenPackageName = 'custom_name';
const String outputPackageName = 'some_output_package';
Expand Down