-
Notifications
You must be signed in to change notification settings - Fork 1.6k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
+ remove_break fix (for
unnecessary_breaks
)
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
Showing
7 changed files
with
141 additions
and
3 deletions.
There are no files selected for viewing
36 changes: 36 additions & 0 deletions
36
pkg/analysis_server/lib/src/services/correction/dart/remove_break.dart
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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)); | ||
}); | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
87 changes: 87 additions & 0 deletions
87
pkg/analysis_server/test/src/services/correction/fix/remove_break_test.dart
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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(); | ||
} | ||
} | ||
'''); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters