Skip to content

Commit 0c6b3d1

Browse files
stereotype441commit-bot@chromium.org
authored andcommitted
Migration: do better function type formatting in DecoratedType.toString.
We now wrap named argument types in `{}` and optional positional argument types in `[]`. A few unit tests have been added to verify this functionality; I didn't exhaustively test the implementation of DecoratedType though. Change-Id: I1e3135adc3abc966f8a41d90c1c6e95e9c7c1eda Reviewed-on: https://dart-review.googlesource.com/c/sdk/+/107747 Commit-Queue: Paul Berry <[email protected]> Reviewed-by: Konstantin Shcheglov <[email protected]>
1 parent a76c459 commit 0c6b3d1

4 files changed

Lines changed: 119 additions & 5 deletions

File tree

‎pkg/nnbd_migration/lib/src/decorated_type.dart‎

Lines changed: 19 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -237,12 +237,26 @@ class DecoratedType {
237237
if (type.typeFormals.isNotEmpty) {
238238
formals = '<${type.typeFormals.join(', ')}>';
239239
}
240-
List<Object> argStrings =
241-
positionalParameters.map((p) => p.toString()).toList();
242-
for (var entry in namedParameters.entries) {
243-
argStrings.add('${entry.key}: ${entry.value}');
240+
List<String> paramStrings = [];
241+
for (int i = 0; i < positionalParameters.length; i++) {
242+
var prefix = '';
243+
if (i == type.normalParameterTypes.length) {
244+
prefix = '[';
245+
}
246+
paramStrings.add('$prefix${positionalParameters[i]}');
247+
}
248+
if (type.normalParameterTypes.length < positionalParameters.length) {
249+
paramStrings.last += ']';
250+
}
251+
if (namedParameters.isNotEmpty) {
252+
var prefix = '{';
253+
for (var entry in namedParameters.entries) {
254+
paramStrings.add('$prefix${entry.key}: ${entry.value}');
255+
prefix = '';
256+
}
257+
paramStrings.last += '}';
244258
}
245-
var args = argStrings.join(', ');
259+
var args = paramStrings.join(', ');
246260
return '$returnType Function$formals($args)$trailing';
247261
} else if (type is DynamicTypeImpl) {
248262
return 'dynamic';

‎pkg/nnbd_migration/lib/src/nullability_node.dart‎

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -403,6 +403,11 @@ abstract class NullabilityNode {
403403
void trackPossiblyOptional() {
404404
_isPossiblyOptional = true;
405405
}
406+
407+
@visibleForTesting
408+
static void clearDebugNames() {
409+
_debugNamesInUse.clear();
410+
}
406411
}
407412

408413
/// Derived class for nullability nodes that arise from the least-upper-bound
Lines changed: 93 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,93 @@
1+
// Copyright (c) 2019, the Dart project authors. Please see the AUTHORS file
2+
// for details. All rights reserved. Use of this source code is governed by a
3+
// BSD-style license that can be found in the LICENSE file.
4+
5+
import 'package:nnbd_migration/src/decorated_type.dart';
6+
import 'package:nnbd_migration/src/nullability_node.dart';
7+
import 'package:test/test.dart';
8+
import 'package:test_reflective_loader/test_reflective_loader.dart';
9+
10+
import 'abstract_single_unit.dart';
11+
12+
main() {
13+
defineReflectiveSuite(() {
14+
defineReflectiveTests(DecoratedTypeTest);
15+
});
16+
}
17+
18+
@reflectiveTest
19+
class DecoratedTypeTest extends AbstractSingleUnitTest {
20+
final _graph = NullabilityGraph();
21+
22+
NullabilityNode get always => _graph.always;
23+
24+
@override
25+
void setUp() {
26+
NullabilityNode.clearDebugNames();
27+
super.setUp();
28+
}
29+
30+
test_toString_named_parameter() async {
31+
await resolveTestUnit('''dynamic f({int x}) {}''');
32+
var type = findElement.function('f').type;
33+
var decoratedType = DecoratedType(type, always,
34+
namedParameters: {
35+
'x': DecoratedType(type.namedParameterTypes['x'], _node(1))
36+
},
37+
returnType: DecoratedType(type.returnType, always));
38+
expect(decoratedType.toString(), 'dynamic Function({x: int?(type(1))})?');
39+
}
40+
41+
test_toString_normal_and_named_parameter() async {
42+
await resolveTestUnit('''dynamic f(int x, {int y}) {}''');
43+
var type = findElement.function('f').type;
44+
var decoratedType = DecoratedType(type, always,
45+
positionalParameters: [
46+
DecoratedType(type.normalParameterTypes[0], _node(1))
47+
],
48+
namedParameters: {
49+
'y': DecoratedType(type.namedParameterTypes['y'], _node(2))
50+
},
51+
returnType: DecoratedType(type.returnType, always));
52+
expect(decoratedType.toString(),
53+
'dynamic Function(int?(type(1)), {y: int?(type(2))})?');
54+
}
55+
56+
test_toString_normal_and_optional_parameter() async {
57+
await resolveTestUnit('''dynamic f(int x, [int y]) {}''');
58+
var type = findElement.function('f').type;
59+
var decoratedType = DecoratedType(type, always,
60+
positionalParameters: [
61+
DecoratedType(type.normalParameterTypes[0], _node(1)),
62+
DecoratedType(type.optionalParameterTypes[0], _node(2))
63+
],
64+
returnType: DecoratedType(type.returnType, always));
65+
expect(decoratedType.toString(),
66+
'dynamic Function(int?(type(1)), [int?(type(2))])?');
67+
}
68+
69+
test_toString_normal_parameter() async {
70+
await resolveTestUnit('''dynamic f(int x) {}''');
71+
var type = findElement.function('f').type;
72+
var decoratedType = DecoratedType(type, always,
73+
positionalParameters: [
74+
DecoratedType(type.normalParameterTypes[0], _node(1))
75+
],
76+
returnType: DecoratedType(type.returnType, always));
77+
expect(decoratedType.toString(), 'dynamic Function(int?(type(1)))?');
78+
}
79+
80+
test_toString_optional_parameter() async {
81+
await resolveTestUnit('''dynamic f([int x]) {}''');
82+
var type = findElement.function('f').type;
83+
var decoratedType = DecoratedType(type, always,
84+
positionalParameters: [
85+
DecoratedType(type.optionalParameterTypes[0], _node(1))
86+
],
87+
returnType: DecoratedType(type.returnType, always));
88+
expect(decoratedType.toString(), 'dynamic Function([int?(type(1))])?');
89+
}
90+
91+
NullabilityNode _node(int offset) =>
92+
NullabilityNode.forTypeAnnotation(offset);
93+
}

‎pkg/nnbd_migration/test/test_all.dart‎

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ import 'package:test_reflective_loader/test_reflective_loader.dart';
66

77
import 'api_test.dart' as api_test;
88
import 'decorated_class_hierarchy_test.dart' as decorated_class_hierarchy_test;
9+
import 'decorated_type_test.dart' as decorated_type_test;
910
import 'edge_builder_test.dart' as edge_builder_test;
1011
import 'node_builder_test.dart' as node_builder_test;
1112
import 'nullability_node_test.dart' as nullability_node_test;
@@ -14,6 +15,7 @@ main() {
1415
defineReflectiveSuite(() {
1516
api_test.main();
1617
decorated_class_hierarchy_test.main();
18+
decorated_type_test.main();
1719
edge_builder_test.main();
1820
node_builder_test.main();
1921
nullability_node_test.main();

0 commit comments

Comments
 (0)