Skip to content

Commit

Permalink
[pigeon] Remove semicolons from Kotlin generation (flutter#5375)
Browse files Browse the repository at this point in the history
Callback calls were generating unnecessary semicolons at the end of the line, which was inconsistent with the rest of the file and with Kotlin style in general (as highlighted by experiments with `ktfmt`).
  • Loading branch information
stuartmorgan authored Nov 13, 2023
1 parent 72de224 commit 428ba3e
Show file tree
Hide file tree
Showing 5 changed files with 15 additions and 9 deletions.
6 changes: 5 additions & 1 deletion packages/pigeon/CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,7 +1,11 @@
## 13.1.1

* [kotlin] Removes unnecessary `;`s in generated code.

## 13.1.0

* [swift] Fixes Flutter Api void return error handling.
* This shouldn't be breaking for anyone, but if you were incorrectly getting
* This shouldn't be breaking for anyone, but if you were incorrectly getting
success responses, you may now be failing (correctly).
* Adds method channel name to error response when channel fails to connect.
* Reduces code generation duplication.
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 = '13.1.0';
const String pigeonVersion = '13.1.1';

/// Read all the content from [stdin] to a String.
String readStdin() {
Expand Down
10 changes: 5 additions & 5 deletions packages/pigeon/lib/kotlin_generator.dart
Original file line number Diff line number Diff line change
Expand Up @@ -412,17 +412,17 @@ class KotlinGenerator extends StructuredGenerator<KotlinOptions> {
indent.writeScoped('if (it is List<*>) {', '} ', () {
indent.writeScoped('if (it.size > 1) {', '} ', () {
indent.writeln(
'callback(Result.failure(FlutterError(it[0] as String, it[1] as String, it[2] as String?)));');
'callback(Result.failure(FlutterError(it[0] as String, it[1] as String, it[2] as String?)))');
}, addTrailingNewline: false);
if (!func.returnType.isNullable && !func.returnType.isVoid) {
indent.addScoped('else if (it[0] == null) {', '} ', () {
indent.writeln(
'callback(Result.failure(FlutterError("null-error", "Flutter api returned null value for non-null return value.", "")));');
'callback(Result.failure(FlutterError("null-error", "Flutter api returned null value for non-null return value.", "")))');
}, addTrailingNewline: false);
}
indent.addScoped('else {', '}', () {
if (func.returnType.isVoid) {
indent.writeln('callback(Result.success(Unit));');
indent.writeln('callback(Result.success(Unit))');
} else {
const String output = 'output';
// Nullable enums require special handling.
Expand All @@ -436,13 +436,13 @@ class KotlinGenerator extends StructuredGenerator<KotlinOptions> {
indent.writeln(
'val $output = ${_cast(root, indent, 'it[0]', type: func.returnType)}');
}
indent.writeln('callback(Result.success($output));');
indent.writeln('callback(Result.success($output))');
}
});
}, addTrailingNewline: false);
indent.addScoped('else {', '} ', () {
indent.writeln(
'callback(Result.failure(createConnectionError(channelName)));');
'callback(Result.failure(createConnectionError(channelName)))');
});
});
});
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%3Apigeon
version: 13.1.0 # This must match the version in lib/generator_tools.dart
version: 13.1.1 # This must match the version in lib/generator_tools.dart

environment:
sdk: ">=2.19.0 <4.0.0"
Expand Down
4 changes: 3 additions & 1 deletion packages/pigeon/test/kotlin_generator_test.dart
Original file line number Diff line number Diff line change
Expand Up @@ -518,6 +518,8 @@ void main() {
final String code = sink.toString();
expect(code, contains('callback: (Result<Unit>) -> Unit'));
expect(code, contains('callback(Result.success(Unit))'));
// Lines should not end in semicolons.
expect(code, isNot(contains(RegExp(r';\n'))));
});

test('gen host void argument api', () {
Expand Down Expand Up @@ -1584,6 +1586,6 @@ void main() {
expect(
code,
contains(
'callback(Result.failure(createConnectionError(channelName)));'));
'callback(Result.failure(createConnectionError(channelName)))'));
});
}

0 comments on commit 428ba3e

Please sign in to comment.