Skip to content

Commit 00aeae4

Browse files
pqcommit-bot@chromium.org
authored andcommitted
add fix for no_duplicate_case_values
See: https://github.com/dart-lang/linter/issues/1374. Change-Id: Ia09f2530c9a943b8b80fda63fb313b752748264a Reviewed-on: https://dart-review.googlesource.com/c/sdk/+/95709 Reviewed-by: Brian Wilkerson <[email protected]> Commit-Queue: Phil Quitslund <[email protected]>
1 parent 593405a commit 00aeae4

File tree

3 files changed

+105
-0
lines changed

3 files changed

+105
-0
lines changed

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

+2
Original file line numberDiff line numberDiff line change
@@ -209,6 +209,8 @@ class DartFixKind {
209209
static const REMOVE_AWAIT = const FixKind('REMOVE_AWAIT', 50, "Remove await");
210210
static const REMOVE_DEAD_CODE =
211211
const FixKind('REMOVE_DEAD_CODE', 50, "Remove dead code");
212+
static const REMOVE_DUPLICATE_CASE = const FixKind(
213+
'REMOVE_DUPLICATE_CASE', 50, "Remove duplicate case statement");
212214
static const REMOVE_EMPTY_CATCH =
213215
const FixKind('REMOVE_EMPTY_CATCH', 50, "Remove empty catch clause");
214216
static const REMOVE_EMPTY_CONSTRUCTOR_BODY = const FixKind(

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

+14
Original file line numberDiff line numberDiff line change
@@ -586,6 +586,9 @@ class FixProcessor {
586586
if (name == LintNames.empty_statements) {
587587
await _addFix_removeEmptyStatement();
588588
}
589+
if (name == LintNames.no_duplicate_case_values) {
590+
await _addFix_removeCaseStatement();
591+
}
589592
if (name == LintNames.non_constant_identifier_names) {
590593
await _addFix_renameToCamelCase();
591594
}
@@ -2699,6 +2702,16 @@ class FixProcessor {
26992702
}
27002703
}
27012704

2705+
Future<void> _addFix_removeCaseStatement() async {
2706+
if (coveredNode is SwitchCase) {
2707+
var changeBuilder = _newDartChangeBuilder();
2708+
await changeBuilder.addFileEdit(file, (DartFileEditBuilder builder) {
2709+
builder.addDeletion(utils.getLinesRange(range.node(coveredNode)));
2710+
});
2711+
_addFixFromBuilder(changeBuilder, DartFixKind.REMOVE_DUPLICATE_CASE);
2712+
}
2713+
}
2714+
27022715
Future<void> _addFix_removeConstKeyword() async {
27032716
final instanceCreationExpression = node;
27042717
if (instanceCreationExpression is InstanceCreationExpression) {
@@ -4325,6 +4338,7 @@ class LintNames {
43254338
static const String empty_catches = 'empty_catches';
43264339
static const String empty_constructor_bodies = 'empty_constructor_bodies';
43274340
static const String empty_statements = 'empty_statements';
4341+
static const String no_duplicate_case_values = 'no_duplicate_case_values';
43284342
static const String non_constant_identifier_names =
43294343
'non_constant_identifier_names';
43304344
static const String prefer_collection_literals = 'prefer_collection_literals';
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,89 @@
1+
// Copyright (c) 2018, the Dart project authors. Please see the AUTHORS file
2+
// for details. All rights reserved. Use of this source code is governed by a
3+
// BSD-style license that can be found in the LICENSE file.
4+
5+
import 'package:analysis_server/src/services/correction/fix.dart';
6+
import 'package:analysis_server/src/services/correction/fix_internal.dart';
7+
import 'package:analyzer_plugin/utilities/fixes/fixes.dart';
8+
import 'package:test_reflective_loader/test_reflective_loader.dart';
9+
10+
import 'fix_processor.dart';
11+
12+
main() {
13+
defineReflectiveSuite(() {
14+
defineReflectiveTests(RemoveDuplicateCaseTest);
15+
});
16+
}
17+
18+
@reflectiveTest
19+
class RemoveDuplicateCaseTest extends FixProcessorLintTest {
20+
@override
21+
FixKind get kind => DartFixKind.REMOVE_DUPLICATE_CASE;
22+
23+
@override
24+
String get lintCode => LintNames.no_duplicate_case_values;
25+
26+
test_removeStringCase() async {
27+
await resolveTestUnit('''
28+
void switchString() {
29+
String v = 'a';
30+
switch (v) {
31+
case 'a':
32+
print('a');
33+
break;
34+
case 'b':
35+
print('b');
36+
break;
37+
case 'a' /*LINT*/:
38+
print('a');
39+
break;
40+
default:
41+
print('?);
42+
}
43+
}
44+
''');
45+
await assertHasFix('''
46+
void switchString() {
47+
String v = 'a';
48+
switch (v) {
49+
case 'a':
50+
print('a');
51+
break;
52+
case 'b':
53+
print('b');
54+
break;
55+
default:
56+
print('?);
57+
}
58+
}
59+
''');
60+
}
61+
62+
test_removeIntCase() async {
63+
await resolveTestUnit('''
64+
void switchInt() {
65+
switch (2) {
66+
case 1:
67+
print('a');
68+
break;
69+
case 2:
70+
case 2 /*LINT*/:
71+
default:
72+
print('?);
73+
}
74+
}
75+
''');
76+
await assertHasFix('''
77+
void switchInt() {
78+
switch (2) {
79+
case 1:
80+
print('a');
81+
break;
82+
case 2:
83+
default:
84+
print('?);
85+
}
86+
}
87+
''');
88+
}
89+
}

0 commit comments

Comments
 (0)