mirror of
https://github.com/dart-lang/sdk
synced 2024-10-14 12:30:03 +00:00
+ remove_break fix (for unnecessary_breaks
)
See: https://github.com/dart-lang/sdk/issues/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>
This commit is contained in:
parent
e86fe85aa4
commit
2192131b5e
|
@ -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));
|
||||||
|
});
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
|
@ -2001,9 +2001,7 @@ LintCode.unnecessary_await_in_return:
|
||||||
LintCode.unnecessary_brace_in_string_interps:
|
LintCode.unnecessary_brace_in_string_interps:
|
||||||
status: hasFix
|
status: hasFix
|
||||||
LintCode.unnecessary_breaks:
|
LintCode.unnecessary_breaks:
|
||||||
status: needsFix
|
status: hasFix
|
||||||
notes: |-
|
|
||||||
https://github.com/dart-lang/sdk/issues/49960
|
|
||||||
LintCode.unnecessary_const:
|
LintCode.unnecessary_const:
|
||||||
status: hasFix
|
status: hasFix
|
||||||
LintCode.unnecessary_constructor_name:
|
LintCode.unnecessary_constructor_name:
|
||||||
|
|
|
@ -944,6 +944,16 @@ class DartFixKind {
|
||||||
DartFixKindPriority.IN_FILE,
|
DartFixKindPriority.IN_FILE,
|
||||||
'Remove awaits 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(
|
static const REMOVE_CHARACTER = FixKind(
|
||||||
'dart.fix.remove.character',
|
'dart.fix.remove.character',
|
||||||
DartFixKindPriority.DEFAULT,
|
DartFixKindPriority.DEFAULT,
|
||||||
|
|
|
@ -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_assertion.dart';
|
||||||
import 'package:analysis_server/src/services/correction/dart/remove_assignment.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_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_character.dart';
|
||||||
import 'package:analysis_server/src/services/correction/dart/remove_comparison.dart';
|
import 'package:analysis_server/src/services/correction/dart/remove_comparison.dart';
|
||||||
import 'package:analysis_server/src/services/correction/dart/remove_const.dart';
|
import 'package:analysis_server/src/services/correction/dart/remove_const.dart';
|
||||||
|
@ -662,6 +663,9 @@ class FixProcessor extends BaseProcessor {
|
||||||
LintNames.unnecessary_brace_in_string_interps: [
|
LintNames.unnecessary_brace_in_string_interps: [
|
||||||
RemoveInterpolationBraces.new,
|
RemoveInterpolationBraces.new,
|
||||||
],
|
],
|
||||||
|
LintNames.unnecessary_breaks: [
|
||||||
|
RemoveBreak.new,
|
||||||
|
],
|
||||||
LintNames.unnecessary_const: [
|
LintNames.unnecessary_const: [
|
||||||
RemoveUnnecessaryConst.new,
|
RemoveUnnecessaryConst.new,
|
||||||
],
|
],
|
||||||
|
|
|
@ -140,6 +140,7 @@ class LintNames {
|
||||||
static const String unawaited_futures = 'unawaited_futures';
|
static const String unawaited_futures = 'unawaited_futures';
|
||||||
static const String unnecessary_brace_in_string_interps =
|
static const String unnecessary_brace_in_string_interps =
|
||||||
'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_const = 'unnecessary_const';
|
||||||
static const String unnecessary_constructor_name =
|
static const String unnecessary_constructor_name =
|
||||||
'unnecessary_constructor_name';
|
'unnecessary_constructor_name';
|
||||||
|
|
|
@ -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();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
''');
|
||||||
|
}
|
||||||
|
}
|
|
@ -147,6 +147,7 @@ import 'remove_argument_test.dart' as remove_argument;
|
||||||
import 'remove_assertion_test.dart' as remove_assertion;
|
import 'remove_assertion_test.dart' as remove_assertion;
|
||||||
import 'remove_assignment_test.dart' as remove_assignment;
|
import 'remove_assignment_test.dart' as remove_assignment;
|
||||||
import 'remove_await_test.dart' as remove_await;
|
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_character_test.dart' as remove_character;
|
||||||
import 'remove_comparison_test.dart' as remove_comparison;
|
import 'remove_comparison_test.dart' as remove_comparison;
|
||||||
import 'remove_const_test.dart' as remove_const;
|
import 'remove_const_test.dart' as remove_const;
|
||||||
|
@ -381,6 +382,7 @@ void main() {
|
||||||
remove_assertion.main();
|
remove_assertion.main();
|
||||||
remove_assignment.main();
|
remove_assignment.main();
|
||||||
remove_await.main();
|
remove_await.main();
|
||||||
|
remove_break.main();
|
||||||
remove_character.main();
|
remove_character.main();
|
||||||
remove_comparison.main();
|
remove_comparison.main();
|
||||||
remove_const.main();
|
remove_const.main();
|
||||||
|
|
Loading…
Reference in a new issue