Skip to content

Commit 289278b

Browse files
kallentuCommit Queue
authored andcommitted
[analysis_server] Dot shorthands: Update ReplaceFinalWithConst fix.
Adds an extra dot shorthand constructor invocation case to the `ReplaceFinalWithConst` fix. Added relevant tests. Bug: #60994 Change-Id: I23d9882ae5b8633a29f6d50a40de35d80edb2a2f Reviewed-on: https://dart-review.googlesource.com/c/sdk/+/442161 Reviewed-by: Brian Wilkerson <[email protected]> Commit-Queue: Kallen Tu <[email protected]>
1 parent 3303e1d commit 289278b

File tree

2 files changed

+51
-2
lines changed

2 files changed

+51
-2
lines changed

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

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -38,8 +38,12 @@ class ReplaceFinalWithConst extends ResolvedCorrectionProducer {
3838
var initializer = variable.initializer;
3939
if (initializer != null) {
4040
Token? constToken;
41-
if (initializer is InstanceCreationExpression) {
42-
constToken = initializer.keyword;
41+
if (initializer
42+
case InstanceCreationExpression(:var keyword) ||
43+
DotShorthandConstructorInvocation(
44+
constKeyword: Token? keyword,
45+
)) {
46+
constToken = keyword;
4347
} else if (initializer is TypedLiteral) {
4448
constToken = initializer.constKeyword;
4549
}

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

Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,51 @@ class ReplaceFinalWithConstTest extends FixProcessorLintTest {
4141
@override
4242
String get lintCode => LintNames.prefer_const_declarations;
4343

44+
Future<void> test_const_dotShorthand_multiple() async {
45+
await resolveTestCode('''
46+
class A {
47+
const A.named();
48+
}
49+
final A a1 = const .named(), a2 = const .named();
50+
''');
51+
await assertHasFix('''
52+
class A {
53+
const A.named();
54+
}
55+
const A a1 = .named(), a2 = .named();
56+
''');
57+
}
58+
59+
Future<void> test_const_dotShorthand_named() async {
60+
await resolveTestCode('''
61+
class A {
62+
const A.named();
63+
}
64+
final A a = const .named();
65+
''');
66+
await assertHasFix('''
67+
class A {
68+
const A.named();
69+
}
70+
const A a = .named();
71+
''');
72+
}
73+
74+
Future<void> test_const_dotShorthand_unnamed() async {
75+
await resolveTestCode('''
76+
class A {
77+
const A();
78+
}
79+
final A a = const .new();
80+
''');
81+
await assertHasFix('''
82+
class A {
83+
const A();
84+
}
85+
const A a = .new();
86+
''');
87+
}
88+
4489
Future<void> test_const_instanceCreation() async {
4590
await resolveTestCode('''
4691
class A {

0 commit comments

Comments
 (0)