Skip to content

Commit b68b86b

Browse files
scheglovCommit Queue
authored andcommitted
Elements. Migrate RemoveUnusedElement.
Change-Id: I616a2078b8c08cb8400cc8297fac118086b2bab0 Reviewed-on: https://dart-review.googlesource.com/c/sdk/+/389640 Commit-Queue: Konstantin Shcheglov <[email protected]> Reviewed-by: Phil Quitslund <[email protected]> Reviewed-by: Samuel Rawlins <[email protected]>
1 parent c5c7fc8 commit b68b86b

File tree

2 files changed

+49
-39
lines changed

2 files changed

+49
-39
lines changed

pkg/analysis_server/analyzer_use_new_elements.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -415,6 +415,7 @@ lib/src/services/correction/dart/remove_unnecessary_raw_string.dart
415415
lib/src/services/correction/dart/remove_unnecessary_string_escape.dart
416416
lib/src/services/correction/dart/remove_unnecessary_string_interpolation.dart
417417
lib/src/services/correction/dart/remove_unnecessary_wildcard_pattern.dart
418+
lib/src/services/correction/dart/remove_unused.dart
418419
lib/src/services/correction/dart/remove_unused_catch_clause.dart
419420
lib/src/services/correction/dart/remove_unused_catch_stack.dart
420421
lib/src/services/correction/dart/remove_unused_import.dart

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

Lines changed: 48 additions & 39 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ import 'package:analysis_server_plugin/edit/dart/correction_producer.dart';
77
import 'package:analyzer/dart/ast/ast.dart';
88
import 'package:analyzer/dart/ast/token.dart';
99
import 'package:analyzer/dart/ast/visitor.dart';
10-
import 'package:analyzer/dart/element/element.dart';
10+
import 'package:analyzer/dart/element/element2.dart';
1111
import 'package:analyzer/source/source_range.dart';
1212
import 'package:analyzer/src/dart/ast/extensions.dart';
1313
import 'package:analyzer_plugin/utilities/change_builder/change_builder_core.dart';
@@ -39,34 +39,39 @@ class RemoveUnusedElement extends _RemoveUnused {
3939
return;
4040
}
4141

42-
if (node is ClassDeclaration ||
43-
node is EnumDeclaration ||
44-
node is FunctionDeclaration ||
45-
node is FunctionTypeAlias ||
46-
node is MethodDeclaration ||
47-
node is VariableDeclaration) {
48-
var element = node is Declaration
49-
? node.declaredElement!
50-
: (node as NamedCompilationUnitMember).declaredElement!;
51-
var references = _findAllReferences(unit, element);
52-
// TODO(pq): consider filtering for references that are limited to within the class.
53-
if (references.isEmpty) {
54-
var parent = node.parent;
55-
var grandParent = parent?.parent;
56-
SourceRange sourceRange;
57-
if (node is VariableDeclaration &&
58-
parent is VariableDeclarationList &&
59-
grandParent != null) {
60-
if (parent.variables.length == 1) {
61-
sourceRange = utils.getLinesRange(range.node(grandParent));
62-
} else {
63-
sourceRange = range.nodeInList(parent.variables, node);
64-
}
42+
var element = switch (node) {
43+
ClassDeclaration() => node.declaredFragment?.element,
44+
EnumDeclaration() => node.declaredFragment?.element,
45+
FunctionDeclaration() =>
46+
node.declaredElement2 ?? node.declaredFragment?.element,
47+
FunctionTypeAlias() => node.declaredFragment?.element,
48+
GenericTypeAlias() => node.declaredFragment?.element,
49+
MethodDeclaration() => node.declaredFragment?.element,
50+
VariableDeclaration() => node.declaredFragment?.element,
51+
_ => null,
52+
};
53+
if (element == null) {
54+
return;
55+
}
56+
57+
var references = _findAllReferences(unit, element);
58+
// TODO(pq): consider filtering for references that are limited to within the class.
59+
if (references.isEmpty) {
60+
var parent = node.parent;
61+
var grandParent = parent?.parent;
62+
SourceRange sourceRange;
63+
if (node is VariableDeclaration &&
64+
parent is VariableDeclarationList &&
65+
grandParent != null) {
66+
if (parent.variables.length == 1) {
67+
sourceRange = utils.getLinesRange(range.node(grandParent));
6568
} else {
66-
sourceRange = utils.getLinesRange(range.node(node));
69+
sourceRange = range.nodeInList(parent.variables, node);
6770
}
68-
sourceRanges.add(sourceRange);
71+
} else {
72+
sourceRange = utils.getLinesRange(range.node(node));
6973
}
74+
sourceRanges.add(sourceRange);
7075
}
7176

7277
await builder.addDartFileEdit(file, (builder) {
@@ -116,8 +121,8 @@ class RemoveUnusedField extends _RemoveUnused {
116121
return;
117122
}
118123

119-
var element = declaration.declaredElement;
120-
if (element is! FieldElement) {
124+
var element = declaration.declaredFragment!.element;
125+
if (element is! FieldElement2) {
121126
return;
122127
}
123128

@@ -243,16 +248,16 @@ class RemoveUnusedField extends _RemoveUnused {
243248
}
244249

245250
class _ElementReferenceCollector extends RecursiveAstVisitor<void> {
246-
final Element element;
251+
final Element2 element;
247252
final List<AstNode> references = [];
248253

249254
_ElementReferenceCollector(this.element);
250255

251256
@override
252257
void visitFieldFormalParameter(FieldFormalParameter node) {
253-
var declaredElement = node.declaredElement;
254-
if (declaredElement is FieldFormalParameterElement) {
255-
if (declaredElement.field == element) {
258+
var declaredElement = node.declaredFragment!.element;
259+
if (declaredElement is FieldFormalParameterElement2) {
260+
if (declaredElement.field2 == element) {
256261
references.add(node);
257262
}
258263
}
@@ -262,7 +267,7 @@ class _ElementReferenceCollector extends RecursiveAstVisitor<void> {
262267

263268
@override
264269
void visitNamedType(NamedType node) {
265-
if (node.element == element) {
270+
if (node.element2 == element) {
266271
references.add(node);
267272
}
268273

@@ -271,15 +276,19 @@ class _ElementReferenceCollector extends RecursiveAstVisitor<void> {
271276

272277
@override
273278
void visitSimpleIdentifier(SimpleIdentifier node) {
274-
var staticElement = node.writeOrReadElement;
279+
var staticElement = node.writeOrReadElement2;
275280
if (staticElement == element) {
276281
references.add(node);
277-
} else if (staticElement is PropertyAccessorElement) {
278-
if (staticElement.variable2 == element) {
282+
} else if (staticElement is GetterElement) {
283+
if (staticElement.variable3 == element) {
284+
references.add(node);
285+
}
286+
} else if (staticElement is SetterElement) {
287+
if (staticElement.variable3 == element) {
279288
references.add(node);
280289
}
281-
} else if (staticElement is FieldFormalParameterElement) {
282-
if (staticElement.field == element) {
290+
} else if (staticElement is FieldFormalParameterElement2) {
291+
if (staticElement.field2 == element) {
283292
references.add(node);
284293
}
285294
}
@@ -289,7 +298,7 @@ class _ElementReferenceCollector extends RecursiveAstVisitor<void> {
289298
abstract class _RemoveUnused extends ResolvedCorrectionProducer {
290299
_RemoveUnused({required super.context});
291300

292-
List<AstNode> _findAllReferences(AstNode root, Element element) {
301+
List<AstNode> _findAllReferences(AstNode root, Element2 element) {
293302
var collector = _ElementReferenceCollector(element);
294303
root.accept(collector);
295304
return collector.references;

0 commit comments

Comments
 (0)