Skip to content

Commit 8353966

Browse files
keertipCommit Queue
authored and
Commit Queue
committed
Wire up remove var and replace if else with conditional for patterns
BUG=#49960 Change-Id: I1a2d6e69127d4d82ed2cd4a121572d0ff6c3af82 Reviewed-on: https://dart-review.googlesource.com/c/sdk/+/280200 Reviewed-by: Brian Wilkerson <[email protected]> Commit-Queue: Keerti Parthasarathy <[email protected]>
1 parent 459b2bf commit 8353966

File tree

5 files changed

+60
-4
lines changed

5 files changed

+60
-4
lines changed

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

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,9 @@ class ReplaceIfElseWithConditional extends CorrectionProducer {
2222
return;
2323
}
2424
var ifStatement = node as IfStatement;
25+
if (ifStatement.caseClause != null) {
26+
return;
27+
}
2528
// single then/else statements
2629
var thenStatement = getSingleStatement(ifStatement.thenStatement);
2730
var elseStatement = getSingleStatement(ifStatement.elseStatement);

pkg/analysis_server/lib/src/services/correction/error_fix_status.yaml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1193,7 +1193,7 @@ CompileTimeErrorCode.VALUES_DECLARATION_IN_ENUM:
11931193
to think about what they were trying to do and it seems more likely that the
11941194
right fix is to rename the member.
11951195
CompileTimeErrorCode.VARIABLE_PATTERN_KEYWORD_IN_DECLARATION_CONTEXT:
1196-
status: needsEvaluation
1196+
status: hasFix
11971197
CompileTimeErrorCode.VARIABLE_TYPE_MISMATCH:
11981198
status: needsEvaluation
11991199
CompileTimeErrorCode.WRONG_EXPLICIT_TYPE_PARAMETER_VARIANCE_IN_SUPERINTERFACE:

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

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -961,6 +961,9 @@ class FixProcessor extends BaseProcessor {
961961
CompileTimeErrorCode.CONST_WITH_NON_TYPE: [
962962
ChangeTo.classOrMixin,
963963
],
964+
CompileTimeErrorCode.CONSTANT_PATTERN_WITH_NON_CONSTANT_EXPRESSION: [
965+
AddConst.new,
966+
],
964967
CompileTimeErrorCode.DEFAULT_VALUE_ON_REQUIRED_PARAMETER: [
965968
RemoveDefaultValue.new,
966969
RemoveRequired.new,
@@ -1299,16 +1302,16 @@ class FixProcessor extends BaseProcessor {
12991302
CompileTimeErrorCode.URI_DOES_NOT_EXIST: [
13001303
CreateFile.new,
13011304
],
1305+
CompileTimeErrorCode.VARIABLE_PATTERN_KEYWORD_IN_DECLARATION_CONTEXT: [
1306+
RemoveVar.new,
1307+
],
13021308
CompileTimeErrorCode.WRONG_NUMBER_OF_TYPE_ARGUMENTS_CONSTRUCTOR: [
13031309
MoveTypeArgumentsToClass.new,
13041310
RemoveTypeArguments.new,
13051311
],
13061312
CompileTimeErrorCode.YIELD_OF_INVALID_TYPE: [
13071313
MakeReturnTypeNullable.new,
13081314
],
1309-
CompileTimeErrorCode.CONSTANT_PATTERN_WITH_NON_CONSTANT_EXPRESSION: [
1310-
AddConst.new,
1311-
],
13121315
FfiCode.SUBTYPE_OF_FFI_CLASS_IN_EXTENDS: [
13131316
RemoveNameFromDeclarationClause.new,
13141317
],

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

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -51,6 +51,21 @@ void f() {
5151
await assertNoAssistAt('else');
5252
}
5353

54+
Future<void> test_ifCasePattern() async {
55+
await resolveTestCode('''
56+
f() {
57+
var json = [1, 2, 3];
58+
int vvv;
59+
if (json case [3, 4]) {
60+
vvv = 111;
61+
} else {
62+
vvv = 222;
63+
}
64+
}
65+
''');
66+
await assertNoAssistAt('if (json case [3, 4])');
67+
}
68+
5469
Future<void> test_notIfStatement() async {
5570
await resolveTestCode('''
5671
void f() {

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

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,41 @@ f() {}
2929
''');
3030
}
3131

32+
Future<void> test_pattern_declaration() async {
33+
await resolveTestCode('''
34+
f() {
35+
var [var x] = [1];
36+
print(x);
37+
}
38+
''');
39+
await assertHasFix('''
40+
f() {
41+
var [x] = [1];
42+
print(x);
43+
}
44+
''');
45+
}
46+
47+
@FailingTest(
48+
issue: "https://github.com/dart-lang/sdk/issues/49960",
49+
reason: "Fix once error is reported")
50+
Future<void> test_pattern_in_assignment() async {
51+
await resolveTestCode('''
52+
f() {
53+
var a = 1;
54+
var b = 2;
55+
(var a, int b) = (3, 4);
56+
}
57+
''');
58+
await assertHasFix('''
59+
f() {
60+
var a = 1;
61+
var b = 2;
62+
(a, int b) = (3, 4);
63+
}
64+
''');
65+
}
66+
3267
Future<void> test_setter() async {
3368
await resolveTestCode('''
3469
class C {

0 commit comments

Comments
 (0)