Skip to content

Commit

Permalink
add fix for no_duplicate_case_values
Browse files Browse the repository at this point in the history
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 <brianwilkerson@google.com>
Commit-Queue: Phil Quitslund <pquitslund@google.com>
  • Loading branch information
pq authored and commit-bot@chromium.org committed Mar 7, 2019
1 parent 593405a commit 00aeae4
Show file tree
Hide file tree
Showing 3 changed files with 105 additions and 0 deletions.
2 changes: 2 additions & 0 deletions pkg/analysis_server/lib/src/services/correction/fix.dart
Original file line number Diff line number Diff line change
Expand Up @@ -209,6 +209,8 @@ class DartFixKind {
static const REMOVE_AWAIT = const FixKind('REMOVE_AWAIT', 50, "Remove await");
static const REMOVE_DEAD_CODE =
const FixKind('REMOVE_DEAD_CODE', 50, "Remove dead code");
static const REMOVE_DUPLICATE_CASE = const FixKind(
'REMOVE_DUPLICATE_CASE', 50, "Remove duplicate case statement");
static const REMOVE_EMPTY_CATCH =
const FixKind('REMOVE_EMPTY_CATCH', 50, "Remove empty catch clause");
static const REMOVE_EMPTY_CONSTRUCTOR_BODY = const FixKind(
Expand Down
14 changes: 14 additions & 0 deletions pkg/analysis_server/lib/src/services/correction/fix_internal.dart
Original file line number Diff line number Diff line change
Expand Up @@ -586,6 +586,9 @@ class FixProcessor {
if (name == LintNames.empty_statements) {
await _addFix_removeEmptyStatement();
}
if (name == LintNames.no_duplicate_case_values) {
await _addFix_removeCaseStatement();
}
if (name == LintNames.non_constant_identifier_names) {
await _addFix_renameToCamelCase();
}
Expand Down Expand Up @@ -2699,6 +2702,16 @@ class FixProcessor {
}
}

Future<void> _addFix_removeCaseStatement() async {
if (coveredNode is SwitchCase) {
var changeBuilder = _newDartChangeBuilder();
await changeBuilder.addFileEdit(file, (DartFileEditBuilder builder) {
builder.addDeletion(utils.getLinesRange(range.node(coveredNode)));
});
_addFixFromBuilder(changeBuilder, DartFixKind.REMOVE_DUPLICATE_CASE);
}
}

Future<void> _addFix_removeConstKeyword() async {
final instanceCreationExpression = node;
if (instanceCreationExpression is InstanceCreationExpression) {
Expand Down Expand Up @@ -4325,6 +4338,7 @@ class LintNames {
static const String empty_catches = 'empty_catches';
static const String empty_constructor_bodies = 'empty_constructor_bodies';
static const String empty_statements = 'empty_statements';
static const String no_duplicate_case_values = 'no_duplicate_case_values';
static const String non_constant_identifier_names =
'non_constant_identifier_names';
static const String prefer_collection_literals = 'prefer_collection_literals';
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,89 @@
// Copyright (c) 2018, the Dart project authors. Please see the AUTHORS file
// for details. All rights reserved. Use of this source code is governed by a
// BSD-style license that can be found in the LICENSE file.

import 'package:analysis_server/src/services/correction/fix.dart';
import 'package:analysis_server/src/services/correction/fix_internal.dart';
import 'package:analyzer_plugin/utilities/fixes/fixes.dart';
import 'package:test_reflective_loader/test_reflective_loader.dart';

import 'fix_processor.dart';

main() {
defineReflectiveSuite(() {
defineReflectiveTests(RemoveDuplicateCaseTest);
});
}

@reflectiveTest
class RemoveDuplicateCaseTest extends FixProcessorLintTest {
@override
FixKind get kind => DartFixKind.REMOVE_DUPLICATE_CASE;

@override
String get lintCode => LintNames.no_duplicate_case_values;

test_removeStringCase() async {
await resolveTestUnit('''
void switchString() {
String v = 'a';
switch (v) {
case 'a':
print('a');
break;
case 'b':
print('b');
break;
case 'a' /*LINT*/:
print('a');
break;
default:
print('?);
}
}
''');
await assertHasFix('''
void switchString() {
String v = 'a';
switch (v) {
case 'a':
print('a');
break;
case 'b':
print('b');
break;
default:
print('?);
}
}
''');
}

test_removeIntCase() async {
await resolveTestUnit('''
void switchInt() {
switch (2) {
case 1:
print('a');
break;
case 2:
case 2 /*LINT*/:
default:
print('?);
}
}
''');
await assertHasFix('''
void switchInt() {
switch (2) {
case 1:
print('a');
break;
case 2:
default:
print('?);
}
}
''');
}
}

0 comments on commit 00aeae4

Please sign in to comment.