Skip to content

Commit

Permalink
add fix for use_rethrow
Browse files Browse the repository at this point in the history
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>
  • Loading branch information
pq authored and commit-bot@chromium.org committed Mar 7, 2019
1 parent 1551d9c commit 593405a
Show file tree
Hide file tree
Showing 3 changed files with 58 additions and 0 deletions.
2 changes: 2 additions & 0 deletions pkg/analysis_server/lib/src/services/correction/fix.dart
Original file line number Diff line number Diff line change
Expand Up @@ -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");
}
14 changes: 14 additions & 0 deletions pkg/analysis_server/lib/src/services/correction/fix_internal.dart
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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;
Expand Down Expand Up @@ -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';
}

/**
Expand Down
Original file line number Diff line number Diff line change
@@ -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;
}
}
''');
}
}

0 comments on commit 593405a

Please sign in to comment.