Skip to content

Commit e776c1f

Browse files
kallentuCommit Queue
authored andcommitted
[analysis_server] Dot shorthands: Update AddConst fix to add const to dot shorthand constructor invocations.
The `AddConst` fix is triggered by the `PreferConstConstructors` lint and this CL updates the fix to add `const` to dot shorthand constructor invocations that can be made constant. Fixes: #61163 Bug: #60994 Change-Id: I4520c1edf807c041f38f5477f48ab1b1327732b5 Reviewed-on: https://dart-review.googlesource.com/c/sdk/+/441720 Reviewed-by: Brian Wilkerson <[email protected]> Commit-Queue: Kallen Tu <[email protected]>
1 parent ed55275 commit e776c1f

File tree

2 files changed

+49
-2
lines changed

2 files changed

+49
-2
lines changed

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

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -95,7 +95,12 @@ class AddConst extends ResolvedCorrectionProducer {
9595
await _insertBeforeNode(builder, targetNode);
9696
return;
9797
}
98-
if (targetNode case InstanceCreationExpression(:var parent, :var keyword)) {
98+
if (targetNode
99+
case InstanceCreationExpression(:var parent, :var keyword) ||
100+
DotShorthandConstructorInvocation(
101+
:var parent,
102+
constKeyword: var keyword,
103+
)) {
99104
var constDeclarations =
100105
getCodeStyleOptions(unitResult.file).preferConstDeclarations;
101106

@@ -110,7 +115,7 @@ class AddConst extends ResolvedCorrectionProducer {
110115
return;
111116
}
112117
}
113-
if (keyword == null) {
118+
if (keyword == null && targetNode is Expression) {
114119
await _insertBeforeNode(builder, targetNode);
115120
return;
116121
}

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

Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -626,6 +626,48 @@ class AddConst_PreferConstConstructorsTest extends FixProcessorLintTest {
626626
@override
627627
String get lintCode => LintNames.prefer_const_constructors;
628628

629+
Future<void> test_dotShorthand_named() async {
630+
await resolveTestCode('''
631+
class C {
632+
const C.named();
633+
}
634+
void f() {
635+
C c = .named();
636+
print(c);
637+
}
638+
''');
639+
await assertHasFix('''
640+
class C {
641+
const C.named();
642+
}
643+
void f() {
644+
C c = const .named();
645+
print(c);
646+
}
647+
''');
648+
}
649+
650+
Future<void> test_dotShorthand_unnamed() async {
651+
await resolveTestCode('''
652+
class C {
653+
const C();
654+
}
655+
void f() {
656+
C c = .new();
657+
print(c);
658+
}
659+
''');
660+
await assertHasFix('''
661+
class C {
662+
const C();
663+
}
664+
void f() {
665+
C c = const .new();
666+
print(c);
667+
}
668+
''');
669+
}
670+
629671
Future<void> test_final_variable() async {
630672
createAnalysisOptionsFile(
631673
lints: [

0 commit comments

Comments
 (0)