Skip to content

Commit 6a534c6

Browse files
FMorschelCommit Queue
authored andcommitted
[DAS] Fixes when clause cases on 'Convert to switch expression' assist
Fixes: #60966 Change-Id: I68187c06528f7328a832bdb6d11170f233ab5e2a Reviewed-on: https://dart-review.googlesource.com/c/sdk/+/435921 Reviewed-by: Brian Wilkerson <[email protected]> Reviewed-by: Samuel Rawlins <[email protected]> Commit-Queue: Brian Wilkerson <[email protected]> Auto-Submit: Felipe Morschel <[email protected]>
1 parent fdc6829 commit 6a534c6

File tree

2 files changed

+79
-10
lines changed

2 files changed

+79
-10
lines changed

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

Lines changed: 9 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -106,7 +106,11 @@ class ConvertToSwitchExpression extends ResolvedCorrectionProducer {
106106
invocation.offset,
107107
invocation.argumentList.leftParenthesis.end,
108108
);
109-
builder.addDeletion(deletion);
109+
if (hasComment) {
110+
builder.addDeletion(deletion);
111+
} else {
112+
builder.addSimpleReplacement(deletion, ' ');
113+
}
110114
builder.addDeletion(
111115
range.entity(invocation.argumentList.rightParenthesis),
112116
);
@@ -154,12 +158,12 @@ class ConvertToSwitchExpression extends ResolvedCorrectionProducer {
154158
var lastColon = lastCase.colon;
155159

156160
var patternCode = group.patternCases
157-
.map((patternCase) => patternCase.guardedPattern.pattern)
161+
.map((patternCase) => patternCase.guardedPattern)
158162
.map((pattern) => utils.getNodeText(pattern))
159163
.join(' || ');
160164
builder.addSimpleReplacement(
161165
range.startEnd(firstCase.keyword, lastColon),
162-
'$patternCode => ',
166+
'$patternCode =>',
163167
);
164168

165169
convertArgumentStatements(
@@ -248,7 +252,7 @@ class ConvertToSwitchExpression extends ResolvedCorrectionProducer {
248252
var lastColon = lastCase.colon;
249253

250254
var patternCode = group.patternCases
251-
.map((patternCase) => patternCase.guardedPattern.pattern)
255+
.map((patternCase) => patternCase.guardedPattern)
252256
.map((pattern) => utils.getNodeText(pattern))
253257
.join(' || ');
254258
builder.addSimpleReplacement(
@@ -334,7 +338,7 @@ class ConvertToSwitchExpression extends ResolvedCorrectionProducer {
334338
var lastCase = group.patternCases.last;
335339

336340
var patternCode = group.patternCases
337-
.map((patternCase) => patternCase.guardedPattern.pattern)
341+
.map((patternCase) => patternCase.guardedPattern)
338342
.map((pattern) => utils.getNodeText(pattern))
339343
.join(' || ');
340344
builder.addSimpleReplacement(

pkg/analysis_server/test/src/services/correction/assist/convert_to_switch_expression_test.dart

Lines changed: 70 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -100,8 +100,8 @@ void f(Color color) {
100100
Color.red => 'red', // Red.
101101
Color.blue => 'blue',
102102
// Not green.
103-
Color.green => throw 'Green is bad',
104-
Color.yellow => /**/
103+
Color.green => throw 'Green is bad',
104+
Color.yellow =>
105105
// Yellow is OK.
106106
'yellow'
107107
});
@@ -164,8 +164,8 @@ void f(Color color) {
164164
Color.red => 'red', // Red.
165165
Color.blue => 'blue',
166166
// Not green.
167-
Color.green => throw 'Green is bad',
168-
Color.yellow => /**/
167+
Color.green => throw 'Green is bad',
168+
Color.yellow =>
169169
// Yellow is OK.
170170
'yellow'
171171
});
@@ -191,7 +191,28 @@ void f(String s) {
191191
print(switch (s) {
192192
'foo' => 'foo',
193193
'bar' => 'bar',
194-
_ => throw 'unrecognized'
194+
_ => throw 'unrecognized'
195+
});
196+
}
197+
''');
198+
}
199+
200+
Future<void> test_argument_when() async {
201+
await resolveTestCode('''
202+
void foo(int a, bool x) {
203+
swit^ch (a) {
204+
case 1 when x:
205+
print(1);
206+
default:
207+
print(3);
208+
}
209+
}
210+
''');
211+
await assertHasAssist('''
212+
void foo(int a, bool x) {
213+
print(switch (a) {
214+
1 when x => 1,
215+
_ => 3
195216
});
196217
}
197218
''');
@@ -476,6 +497,29 @@ String f(String s) {
476497
''');
477498
}
478499

500+
Future<void> test_assignment_when() async {
501+
await resolveTestCode('''
502+
void foo(int a, bool x) {
503+
int value;
504+
swit^ch (a) {
505+
case 1 when x:
506+
value = 1;
507+
default:
508+
value = 3;
509+
}
510+
}
511+
''');
512+
await assertHasAssist('''
513+
void foo(int a, bool x) {
514+
int value;
515+
value = switch (a) {
516+
1 when x => 1,
517+
_ => 3
518+
};
519+
}
520+
''');
521+
}
522+
479523
Future<void> test_empty() async {
480524
await resolveTestCode('''
481525
void f(int x) {
@@ -822,6 +866,27 @@ String name(Color color) {
822866
Color.yellow => 'yellow'
823867
};
824868
}
869+
''');
870+
}
871+
872+
Future<void> test_return_when() async {
873+
await resolveTestCode('''
874+
int foo(int a, bool x) {
875+
swit^ch (a) {
876+
case 1 when x:
877+
return 1;
878+
default:
879+
return 3;
880+
}
881+
}
882+
''');
883+
await assertHasAssist('''
884+
int foo(int a, bool x) {
885+
return switch (a) {
886+
1 when x => 1,
887+
_ => 3
888+
};
889+
}
825890
''');
826891
}
827892
}

0 commit comments

Comments
 (0)