From 593405afda8132cd93fe86eb3dfe6cebb2ef73ac Mon Sep 17 00:00:00 2001 From: pq Date: Thu, 7 Mar 2019 15:25:26 +0000 Subject: [PATCH] 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 Reviewed-by: Brian Wilkerson --- .../lib/src/services/correction/fix.dart | 2 + .../src/services/correction/fix_internal.dart | 14 +++++++ .../correction/fix/use_rethrow_test.dart | 42 +++++++++++++++++++ 3 files changed, 58 insertions(+) create mode 100644 pkg/analysis_server/test/src/services/correction/fix/use_rethrow_test.dart diff --git a/pkg/analysis_server/lib/src/services/correction/fix.dart b/pkg/analysis_server/lib/src/services/correction/fix.dart index b8b81bd0754..deb01ba3d8f 100644 --- a/pkg/analysis_server/lib/src/services/correction/fix.dart +++ b/pkg/analysis_server/lib/src/services/correction/fix.dart @@ -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"); } diff --git a/pkg/analysis_server/lib/src/services/correction/fix_internal.dart b/pkg/analysis_server/lib/src/services/correction/fix_internal.dart index e9e042b3fdf..c967694272d 100644 --- a/pkg/analysis_server/lib/src/services/correction/fix_internal.dart +++ b/pkg/analysis_server/lib/src/services/correction/fix_internal.dart @@ -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 _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 _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'; } /** diff --git a/pkg/analysis_server/test/src/services/correction/fix/use_rethrow_test.dart b/pkg/analysis_server/test/src/services/correction/fix/use_rethrow_test.dart new file mode 100644 index 00000000000..5a498674ce8 --- /dev/null +++ b/pkg/analysis_server/test/src/services/correction/fix/use_rethrow_test.dart @@ -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; + } +} +'''); + } +}