Skip to content

Commit a023c3f

Browse files
keertipCommit Queue
authored andcommitted
Fix bug with change parameter type when there are no parameters in argument list
Change-Id: I5cabd16091cb66be45f107a3701128776e8129ed Reviewed-on: https://dart-review.googlesource.com/c/sdk/+/310820 Commit-Queue: Keerti Parthasarathy <[email protected]> Reviewed-by: Brian Wilkerson <[email protected]>
1 parent 6fec16d commit a023c3f

File tree

3 files changed

+57
-5
lines changed

3 files changed

+57
-5
lines changed

pkg/analysis_server/lib/src/services/correction/fix/data_driven/modify_parameters.dart

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -126,7 +126,9 @@ class ModifyParameters extends Change<_Data> {
126126
// that the parameter was absent.
127127
var index = reference is PositionalFormalParameterReference
128128
? reference.index
129-
: remainingArguments.last + 1;
129+
: remainingArguments.isNotEmpty
130+
? remainingArguments.last + 1
131+
: 0;
130132
remainingArguments.add(index);
131133
indexToNewArgumentMap[index] = modification;
132134
argumentsToInsert.add(index);
@@ -229,11 +231,13 @@ class ModifyParameters extends Change<_Data> {
229231
offset = arguments[remainingIndex - 1].end;
230232
needsInitialComma = true;
231233
} else {
232-
offset = arguments[remainingIndex].offset;
234+
offset = arguments.isNotEmpty
235+
? arguments[remainingIndex].offset
236+
: argumentList.leftParenthesis.end;
233237
}
234238
builder.addInsertion(offset, (builder) {
235239
writeInsertionRange(builder, insertionRange, needsInitialComma);
236-
if (insertionIndex == 0) {
240+
if (insertionIndex == 0 && arguments.isNotEmpty) {
237241
builder.write(', ');
238242
}
239243
});

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

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -855,6 +855,9 @@ class FixProcessor extends BaseProcessor {
855855
CompileTimeErrorCode.INVALID_OVERRIDE_SETTER: [
856856
DataDriven.new,
857857
],
858+
CompileTimeErrorCode.MISSING_REQUIRED_ARGUMENT: [
859+
DataDriven.new,
860+
],
858861
CompileTimeErrorCode.MIXIN_OF_NON_CLASS: [
859862
DataDriven.new,
860863
ImportLibrary.forType,

pkg/analysis_server/test/src/services/correction/fix/data_driven/flutter_use_case_test.dart

Lines changed: 47 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2321,6 +2321,51 @@ void f() {
23212321
Future<void>
23222322
test_services_ClipboardData_changeParameterNonNullAbsent() async {
23232323
setPackageContent('''
2324+
class ClipboardData {
2325+
const ClipboardData({required String this.text});
2326+
2327+
final String? text;
2328+
}
2329+
''');
2330+
2331+
addPackageDataFile('''
2332+
version: 1
2333+
transforms:
2334+
- title: "Migrate to empty 'text' string"
2335+
date: 2023-04-19
2336+
element:
2337+
uris: ['$importUri']
2338+
constructor: ''
2339+
inClass: 'ClipboardData'
2340+
changes:
2341+
- kind: 'changeParameterType'
2342+
name: 'text'
2343+
nullability: non_null
2344+
argumentValue:
2345+
expression: "''"
2346+
''');
2347+
2348+
await resolveTestCode('''
2349+
import '$importUri';
2350+
2351+
void f() {
2352+
var c = ClipboardData();
2353+
print(c);
2354+
}
2355+
''');
2356+
await assertHasFix('''
2357+
import '$importUri';
2358+
2359+
void f() {
2360+
var c = ClipboardData(text: '');
2361+
print(c);
2362+
}
2363+
''');
2364+
}
2365+
2366+
Future<void>
2367+
test_services_ClipboardData_changeParameterNonNullAdditional() async {
2368+
setPackageContent('''
23242369
class ClipboardData {
23252370
const ClipboardData({required String this.text, String? this.p});
23262371
@@ -2350,15 +2395,15 @@ transforms:
23502395
import '$importUri';
23512396
23522397
void f() {
2353-
var c = ClipboardData(p: 'hello', text: null);
2398+
var c = ClipboardData(text: null, p: 'hello');
23542399
print(c);
23552400
}
23562401
''');
23572402
await assertHasFix('''
23582403
import '$importUri';
23592404
23602405
void f() {
2361-
var c = ClipboardData(p: 'hello', text: '');
2406+
var c = ClipboardData(text: '', p: 'hello');
23622407
print(c);
23632408
}
23642409
''');

0 commit comments

Comments
 (0)