Skip to content

Commit

Permalink
+ remove_break fix (for unnecessary_breaks)
Browse files Browse the repository at this point in the history
See: #49960

Change-Id: I67f3e58030aa800bebc045fc86114a5d9e5f7522
Reviewed-on: https://dart-review.googlesource.com/c/sdk/+/281464
Reviewed-by: Brian Wilkerson <brianwilkerson@google.com>
Commit-Queue: Phil Quitslund <pquitslund@google.com>
  • Loading branch information
pq authored and Commit Queue committed Feb 7, 2023
1 parent e86fe85 commit 2192131
Show file tree
Hide file tree
Showing 7 changed files with 141 additions and 3 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
// Copyright (c) 2023, 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/dart/abstract_producer.dart';
import 'package:analysis_server/src/services/correction/fix.dart';
import 'package:analyzer/dart/ast/ast.dart';
import 'package:analyzer_plugin/utilities/change_builder/change_builder_core.dart';
import 'package:analyzer_plugin/utilities/fixes/fixes.dart';
import 'package:analyzer_plugin/utilities/range_factory.dart';

class RemoveBreak extends CorrectionProducer {
@override
bool get canBeAppliedInBulk => true;

@override
bool get canBeAppliedToFile => true;

@override
FixKind get fixKind => DartFixKind.REMOVE_BREAK;

@override
FixKind get multiFixKind => DartFixKind.REMOVE_BREAK_MULTI;

@override
Future<void> compute(ChangeBuilder builder) async {
final breakStatement = node;
if (breakStatement is BreakStatement) {
await builder.addDartFileEdit(file, (builder) {
var start = utils.getLineContentStart(breakStatement.offset);
var end = utils.getLineContentEnd(breakStatement.end);
builder.addDeletion(range.startOffsetEndOffset(start, end));
});
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -2001,9 +2001,7 @@ LintCode.unnecessary_await_in_return:
LintCode.unnecessary_brace_in_string_interps:
status: hasFix
LintCode.unnecessary_breaks:
status: needsFix
notes: |-
https://github.com/dart-lang/sdk/issues/49960
status: hasFix
LintCode.unnecessary_const:
status: hasFix
LintCode.unnecessary_constructor_name:
Expand Down
10 changes: 10 additions & 0 deletions pkg/analysis_server/lib/src/services/correction/fix.dart
Original file line number Diff line number Diff line change
Expand Up @@ -944,6 +944,16 @@ class DartFixKind {
DartFixKindPriority.IN_FILE,
'Remove awaits in file',
);
static const REMOVE_BREAK = FixKind(
'dart.fix.remove.break',
DartFixKindPriority.DEFAULT,
'Remove break',
);
static const REMOVE_BREAK_MULTI = FixKind(
'dart.fix.remove.break.multi',
DartFixKindPriority.IN_FILE,
'Remove unnecessary breaks in file',
);
static const REMOVE_CHARACTER = FixKind(
'dart.fix.remove.character',
DartFixKindPriority.DEFAULT,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -118,6 +118,7 @@ import 'package:analysis_server/src/services/correction/dart/remove_argument.dar
import 'package:analysis_server/src/services/correction/dart/remove_assertion.dart';
import 'package:analysis_server/src/services/correction/dart/remove_assignment.dart';
import 'package:analysis_server/src/services/correction/dart/remove_await.dart';
import 'package:analysis_server/src/services/correction/dart/remove_break.dart';
import 'package:analysis_server/src/services/correction/dart/remove_character.dart';
import 'package:analysis_server/src/services/correction/dart/remove_comparison.dart';
import 'package:analysis_server/src/services/correction/dart/remove_const.dart';
Expand Down Expand Up @@ -662,6 +663,9 @@ class FixProcessor extends BaseProcessor {
LintNames.unnecessary_brace_in_string_interps: [
RemoveInterpolationBraces.new,
],
LintNames.unnecessary_breaks: [
RemoveBreak.new,
],
LintNames.unnecessary_const: [
RemoveUnnecessaryConst.new,
],
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -140,6 +140,7 @@ class LintNames {
static const String unawaited_futures = 'unawaited_futures';
static const String unnecessary_brace_in_string_interps =
'unnecessary_brace_in_string_interps';
static const String unnecessary_breaks = 'unnecessary_breaks';
static const String unnecessary_const = 'unnecessary_const';
static const String unnecessary_constructor_name =
'unnecessary_constructor_name';
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,87 @@
// Copyright (c) 2023, 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/linter/lint_names.dart';
import 'package:analyzer_plugin/utilities/fixes/fixes.dart';
import 'package:test_reflective_loader/test_reflective_loader.dart';

import 'fix_processor.dart';

void main() {
defineReflectiveSuite(() {
defineReflectiveTests(RemoveBreakBulkTest);
defineReflectiveTests(RemoveBreakTest);
});
}

@reflectiveTest
class RemoveBreakBulkTest extends BulkFixProcessorTest {
@override
List<String> get experiments => ['patterns', 'records'];

@override
String get lintCode => LintNames.unnecessary_breaks;

Future<void> test_singleFile() async {
await resolveTestCode('''
f() {
switch (1) {
case 1:
f();
break;
case 2:
f();
break;
}
}
''');
await assertHasFix('''
f() {
switch (1) {
case 1:
f();
case 2:
f();
}
}
''');
}
}

@reflectiveTest
class RemoveBreakTest extends FixProcessorLintTest {
@override
List<String> get experiments => ['patterns', 'records'];

@override
FixKind get kind => DartFixKind.REMOVE_BREAK;

@override
String get lintCode => LintNames.unnecessary_breaks;

Future<void> test_single() async {
await resolveTestCode('''
f() {
switch (1) {
case 1:
f();
break;
case 2:
f();
}
}
''');
await assertHasFix('''
f() {
switch (1) {
case 1:
f();
case 2:
f();
}
}
''');
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -147,6 +147,7 @@ import 'remove_argument_test.dart' as remove_argument;
import 'remove_assertion_test.dart' as remove_assertion;
import 'remove_assignment_test.dart' as remove_assignment;
import 'remove_await_test.dart' as remove_await;
import 'remove_break_test.dart' as remove_break;
import 'remove_character_test.dart' as remove_character;
import 'remove_comparison_test.dart' as remove_comparison;
import 'remove_const_test.dart' as remove_const;
Expand Down Expand Up @@ -381,6 +382,7 @@ void main() {
remove_assertion.main();
remove_assignment.main();
remove_await.main();
remove_break.main();
remove_character.main();
remove_comparison.main();
remove_const.main();
Expand Down

0 comments on commit 2192131

Please sign in to comment.