Skip to content

Commit e6df549

Browse files
jakemac53Commit Queue
authored andcommitted
support renamed parameters when adding super calls
Bug: #59745 Change-Id: I177e4f748954ddfea1b300085800c8c9a87bed3f Reviewed-on: https://dart-review.googlesource.com/c/sdk/+/408840 Auto-Submit: Jake Macdonald <[email protected]> Commit-Queue: Brian Wilkerson <[email protected]> Reviewed-by: Brian Wilkerson <[email protected]>
1 parent ddfd460 commit e6df549

File tree

2 files changed

+50
-15
lines changed

2 files changed

+50
-15
lines changed

pkg/analysis_server/lib/src/services/correction/dart/add_call_super.dart

Lines changed: 21 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -43,24 +43,30 @@ class AddCallSuper extends ResolvedCorrectionProducer {
4343
var nameObj = Name.forLibrary(classElement.library2, name);
4444
var overridden = InheritanceManager3().getInherited4(classElement, nameObj);
4545
if (overridden == null) return;
46-
var overriddenParameters = overridden.formalParameters.map((p) => p.name3);
46+
var overriddenNamedParameters = overridden.formalParameters
47+
.where((p) => p.isNamed)
48+
.map((p) => p.name3);
4749

4850
var body = methodDeclaration.body;
4951
var parameters = methodDeclaration.parameters?.parameters;
50-
var argumentList =
51-
parameters
52-
?.map((p) {
53-
var name = p.name?.lexeme;
54-
if (overriddenParameters.contains(name)) {
55-
return p.isNamed ? '$name: $name' : name;
56-
}
57-
return null;
58-
})
59-
.nonNulls
60-
.join(', ') ??
61-
'';
62-
63-
_addition = '$name($argumentList)';
52+
var arguments = <String>[];
53+
if (parameters != null) {
54+
for (var i = 0; i < parameters.length; i++) {
55+
var p = parameters[i];
56+
var name = p.name?.lexeme;
57+
if (name == null) continue;
58+
if (p.isPositional) {
59+
if (i < overridden.formalParameters.length &&
60+
overridden.formalParameters[i].isPositional) {
61+
arguments.add(name);
62+
}
63+
} else if (overriddenNamedParameters.contains(name)) {
64+
arguments.add(p.isNamed ? '$name: $name' : name);
65+
}
66+
}
67+
}
68+
69+
_addition = '$name(${arguments.join(', ')})';
6470

6571
if (body is BlockFunctionBody) {
6672
await _block(builder, body.block);

pkg/analysis_server/test/src/services/correction/fix/add_call_super_test.dart

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -177,6 +177,35 @@ class B extends A {
177177
''', matchFixMessage: "Add 'super.a(i)'");
178178
}
179179

180+
Future<void> test_body_renamed_parameters() async {
181+
await resolveTestCode('''
182+
import 'package:meta/meta.dart';
183+
184+
class A {
185+
@mustCallSuper
186+
void m(int x, bool y) {}
187+
}
188+
class B extends A {
189+
@override
190+
void m(int x, bool z) {}
191+
}
192+
''');
193+
await assertHasFix('''
194+
import 'package:meta/meta.dart';
195+
196+
class A {
197+
@mustCallSuper
198+
void m(int x, bool y) {}
199+
}
200+
class B extends A {
201+
@override
202+
void m(int x, bool z) {
203+
super.m(x, z);
204+
}
205+
}
206+
''', matchFixMessage: "Add 'super.m(x, z)'");
207+
}
208+
180209
Future<void> test_body_required() async {
181210
await resolveTestCode('''
182211
import 'package:meta/meta.dart';

0 commit comments

Comments
 (0)