Skip to content

Commit bc9a7c9

Browse files
fshcheglovCommit Queue
authored andcommitted
Fix convert_to_initializing_format not working properly with required parameters when there is no type presevation.
Change-Id: I184455288d50d8738c868efefc1b98446eaf5c1b Reviewed-on: https://dart-review.googlesource.com/c/sdk/+/438321 Commit-Queue: Konstantin Shcheglov <[email protected]> Reviewed-by: Samuel Rawlins <[email protected]> Reviewed-by: Konstantin Shcheglov <[email protected]>
1 parent 6dee5e4 commit bc9a7c9

File tree

2 files changed

+17
-1
lines changed

2 files changed

+17
-1
lines changed

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

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -92,9 +92,10 @@ class ConvertToInitializingFormal extends ResolvedCorrectionProducer {
9292
if (preserveType) {
9393
builder.addSimpleInsertion(identifier.offset, 'this.');
9494
} else {
95+
var prefix = parameter.requiredKeyword != null ? 'required ' : '';
9596
builder.addSimpleReplacement(
9697
range.node(parameter),
97-
'this.${identifier.lexeme}',
98+
'${prefix}this.${identifier.lexeme}',
9899
);
99100
}
100101

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

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -175,6 +175,21 @@ class C {
175175
''');
176176
}
177177

178+
Future<void> test_initializer_parameterRequired() async {
179+
await resolveTestCode('''
180+
class C {
181+
final int foo;
182+
C({required int foo}) : foo = foo;
183+
}
184+
''');
185+
await assertHasFix('''
186+
class C {
187+
final int foo;
188+
C({required this.foo});
189+
}
190+
''');
191+
}
192+
178193
Future<void> test_initializer_positional() async {
179194
await resolveTestCode('''
180195
class C {

0 commit comments

Comments
 (0)