Skip to content

Commit d7dee79

Browse files
authored
Remove flutter api named and optional parameters (flutter#5689)
fixes flutter#140045 replaces flutter/packages#5663 as no longer needed.
1 parent baae12e commit d7dee79

File tree

8 files changed

+92
-9
lines changed

8 files changed

+92
-9
lines changed

packages/pigeon/CHANGELOG.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,8 @@
1+
## 15.0.2
2+
3+
* Prevents optional and non-positional parameters in Flutter APIs.
4+
* [dart] Fixes named parameters in test output of host API methods.
5+
16
## 15.0.1
27

38
* [java] Adds @CanIgnoreReturnValue annotation to class builder.

packages/pigeon/lib/ast.dart

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -277,7 +277,7 @@ class Parameter extends NamedType {
277277
bool? isPositional,
278278
bool? isRequired,
279279
super.documentationComments,
280-
}) : isNamed = isNamed ?? true,
280+
}) : isNamed = isNamed ?? false,
281281
isOptional = isOptional ?? false,
282282
isPositional = isPositional ?? true,
283283
isRequired = isRequired ?? true;

packages/pigeon/lib/dart_generator.dart

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -418,9 +418,9 @@ $resultAt != null
418418
}
419419
});
420420
final Iterable<String> argNames =
421-
indexMap(func.parameters, (int index, NamedType field) {
421+
indexMap(func.parameters, (int index, Parameter field) {
422422
final String name = _getSafeArgumentName(index, field);
423-
return '$name${field.type.isNullable ? '' : '!'}';
423+
return '${field.isNamed ? '${field.name}: ' : ''}$name${field.type.isNullable ? '' : '!'}';
424424
});
425425
call = 'api.${func.name}(${argNames.join(', ')})';
426426
}

packages/pigeon/lib/generator_tools.dart

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ import 'ast.dart';
1313
/// The current version of pigeon.
1414
///
1515
/// This must match the version in pubspec.yaml.
16-
const String pigeonVersion = '15.0.1';
16+
const String pigeonVersion = '15.0.2';
1717

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

packages/pigeon/lib/pigeon_lib.dart

Lines changed: 19 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -809,6 +809,21 @@ List<Error> _validateAst(Root root, String source) {
809809
lineNumber: _calculateLineNumberNullable(source, param.offset),
810810
));
811811
}
812+
if (api.location == ApiLocation.flutter) {
813+
if (!param.isPositional) {
814+
result.add(Error(
815+
message:
816+
'FlutterApi method parameters must be positional, in method "${method.name}" in API: "${api.name}"',
817+
lineNumber: _calculateLineNumberNullable(source, param.offset),
818+
));
819+
} else if (param.isOptional) {
820+
result.add(Error(
821+
message:
822+
'FlutterApi method parameters must not be optional, in method "${method.name}" in API: "${api.name}"',
823+
lineNumber: _calculateLineNumberNullable(source, param.offset),
824+
));
825+
}
826+
}
812827
}
813828
if (method.objcSelector.isNotEmpty) {
814829
if (':'.allMatches(method.objcSelector).length !=
@@ -1132,10 +1147,10 @@ class _RootBuilder extends dart_ast_visitor.RecursiveAstVisitor<Object?> {
11321147
),
11331148
name: formalParameter.name?.lexeme ?? '',
11341149
offset: formalParameter.offset,
1135-
isNamed: isNamed,
1136-
isOptional: isOptional,
1137-
isPositional: isPositional,
1138-
isRequired: isRequired,
1150+
isNamed: isNamed ?? formalParameter.isNamed,
1151+
isOptional: isOptional ?? formalParameter.isOptional,
1152+
isPositional: isPositional ?? formalParameter.isPositional,
1153+
isRequired: isRequired ?? formalParameter.isRequired,
11391154
defaultValue: defaultValue,
11401155
);
11411156
} else if (simpleFormalParameter != null) {

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/main/packages/pigeon
44
issue_tracker: https://github.com/flutter/flutter/issues?q=is%3Aissue+is%3Aopen+label%3A%22p%3A+pigeon%22
5-
version: 15.0.1 # This must match the version in lib/generator_tools.dart
5+
version: 15.0.2 # This must match the version in lib/generator_tools.dart
66

77
environment:
88
sdk: ">=3.0.0 <4.0.0"

packages/pigeon/test/dart_generator_test.dart

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1417,6 +1417,41 @@ void main() {
14171417
expect(code, contains('void doit(int? foo);'));
14181418
});
14191419

1420+
test('named argument flutter', () {
1421+
final Root root = Root(
1422+
apis: <Api>[
1423+
Api(name: 'Api', location: ApiLocation.flutter, methods: <Method>[
1424+
Method(
1425+
name: 'doit',
1426+
returnType: const TypeDeclaration.voidDeclaration(),
1427+
parameters: <Parameter>[
1428+
Parameter(
1429+
name: 'foo',
1430+
type: const TypeDeclaration(
1431+
baseName: 'int',
1432+
isNullable: false,
1433+
),
1434+
isNamed: true,
1435+
isPositional: false),
1436+
])
1437+
])
1438+
],
1439+
classes: <Class>[],
1440+
enums: <Enum>[],
1441+
);
1442+
final StringBuffer sink = StringBuffer();
1443+
const DartGenerator generator = DartGenerator();
1444+
generator.generate(
1445+
const DartOptions(),
1446+
root,
1447+
sink,
1448+
dartPackageName: DEFAULT_PACKAGE_NAME,
1449+
);
1450+
final String code = sink.toString();
1451+
expect(code, contains('void doit({required int foo});'));
1452+
expect(code, contains('api.doit(foo: arg_foo!)'));
1453+
});
1454+
14201455
test('uses output package name for imports', () {
14211456
const String overriddenPackageName = 'custom_name';
14221457
const String outputPackageName = 'some_output_package';

packages/pigeon/test/pigeon_lib_test.dart

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1324,4 +1324,32 @@ abstract class Api {
13241324
});
13251325
await completer.future;
13261326
});
1327+
1328+
test('unsupported non-positional parameters on FlutterApi', () {
1329+
const String code = '''
1330+
@FlutterApi()
1331+
abstract class Api {
1332+
int? calc({int? anInt});
1333+
}
1334+
''';
1335+
1336+
final ParseResults results = parseSource(code);
1337+
expect(results.errors.length, 1);
1338+
expect(results.errors[0].message,
1339+
contains('FlutterApi method parameters must be positional'));
1340+
});
1341+
1342+
test('unsupported optional parameters on FlutterApi', () {
1343+
const String code = '''
1344+
@FlutterApi()
1345+
abstract class Api {
1346+
int? calc([int? anInt]);
1347+
}
1348+
''';
1349+
1350+
final ParseResults results = parseSource(code);
1351+
expect(results.errors.length, 1);
1352+
expect(results.errors[0].message,
1353+
contains('FlutterApi method parameters must not be optional'));
1354+
});
13271355
}

0 commit comments

Comments
 (0)