add fix for use_rethrow
See: https://github.com/dart-lang/linter/issues/1374. Change-Id: I7e79ad37681afdbd94d7968451a768e74217578e Reviewed-on: https://dart-review.googlesource.com/c/sdk/+/95708 Commit-Queue: Phil Quitslund <pquitslund@google.com> Reviewed-by: Brian Wilkerson <brianwilkerson@google.com>
This commit is contained in:
committed by
commit-bot@chromium.org
parent
1551d9c660
commit
593405afda
@@ -297,4 +297,6 @@ class DartFixKind {
|
||||
'USE_NOT_EQ_NULL', 50, "Use != null instead of 'is! Null'",
|
||||
appliedTogetherMessage:
|
||||
"Use != null instead of 'is! Null' everywhere in file");
|
||||
static const USE_RETHROW =
|
||||
const FixKind('USE_RETHROW', 50, "Replace throw with rethrow");
|
||||
}
|
||||
|
||||
@@ -625,6 +625,9 @@ class FixProcessor {
|
||||
if (name == LintNames.unnecessary_this) {
|
||||
await _addFix_removeThisExpression();
|
||||
}
|
||||
if (name == LintNames.use_rethrow_when_possible) {
|
||||
await _addFix_replaceWithRethrow();
|
||||
}
|
||||
}
|
||||
// done
|
||||
return fixes;
|
||||
@@ -3224,6 +3227,16 @@ class FixProcessor {
|
||||
}
|
||||
}
|
||||
|
||||
Future<void> _addFix_replaceWithRethrow() async {
|
||||
if (coveredNode is ThrowExpression) {
|
||||
var changeBuilder = _newDartChangeBuilder();
|
||||
await changeBuilder.addFileEdit(file, (DartFileEditBuilder builder) {
|
||||
builder.addSimpleReplacement(range.node(coveredNode), 'rethrow');
|
||||
});
|
||||
_addFixFromBuilder(changeBuilder, DartFixKind.USE_RETHROW);
|
||||
}
|
||||
}
|
||||
|
||||
Future<void> _addFix_replaceWithIdentifier() async {
|
||||
// TODO(brianwilkerson) Determine whether this await is necessary.
|
||||
await null;
|
||||
@@ -4329,6 +4342,7 @@ class LintNames {
|
||||
static const String unnecessary_new = 'unnecessary_new';
|
||||
static const String unnecessary_override = 'unnecessary_override';
|
||||
static const String unnecessary_this = 'unnecessary_this';
|
||||
static const String use_rethrow_when_possible = 'use_rethrow_when_possible';
|
||||
}
|
||||
|
||||
/**
|
||||
|
||||
@@ -0,0 +1,42 @@
|
||||
// Copyright (c) 2019, 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(UseRethrowTest);
|
||||
});
|
||||
}
|
||||
|
||||
@reflectiveTest
|
||||
class UseRethrowTest extends FixProcessorLintTest {
|
||||
@override
|
||||
FixKind get kind => DartFixKind.USE_RETHROW;
|
||||
|
||||
@override
|
||||
String get lintCode => LintNames.use_rethrow_when_possible;
|
||||
|
||||
test_rethrow() async {
|
||||
await resolveTestUnit('''
|
||||
void bad1() {
|
||||
try {} catch (e) {
|
||||
throw/*LINT*/ e;
|
||||
}
|
||||
}
|
||||
''');
|
||||
await assertHasFix('''
|
||||
void bad1() {
|
||||
try {} catch (e) {
|
||||
rethrow;
|
||||
}
|
||||
}
|
||||
''');
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user