Skip to content

Commit f03b884

Browse files
FMorschelCommit Queue
authored andcommitted
[DAS] Adds null-aware fix to function invocations
No specific issue but I noticed this at a comment on the first one and some related work about `call` is being done at the second. Bug: #42762 Bug: #61319 Change-Id: Iac73d62884ae70dd27c47ee135748eca23ed06b2 Reviewed-on: https://dart-review.googlesource.com/c/sdk/+/449142 Auto-Submit: Felipe Morschel <[email protected]> Reviewed-by: Samuel Rawlins <[email protected]> Reviewed-by: Brian Wilkerson <[email protected]> Commit-Queue: Brian Wilkerson <[email protected]>
1 parent 266d076 commit f03b884

File tree

3 files changed

+39
-11
lines changed

3 files changed

+39
-11
lines changed

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

Lines changed: 22 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@ class ReplaceWithNullAware extends ResolvedCorrectionProducer {
1515

1616
/// The operator to replace.
1717
String _operator = '.';
18+
String _operatorPrefix = '?';
1819

1920
ReplaceWithNullAware.inChain({required super.context})
2021
: _correctionKind = _CorrectionKind.inChain;
@@ -30,7 +31,7 @@ class ReplaceWithNullAware extends ResolvedCorrectionProducer {
3031
CorrectionApplicability.singleLocation;
3132

3233
@override
33-
List<String> get fixArguments => [_operator, '?$_operator'];
34+
List<String> get fixArguments => [_operator, '$_operatorPrefix$_operator'];
3435

3536
@override
3637
FixKind get fixKind => DartFixKind.REPLACE_WITH_NULL_AWARE;
@@ -81,25 +82,36 @@ class ReplaceWithNullAware extends ResolvedCorrectionProducer {
8182
node = parent.cascadeSections.first;
8283
}
8384
}
84-
if (node is MethodInvocation) {
85-
await _insert(builder, node.operator);
86-
} else if (node is PrefixedIdentifier) {
87-
await _insert(builder, node.period);
88-
} else if (node is PropertyAccess) {
89-
await _insert(builder, node.operator);
90-
} else if (node is IndexExpression) {
91-
await _insert(builder, node.period);
85+
switch (node) {
86+
case MethodInvocation():
87+
await _insert(builder, node.operator);
88+
case PrefixedIdentifier():
89+
await _insert(builder, node.period);
90+
case PropertyAccess():
91+
await _insert(builder, node.operator);
92+
case IndexExpression():
93+
await _insert(builder, node.period);
94+
case FunctionExpressionInvocation(:var argumentList):
95+
await _insertCall(builder, argumentList);
9296
}
9397
}
9498

9599
Future<void> _insert(ChangeBuilder builder, Token? token) async {
96100
if (token != null) {
97101
_operator = token.lexeme;
98102
await builder.addDartFileEdit(file, (builder) {
99-
builder.addSimpleInsertion(token.offset, '?');
103+
builder.addSimpleInsertion(token.offset, _operatorPrefix);
100104
});
101105
}
102106
}
107+
108+
Future<void> _insertCall(ChangeBuilder builder, ArgumentList node) async {
109+
_operator = '(';
110+
_operatorPrefix = '?.call';
111+
await builder.addDartFileEdit(file, (builder) {
112+
builder.addSimpleInsertion(node.offset, _operatorPrefix);
113+
});
114+
}
103115
}
104116

105117
/// The kinds of corrections supported by [ReplaceWithNullAware].

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

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -887,7 +887,10 @@ final _builtInNonLintGenerators = <DiagnosticCode, List<ProducerGenerator>>{
887887
CompileTimeErrorCode.superInvocationNotLast: [MakeSuperInvocationLast.new],
888888
CompileTimeErrorCode.switchCaseCompletesNormally: [AddSwitchCaseBreak.new],
889889
CompileTimeErrorCode.typeTestWithUndefinedName: [ChangeTo.classOrMixin],
890-
CompileTimeErrorCode.uncheckedInvocationOfNullableValue: [AddNullCheck.new],
890+
CompileTimeErrorCode.uncheckedInvocationOfNullableValue: [
891+
AddNullCheck.new,
892+
ReplaceWithNullAware.single,
893+
],
891894
CompileTimeErrorCode.uncheckedMethodInvocationOfNullableValue: [
892895
AddNullCheck.new,
893896
ExtractLocalVariable.new,

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

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,19 @@ class ReplaceWithNullAwareTest extends FixProcessorTest {
2121
@override
2222
FixKind get kind => DartFixKind.REPLACE_WITH_NULL_AWARE;
2323

24+
Future<void> test_function() async {
25+
await resolveTestCode('''
26+
void foo(void Function()? f) {
27+
f();
28+
}
29+
''');
30+
await assertHasFix('''
31+
void foo(void Function()? f) {
32+
f?.call();
33+
}
34+
''');
35+
}
36+
2437
Future<void> test_indexExpression() async {
2538
await resolveTestCode('''
2639
void f(List<int>? l) {

0 commit comments

Comments
 (0)