Skip to content

Commit fe35362

Browse files
scheglovCommit Queue
authored andcommitted
[CMSR] Support for removing formal parameters.
Change-Id: Id04f6c7854dec3d5059009a0a579110690d3eb62 Reviewed-on: https://dart-review.googlesource.com/c/sdk/+/310963 Commit-Queue: Konstantin Shcheglov <[email protected]> Reviewed-by: Samuel Rawlins <[email protected]>
1 parent 915c1ed commit fe35362

File tree

3 files changed

+233
-9
lines changed

3 files changed

+233
-9
lines changed

pkg/analysis_server/lib/src/services/refactoring/agnostic/change_method_signature.dart

Lines changed: 17 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -171,10 +171,14 @@ class MethodSignatureUpdate {
171171
/// class hierarchy, will be updated. The new formal parameters will be
172172
/// written in the order [formalParameters] field, with new kinds.
173173
///
174-
/// TODO(scheglov) Test removing.
175174
/// TODO(scheglov) Consider adding.
176175
final List<FormalParameterUpdate> formalParameters;
177176

177+
/// Normally, after writing formal parameters in the order specified by
178+
/// [formalParameters], we write any remaining named formal parameters.
179+
/// So, here we record names that are explicitly removed.
180+
final Set<String> removedNamedFormalParameters;
181+
178182
/// Specifies whether to add the trailing comma after formal parameters.
179183
///
180184
/// TODO(scheglov) Test for empty formal parameter lists.
@@ -185,6 +189,7 @@ class MethodSignatureUpdate {
185189

186190
MethodSignatureUpdate({
187191
required this.formalParameters,
192+
this.removedNamedFormalParameters = const {},
188193
required this.formalParametersTrailingComma,
189194
required this.argumentsTrailingComma,
190195
});
@@ -684,6 +689,8 @@ class _SignatureUpdater {
684689
);
685690
}
686691
}).toList(),
692+
removedNamedFormalParameters:
693+
signatureUpdate.removedNamedFormalParameters,
687694
trailingComma: signatureUpdate.argumentsTrailingComma,
688695
resolvedUnit: resolvedUnit,
689696
argumentList: argumentList,
@@ -778,8 +785,8 @@ class _SignatureUpdater {
778785
return ChangeStatusFailure();
779786
}
780787
} else {
781-
existing =
782-
existingFormalParameters.named.remove(formalParameterState.name);
788+
final name = formalParameterState.name;
789+
existing = existingFormalParameters.named.remove(name);
783790
if (existing == null) {
784791
continue;
785792
}
@@ -808,10 +815,14 @@ class _SignatureUpdater {
808815
}
809816

810817
// Add back remaining named formal parameters.
811-
for (final existing in existingFormalParameters.named.values) {
812-
final text = utils.getNodeText(existing);
818+
final removedNamed = signatureUpdate.removedNamedFormalParameters;
819+
existingFormalParameters.named.forEach((name, node) {
820+
if (removedNamed.contains(name)) {
821+
return;
822+
}
823+
final text = utils.getNodeText(node);
813824
namedWrites.add(text);
814-
}
825+
});
815826

816827
await builder.addDartFileEdit(path, (builder) {
817828
builder.addReplacement(

pkg/analysis_server/lib/src/services/refactoring/framework/write_invocation_arguments.dart

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@ import 'package:collection/collection.dart';
1515
/// by the formal parameter updates, reordering, changing kind, etc.
1616
Future<WriteArgumentsStatus> writeArguments({
1717
required List<FormalParameterUpdate> formalParameterUpdates,
18+
required Set<String> removedNamedFormalParameters,
1819
required ArgumentsTrailingComma trailingComma,
1920
required ResolvedUnitResult resolvedUnit,
2021
required ArgumentList argumentList,
@@ -31,7 +32,8 @@ Future<WriteArgumentsStatus> writeArguments({
3132
case FormalParameterUpdateExisting(:final reference):
3233
switch (reference) {
3334
case NamedFormalParameterReference():
34-
final argument = namedArguments.remove(reference.name);
35+
final name = reference.name;
36+
final argument = namedArguments.remove(name);
3537
if (argument == null) {
3638
continue;
3739
}
@@ -86,13 +88,16 @@ Future<WriteArgumentsStatus> writeArguments({
8688
}
8789

8890
// Add remaining named arguments.
89-
for (final argument in namedArguments.values) {
91+
namedArguments.forEach((name, argument) {
92+
if (removedNamedFormalParameters.contains(name)) {
93+
return;
94+
}
9095
newArguments.add(
9196
_ArgumentAsIs(
9297
argument: argument,
9398
),
9499
);
95-
}
100+
});
96101

97102
await builder.addDartFileEdit(resolvedUnit.path, (builder) {
98103
builder.addReplacement(range.node(argumentList), (builder) {

pkg/analysis_server/test/services/refactoring/agnostic/change_method_signature_test.dart

Lines changed: 208 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1393,6 +1393,69 @@ void f(A a, B b) {
13931393
''');
13941394
}
13951395

1396+
Future<void> test_classMethod_requiredNamed_remove() async {
1397+
await _analyzeValidSelection(r'''
1398+
class A {
1399+
void ^test({
1400+
required int a,
1401+
required int b,
1402+
required int c,
1403+
}) {}
1404+
}
1405+
1406+
class B extends A {
1407+
void test({
1408+
required int a,
1409+
required int b,
1410+
required int c,
1411+
}) {}
1412+
}
1413+
1414+
void f(A a, B b) {
1415+
a.test(a: 0, b: 1, c: 2);
1416+
b.test(a: 3, b: 4, c: 5);
1417+
}
1418+
''');
1419+
1420+
final signatureUpdate = MethodSignatureUpdate(
1421+
formalParameters: [
1422+
FormalParameterUpdate(
1423+
id: 0,
1424+
kind: FormalParameterKind.requiredNamed,
1425+
),
1426+
FormalParameterUpdate(
1427+
id: 2,
1428+
kind: FormalParameterKind.requiredNamed,
1429+
),
1430+
],
1431+
removedNamedFormalParameters: {'b'},
1432+
formalParametersTrailingComma: TrailingComma.ifPresent,
1433+
argumentsTrailingComma: ArgumentsTrailingComma.ifPresent,
1434+
);
1435+
1436+
await _assertUpdate(signatureUpdate, r'''
1437+
>>>>>>> /home/test/lib/test.dart
1438+
class A {
1439+
void test({
1440+
required int a,
1441+
required int c,
1442+
}) {}
1443+
}
1444+
1445+
class B extends A {
1446+
void test({
1447+
required int a,
1448+
required int c,
1449+
}) {}
1450+
}
1451+
1452+
void f(A a, B b) {
1453+
a.test(a: 0, c: 2);
1454+
b.test(a: 3, c: 5);
1455+
}
1456+
''');
1457+
}
1458+
13961459
Future<void> test_classMethod_requiredPositional_reorder() async {
13971460
await _analyzeValidSelection(r'''
13981461
class A {
@@ -1769,6 +1832,41 @@ void f() {
17691832
''');
17701833
}
17711834

1835+
Future<void> test_topFunction_optionalNamed_remove() async {
1836+
await _analyzeValidSelection(r'''
1837+
void ^test({int a, int b, int c}) {}
1838+
1839+
void f() {
1840+
test(a: 0, b: 1, c: 2);
1841+
}
1842+
''');
1843+
1844+
final signatureUpdate = MethodSignatureUpdate(
1845+
formalParameters: [
1846+
FormalParameterUpdate(
1847+
id: 0,
1848+
kind: FormalParameterKind.optionalNamed,
1849+
),
1850+
FormalParameterUpdate(
1851+
id: 2,
1852+
kind: FormalParameterKind.optionalNamed,
1853+
),
1854+
],
1855+
removedNamedFormalParameters: {'b'},
1856+
formalParametersTrailingComma: TrailingComma.ifPresent,
1857+
argumentsTrailingComma: ArgumentsTrailingComma.ifPresent,
1858+
);
1859+
1860+
await _assertUpdate(signatureUpdate, r'''
1861+
>>>>>>> /home/test/lib/test.dart
1862+
void test({int a, int c}) {}
1863+
1864+
void f() {
1865+
test(a: 0, c: 2);
1866+
}
1867+
''');
1868+
}
1869+
17721870
Future<void> test_topFunction_optionalNamed_reorder() async {
17731871
await _analyzeValidSelection(r'''
17741872
void ^test({
@@ -1994,6 +2092,40 @@ void f() {
19942092
''');
19952093
}
19962094

2095+
Future<void> test_topFunction_optionalPositional_remove() async {
2096+
await _analyzeValidSelection(r'''
2097+
void ^test([int? a, int? b, int? c]) {}
2098+
2099+
void f() {
2100+
test(0, 1, 2);
2101+
}
2102+
''');
2103+
2104+
final signatureUpdate = MethodSignatureUpdate(
2105+
formalParameters: [
2106+
FormalParameterUpdate(
2107+
id: 0,
2108+
kind: FormalParameterKind.optionalPositional,
2109+
),
2110+
FormalParameterUpdate(
2111+
id: 2,
2112+
kind: FormalParameterKind.optionalPositional,
2113+
),
2114+
],
2115+
formalParametersTrailingComma: TrailingComma.ifPresent,
2116+
argumentsTrailingComma: ArgumentsTrailingComma.ifPresent,
2117+
);
2118+
2119+
await _assertUpdate(signatureUpdate, r'''
2120+
>>>>>>> /home/test/lib/test.dart
2121+
void test([int? a, int? c]) {}
2122+
2123+
void f() {
2124+
test(0, 2);
2125+
}
2126+
''');
2127+
}
2128+
19972129
Future<void> test_topFunction_optionalPositional_reorder() async {
19982130
await _analyzeValidSelection(r'''
19992131
void ^test([int a, double b]) {}
@@ -2193,6 +2325,48 @@ ChangeStatusFailure
21932325
''');
21942326
}
21952327

2328+
Future<void> test_topFunction_requiredNamed_remove() async {
2329+
await _analyzeValidSelection(r'''
2330+
void ^test({
2331+
required int a,
2332+
required int b,
2333+
required int c,
2334+
}) {}
2335+
2336+
void f() {
2337+
test(a: 0, b: 1, c: 2);
2338+
}
2339+
''');
2340+
2341+
final signatureUpdate = MethodSignatureUpdate(
2342+
formalParameters: [
2343+
FormalParameterUpdate(
2344+
id: 0,
2345+
kind: FormalParameterKind.requiredNamed,
2346+
),
2347+
FormalParameterUpdate(
2348+
id: 2,
2349+
kind: FormalParameterKind.requiredNamed,
2350+
),
2351+
],
2352+
removedNamedFormalParameters: {'b'},
2353+
formalParametersTrailingComma: TrailingComma.ifPresent,
2354+
argumentsTrailingComma: ArgumentsTrailingComma.ifPresent,
2355+
);
2356+
2357+
await _assertUpdate(signatureUpdate, r'''
2358+
>>>>>>> /home/test/lib/test.dart
2359+
void test({
2360+
required int a,
2361+
required int c,
2362+
}) {}
2363+
2364+
void f() {
2365+
test(a: 0, c: 2);
2366+
}
2367+
''');
2368+
}
2369+
21962370
Future<void> test_topFunction_requiredNamed_reorder() async {
21972371
await _analyzeValidSelection(r'''
21982372
void ^test({
@@ -2387,6 +2561,40 @@ void f() {
23872561
''');
23882562
}
23892563

2564+
Future<void> test_topFunction_requiredPositional_remove() async {
2565+
await _analyzeValidSelection(r'''
2566+
void ^test(int a, int b, int c) {}
2567+
2568+
void f() {
2569+
test(0, 1, 2);
2570+
}
2571+
''');
2572+
2573+
final signatureUpdate = MethodSignatureUpdate(
2574+
formalParameters: [
2575+
FormalParameterUpdate(
2576+
id: 0,
2577+
kind: FormalParameterKind.requiredPositional,
2578+
),
2579+
FormalParameterUpdate(
2580+
id: 2,
2581+
kind: FormalParameterKind.requiredPositional,
2582+
),
2583+
],
2584+
formalParametersTrailingComma: TrailingComma.ifPresent,
2585+
argumentsTrailingComma: ArgumentsTrailingComma.ifPresent,
2586+
);
2587+
2588+
await _assertUpdate(signatureUpdate, r'''
2589+
>>>>>>> /home/test/lib/test.dart
2590+
void test(int a, int c) {}
2591+
2592+
void f() {
2593+
test(0, 2);
2594+
}
2595+
''');
2596+
}
2597+
23902598
Future<void> test_topFunction_requiredPositional_reorder() async {
23912599
await _analyzeValidSelection(r'''
23922600
void ^test(int a, double b) {}

0 commit comments

Comments
 (0)