Skip to content

Commit b1af1bd

Browse files
kallentuCommit Queue
authored andcommitted
[analysis_server] Dot shorthands: Update RemoveConst fix.
Updates `RemoveConst` fix to handle dot shorthands. Will remove `const` where it can't be constant and then push the `const` into a dot shorthand's arguments if possible. Bug: #60994 Change-Id: Idb7ef4e5006c6561ce621a48e8351fd65b27c5b3 Reviewed-on: https://dart-review.googlesource.com/c/sdk/+/441863 Reviewed-by: Brian Wilkerson <[email protected]> Reviewed-by: Chloe Stefantsova <[email protected]> Commit-Queue: Kallen Tu <[email protected]>
1 parent e4e36a3 commit b1af1bd

File tree

3 files changed

+81
-16
lines changed

3 files changed

+81
-16
lines changed

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

Lines changed: 30 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -75,6 +75,17 @@ class _PushConstVisitor extends GeneralizingAstVisitor<void> {
7575

7676
_PushConstVisitor(this.builder, this.excluded);
7777

78+
@override
79+
void visitDotShorthandConstructorInvocation(
80+
DotShorthandConstructorInvocation node,
81+
) {
82+
_pushConstInConstructorInvocation(
83+
node,
84+
node.argumentList,
85+
node.constKeyword,
86+
);
87+
}
88+
7889
@override
7990
void visitIfElement(IfElement node) {
8091
node.thenElement.accept(this);
@@ -83,15 +94,7 @@ class _PushConstVisitor extends GeneralizingAstVisitor<void> {
8394

8495
@override
8596
void visitInstanceCreationExpression(InstanceCreationExpression node) {
86-
if (_containsExcluded(node)) {
87-
node.argumentList.visitChildren(this);
88-
} else if (excluded.any(node.contains)) {
89-
// Don't speculate whether arguments can be const.
90-
} else {
91-
if (node.keyword == null) {
92-
builder.addSimpleInsertion(node.offset, '${Keyword.CONST.lexeme} ');
93-
}
94-
}
97+
_pushConstInConstructorInvocation(node, node.argumentList, node.keyword);
9598
}
9699

97100
@override
@@ -128,6 +131,20 @@ class _PushConstVisitor extends GeneralizingAstVisitor<void> {
128131
bool _containsExcluded(AstNode node) {
129132
return {for (var e in excluded) ...e.withAncestors}.contains(node);
130133
}
134+
135+
void _pushConstInConstructorInvocation(
136+
AstNode node,
137+
ArgumentList argumentList,
138+
Token? constKeyword,
139+
) {
140+
if (_containsExcluded(node)) {
141+
argumentList.visitChildren(this);
142+
} else if (excluded.any(node.contains)) {
143+
// Don't speculate whether arguments can be const.
144+
} else if (constKeyword == null) {
145+
builder.addSimpleInsertion(node.offset, '${Keyword.CONST.lexeme} ');
146+
}
147+
}
131148
}
132149

133150
abstract class _RemoveConst extends ParsedCorrectionProducer {
@@ -175,16 +192,13 @@ abstract class _RemoveConst extends ParsedCorrectionProducer {
175192
if (_codesWhereThisIsValid.contains(e.diagnosticCode)) e,
176193
];
177194
switch (node) {
178-
case InstanceCreationExpression contextNode:
179-
var (:constNodes, :nodesWithDiagnostic) = contextNode
180-
.argumentList
181-
.arguments
195+
case InstanceCreationExpression(:var argumentList) ||
196+
DotShorthandConstructorInvocation(:var argumentList):
197+
var (:constNodes, :nodesWithDiagnostic) = argumentList.arguments
182198
.withDiagnosticCodeIn(validDiagnostics);
183199
await builder.addDartFileEdit(file, (builder) {
184200
_deleteToken(builder, constKeyword);
185-
contextNode.accept(
186-
_PushConstVisitor(builder, nodesWithDiagnostic),
187-
);
201+
node.accept(_PushConstVisitor(builder, nodesWithDiagnostic));
188202
});
189203
case TypedLiteral contextNode:
190204
var (:constNodes, :nodesWithDiagnostic) = switch (contextNode) {

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

Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -624,6 +624,21 @@ var x = A([0]);
624624
''');
625625
}
626626

627+
Future<void> test_explicitConst_dotShorthand() async {
628+
await resolveTestCode('''
629+
class A {
630+
A(_);
631+
}
632+
A x = const .new([0]);
633+
''');
634+
await assertHasFix('''
635+
class A {
636+
A(_);
637+
}
638+
A x = .new([0]);
639+
''');
640+
}
641+
627642
Future<void> test_implicitConst_instanceCreation_argument() async {
628643
await resolveTestCode('''
629644
class A {}
@@ -645,6 +660,38 @@ var x = B(A(), const [0]);
645660
''');
646661
}
647662

663+
Future<void>
664+
test_implicitConst_instanceCreation_argument_dotShorthand() async {
665+
await resolveTestCode('''
666+
enum E { a }
667+
668+
class A {
669+
A.named();
670+
const A.constNamed();
671+
}
672+
673+
class B {
674+
const B(A a, A aa, E e);
675+
}
676+
677+
B x = const .new(.named(), .constNamed(), .a);
678+
''');
679+
await assertHasFix('''
680+
enum E { a }
681+
682+
class A {
683+
A.named();
684+
const A.constNamed();
685+
}
686+
687+
class B {
688+
const B(A a, A aa, E e);
689+
}
690+
691+
B x = .new(.named(), const .constNamed(), .a);
692+
''');
693+
}
694+
648695
Future<void> test_implicitConst_instanceCreation_argument_named() async {
649696
await resolveTestCode('''
650697
class A {}

pkg/analyzer/lib/src/dart/ast/ast.dart

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8287,6 +8287,10 @@ sealed class ExpressionImpl extends AstNodeImpl
82878287
return (current, constKeyword);
82888288
}
82898289
return null;
8290+
case DotShorthandConstructorInvocation():
8291+
if (current.constKeyword case var constKeyword?) {
8292+
return (current, constKeyword);
8293+
}
82908294
case EnumConstantArguments():
82918295
return (current, null);
82928296
case InstanceCreationExpression():

0 commit comments

Comments
 (0)