Skip to content

Commit 3753014

Browse files
scheglovcommit-bot@chromium.org
authored andcommitted
Extract RemoveUnusedLocalVariable producer.
[email protected], [email protected] Change-Id: I0d0d7cccd0adb064f87bb1b9e656cad49f03a2e0 Reviewed-on: https://dart-review.googlesource.com/c/sdk/+/139033 Reviewed-by: Brian Wilkerson <[email protected]> Commit-Queue: Konstantin Shcheglov <[email protected]>
1 parent c3673bd commit 3753014

File tree

2 files changed

+68
-51
lines changed

2 files changed

+68
-51
lines changed
Lines changed: 65 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,65 @@
1+
// Copyright (c) 2020, the Dart project authors. Please see the AUTHORS file
2+
// for details. All rights reserved. Use of this source code is governed by a
3+
// BSD-style license that can be found in the LICENSE file.
4+
5+
import 'package:analysis_server/src/services/correction/dart/abstract_producer.dart';
6+
import 'package:analysis_server/src/services/correction/fix.dart';
7+
import 'package:analysis_server/src/services/correction/util.dart';
8+
import 'package:analyzer/dart/ast/ast.dart';
9+
import 'package:analyzer/dart/element/element.dart';
10+
import 'package:analyzer/source/source_range.dart';
11+
import 'package:analyzer_plugin/utilities/change_builder/change_builder_dart.dart';
12+
import 'package:analyzer_plugin/utilities/fixes/fixes.dart';
13+
import 'package:analyzer_plugin/utilities/range_factory.dart';
14+
15+
class RemoveUnusedLocalVariable extends CorrectionProducer {
16+
@override
17+
FixKind get fixKind => DartFixKind.REMOVE_UNUSED_LOCAL_VARIABLE;
18+
19+
@override
20+
Future<void> compute(DartChangeBuilder builder) async {
21+
final declaration = node.parent;
22+
if (!(declaration is VariableDeclaration && declaration.name == node)) {
23+
return;
24+
}
25+
Element element = (declaration as VariableDeclaration).declaredElement;
26+
if (element is! LocalElement) {
27+
return;
28+
}
29+
30+
final sourceRanges = <SourceRange>[];
31+
32+
final functionBody = declaration.thisOrAncestorOfType<FunctionBody>();
33+
final references = findLocalElementReferences(functionBody, element);
34+
for (var reference in references) {
35+
final node = reference.thisOrAncestorMatching((node) =>
36+
node is VariableDeclaration || node is AssignmentExpression);
37+
var sourceRange;
38+
if (node is VariableDeclaration) {
39+
VariableDeclarationList parent = node.parent;
40+
if (parent.variables.length == 1) {
41+
sourceRange = utils.getLinesRange(range.node(parent.parent));
42+
} else {
43+
sourceRange = range.nodeInList(parent.variables, node);
44+
}
45+
} else if (node is AssignmentExpression) {
46+
// todo (pq): consider node.parent is! ExpressionStatement to handle
47+
// assignments in parens, etc.
48+
if (node.parent is ArgumentList) {
49+
sourceRange = range.startStart(node, node.operator.next);
50+
} else {
51+
sourceRange = utils.getLinesRange(range.node(node.parent));
52+
}
53+
} else {
54+
return;
55+
}
56+
sourceRanges.add(sourceRange);
57+
}
58+
59+
await builder.addFileEdit(file, (builder) {
60+
for (var sourceRange in sourceRanges) {
61+
builder.addDeletion(sourceRange);
62+
}
63+
});
64+
}
65+
}

pkg/analysis_server/lib/src/services/correction/fix_internal.dart

Lines changed: 3 additions & 51 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@ import 'package:analysis_server/src/services/correction/dart/inline_typedef.dart
2222
import 'package:analysis_server/src/services/correction/dart/remove_dead_if_null.dart';
2323
import 'package:analysis_server/src/services/correction/dart/remove_if_null_operator.dart';
2424
import 'package:analysis_server/src/services/correction/dart/remove_unused.dart';
25+
import 'package:analysis_server/src/services/correction/dart/remove_unused_local_variable.dart';
2526
import 'package:analysis_server/src/services/correction/dart/replace_with_eight_digit_hex.dart';
2627
import 'package:analysis_server/src/services/correction/dart/wrap_in_future.dart';
2728
import 'package:analysis_server/src/services/correction/fix.dart';
@@ -380,9 +381,6 @@ class FixProcessor extends BaseProcessor {
380381
if (errorCode == HintCode.UNUSED_LABEL) {
381382
await _addFix_removeUnusedLabel();
382383
}
383-
if (errorCode == HintCode.UNUSED_LOCAL_VARIABLE) {
384-
await _addFix_removeUnusedLocalVariable();
385-
}
386384
if (errorCode == HintCode.UNUSED_SHOWN_NAME) {
387385
await _addFix_removeNameFromCombinator();
388386
}
@@ -3773,54 +3771,6 @@ class FixProcessor extends BaseProcessor {
37733771
}
37743772
}
37753773

3776-
Future<void> _addFix_removeUnusedLocalVariable() async {
3777-
final declaration = node.parent;
3778-
if (!(declaration is VariableDeclaration && declaration.name == node)) {
3779-
return;
3780-
}
3781-
Element element = (declaration as VariableDeclaration).declaredElement;
3782-
if (element is! LocalElement) {
3783-
return;
3784-
}
3785-
3786-
final sourceRanges = <SourceRange>[];
3787-
3788-
final functionBody = declaration.thisOrAncestorOfType<FunctionBody>();
3789-
final references = findLocalElementReferences(functionBody, element);
3790-
for (var reference in references) {
3791-
final node = reference.thisOrAncestorMatching((node) =>
3792-
node is VariableDeclaration || node is AssignmentExpression);
3793-
var sourceRange;
3794-
if (node is VariableDeclaration) {
3795-
VariableDeclarationList parent = node.parent;
3796-
if (parent.variables.length == 1) {
3797-
sourceRange = utils.getLinesRange(range.node(parent.parent));
3798-
} else {
3799-
sourceRange = range.nodeInList(parent.variables, node);
3800-
}
3801-
} else if (node is AssignmentExpression) {
3802-
// todo (pq): consider node.parent is! ExpressionStatement to handle
3803-
// assignments in parens, etc.
3804-
if (node.parent is ArgumentList) {
3805-
sourceRange = range.startStart(node, node.operator.next);
3806-
} else {
3807-
sourceRange = utils.getLinesRange(range.node(node.parent));
3808-
}
3809-
} else {
3810-
return;
3811-
}
3812-
sourceRanges.add(sourceRange);
3813-
}
3814-
3815-
final changeBuilder = _newDartChangeBuilder();
3816-
await changeBuilder.addFileEdit(file, (DartFileEditBuilder builder) {
3817-
for (var sourceRange in sourceRanges) {
3818-
builder.addDeletion(sourceRange);
3819-
}
3820-
});
3821-
_addFixFromBuilder(changeBuilder, DartFixKind.REMOVE_UNUSED_LOCAL_VARIABLE);
3822-
}
3823-
38243774
Future<void> _addFix_renameToCamelCase() async {
38253775
if (node is! SimpleIdentifier) {
38263776
return;
@@ -4608,6 +4558,8 @@ class FixProcessor extends BaseProcessor {
46084558
await compute(RemoveUnusedElement());
46094559
} else if (errorCode == HintCode.UNUSED_FIELD) {
46104560
await compute(RemoveUnusedField());
4561+
} else if (errorCode == HintCode.UNUSED_LOCAL_VARIABLE) {
4562+
await compute(RemoveUnusedLocalVariable());
46114563
} else if (errorCode == StaticWarningCode.DEAD_NULL_AWARE_EXPRESSION) {
46124564
await compute(RemoveDeadIfNull());
46134565
} else if (errorCode is LintCode) {

0 commit comments

Comments
 (0)