Skip to content

Commit

Permalink
Support optional and named args in Function types.
Browse files Browse the repository at this point in the history
  • Loading branch information
davidmorgan committed Aug 3, 2023
1 parent da81728 commit d13df6d
Show file tree
Hide file tree
Showing 5 changed files with 355 additions and 6 deletions.
6 changes: 6 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,11 @@
# Changelog

# 8.6.2

- Fix generation for fields that are `Function` types that are declared
separately, for example in a `mixin` defined in another source file, and use
named or positional parameters.

# 8.6.1

- Fix support for generating enum mixins for Dart 3. Instead of triggering
Expand Down
29 changes: 25 additions & 4 deletions built_value_generator/lib/src/dart_types.dart
Original file line number Diff line number Diff line change
Expand Up @@ -69,10 +69,31 @@ class DartTypes {
} else if (dartType is DynamicType) {
return 'dynamic';
} else if (dartType is FunctionType) {
return getName(dartType.returnType) +
' Function(' +
dartType.parameters.map((p) => getName(p.type)).join(', ') +
')$suffix';
final parameters = StringBuffer();

parameters.write(
dartType.normalParameterTypes.map((t) => getName(t)).join(', '));

if (dartType.optionalParameterTypes.isNotEmpty) {
if (parameters.isNotEmpty) parameters.write(', ');
parameters.write('[');
parameters.write(
dartType.optionalParameterTypes.map((t) => getName(t)).join(', '));
parameters.write(']');
}

if (dartType.namedParameterTypes.isNotEmpty) {
if (parameters.isNotEmpty) parameters.write(', ');
parameters.write('{');
parameters.write(dartType.parameters
.where((p) => p.isOptionalNamed || p.isRequiredNamed)
.map((p) => '${p.isRequiredNamed ? 'required ' : ''}'
'${getName(p.type)} ${p.name}')
.join(', '));
parameters.write('}');
}

return getName(dartType.returnType) + ' Function($parameters)$suffix';
} else if (dartType is InterfaceType) {
var typeArguments = dartType.typeArguments;
if (typeArguments.isEmpty) {
Expand Down
10 changes: 10 additions & 0 deletions end_to_end_test/lib/mixins_src.dart
Original file line number Diff line number Diff line change
Expand Up @@ -16,3 +16,13 @@ abstract class Mixin {

// Don't fail on encountering new mixin syntax.
mixin Foo {}

mixin FunctionMixin {
Function get mixinBareFunction;
Future<void> Function(int, double) get mixinPositionalFunction;
Future<void> Function(int, [double]) get mixinOptionalFunction;
Future<void> Function(int x, double y) get mixinPositionalNamedFunction;
Future<void> Function(int x, {int y, double z}) get mixinNamedFunction;
Future<void> Function(int x, {required int y, required double z})
get mixinRequiredNamedFunction;
}
25 changes: 24 additions & 1 deletion end_to_end_test/lib/values.dart
Original file line number Diff line number Diff line change
@@ -1,14 +1,16 @@
// Copyright (c) 2020, Google Inc. Please see the AUTHORS file for details.
// All rights reserved. Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE file.
// @dart=2.12

import 'package:built_collection/built_collection.dart';
import 'package:built_value/built_value.dart';
import 'package:built_value/serializer.dart';
import 'package:end_to_end_test/enums.dart' as using_import_as;
import 'package:end_to_end_test/mixins_src.dart';
import 'package:fixnum/fixnum.dart';

import 'old.dart';

part 'values.g.dart';

abstract class SimpleValue implements Built<SimpleValue, SimpleValueBuilder> {
Expand Down Expand Up @@ -931,3 +933,24 @@ abstract class ValueWithAwkwardNestedBuilderBuilder
_$ValueWithAwkwardNestedBuilderBuilder;
ValueWithAwkwardNestedBuilderBuilder._();
}

abstract class VariousFunctionsValue
// Functions declared in a different file are rendered via DartType instead
// of using the AST, so check those too.
with
FunctionMixin
implements
Built<VariousFunctionsValue, VariousFunctionsValueBuilder> {
Function get bareFunction;
Future<void> Function(int, double) get positionalFunction;
Future<void> Function(int, [double]) get optionalFunction;
Future<void> Function(int x, double y) get positionalNamedFunction;
Future<void> Function(int x, {int y, double z}) get namedFunction;
Future<void> Function(int x, {required int y, required double z})
get requiredNamedFunction;

factory VariousFunctionsValue(
[void Function(VariousFunctionsValueBuilder) updates]) =
_$VariousFunctionsValue;
VariousFunctionsValue._();
}
Loading

0 comments on commit d13df6d

Please sign in to comment.