Correctly handle a trailing comma in convert_to_super_parameter

Change-Id: I8acfe80106666d9105f0c7a4c5e8c6352635e2bf
Reviewed-on: https://dart-review.googlesource.com/c/sdk/+/237601
Reviewed-by: Phil Quitslund <pquitslund@google.com>
Commit-Queue: Brian Wilkerson <brianwilkerson@google.com>
This commit is contained in:
Brian Wilkerson
2022-03-16 21:41:44 +00:00
committed by Commit Bot
parent f83b5c251d
commit 844ca214c4
2 changed files with 38 additions and 10 deletions
@@ -67,7 +67,8 @@ class ConvertToSuperParameters extends CorrectionProducer {
var parameterMap = _parameterMap(constructor.parameters);
List<_ParameterData>? positional = [];
var named = <_ParameterData>[];
var arguments = superInvocation.argumentList.arguments;
var argumentList = superInvocation.argumentList;
var arguments = argumentList.arguments;
for (var argumentIndex = 0;
argumentIndex < arguments.length;
argumentIndex++) {
@@ -156,18 +157,26 @@ class ConvertToSuperParameters extends CorrectionProducer {
}
// Remove the corresponding arguments.
if (argumentsToDelete.length == arguments.length &&
superInvocation.constructorName == null) {
var initializers = constructor.initializers;
SourceRange initializerRange;
if (initializers.length == 1) {
initializerRange =
range.endEnd(constructor.parameters, superInvocation);
if (argumentsToDelete.length == arguments.length) {
if (superInvocation.constructorName == null) {
// Delete the whole invocation.
var initializers = constructor.initializers;
SourceRange initializerRange;
if (initializers.length == 1) {
initializerRange =
range.endEnd(constructor.parameters, superInvocation);
} else {
initializerRange = range.nodeInList(initializers, superInvocation);
}
builder.addDeletion(initializerRange);
} else {
initializerRange = range.nodeInList(initializers, superInvocation);
// Leave the invocation, but remove all of the arguments, including
// any trailing comma.
builder.addDeletion(range.endStart(
argumentList.leftParenthesis, argumentList.rightParenthesis));
}
builder.addDeletion(initializerRange);
} else {
// Remove just the arguments that are no longer needed.
var ranges = range.nodesInList(arguments, argumentsToDelete);
for (var range in ranges) {
builder.addDeletion(range);
@@ -733,6 +733,25 @@ class A {
class B extends A {
B(super.x);
}
''');
}
Future<void> test_trailingComma() async {
await resolveTestCode('''
class A {
A._(int x, int y);
}
class B extends A {
B(int x, int y) : super._(x, y,);
}
''');
await assertHasAssistAt('B(', '''
class A {
A._(int x, int y);
}
class B extends A {
B(super.x, super.y) : super._();
}
''');
}
}