Skip to content

Commit 7023392

Browse files
scheglovCommit Queue
authored andcommitted
Elements. Update RemoveUnusedElement to use FragmentDeclaration and LocalFunctionDeclarationView.
Change-Id: I8dee4c04e372e3312959ba7f3aafd79765677298 Reviewed-on: https://dart-review.googlesource.com/c/sdk/+/389592 Reviewed-by: Phil Quitslund <[email protected]> Commit-Queue: Konstantin Shcheglov <[email protected]>
1 parent fe3a863 commit 7023392

File tree

2 files changed

+27
-12
lines changed

2 files changed

+27
-12
lines changed

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

Lines changed: 7 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -4,11 +4,11 @@
44

55
import 'package:analysis_server/src/services/correction/fix.dart';
66
import 'package:analysis_server_plugin/edit/dart/correction_producer.dart';
7-
import 'package:analyzer/dart/ast/ast.dart';
87
import 'package:analyzer/dart/ast/token.dart';
98
import 'package:analyzer/dart/ast/visitor.dart';
109
import 'package:analyzer/dart/element/element2.dart';
1110
import 'package:analyzer/source/source_range.dart';
11+
import 'package:analyzer/src/dart/ast/ast.dart';
1212
import 'package:analyzer/src/dart/ast/extensions.dart';
1313
import 'package:analyzer_plugin/utilities/change_builder/change_builder_core.dart';
1414
import 'package:analyzer_plugin/utilities/fixes/fixes.dart';
@@ -39,17 +39,12 @@ class RemoveUnusedElement extends _RemoveUnused {
3939
return;
4040
}
4141

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-
};
42+
Element2? element;
43+
if (LocalFunctionDeclarationView.of(node) case var localFunction?) {
44+
element = localFunction.declaredElement;
45+
} else if (node is FragmentDeclaration) {
46+
element = node.declaredFragment?.element;
47+
}
5348
if (element == null) {
5449
return;
5550
}

pkg/analyzer/lib/src/dart/ast/ast.dart

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11447,6 +11447,26 @@ sealed class LiteralImpl extends ExpressionImpl implements Literal {
1144711447
Precedence get precedence => Precedence.primary;
1144811448
}
1144911449

11450+
// TODO(scheglov): parse into actual `LocalFunctionDeclarationImpl`.
11451+
class LocalFunctionDeclarationView {
11452+
final FunctionDeclarationImpl declaration;
11453+
11454+
LocalFunctionDeclarationView(this.declaration);
11455+
11456+
LocalFunctionElement? get declaredElement {
11457+
return declaration.declaredElement2;
11458+
}
11459+
11460+
static LocalFunctionDeclarationView? of(AstNode declaration) {
11461+
if (declaration is FunctionDeclarationImpl) {
11462+
if (declaration.parent is FunctionDeclarationStatement) {
11463+
return LocalFunctionDeclarationView(declaration);
11464+
}
11465+
}
11466+
return null;
11467+
}
11468+
}
11469+
1145011470
/// Additional information about local variables within a function or method
1145111471
/// produced at resolution time.
1145211472
class LocalVariableInfo {

0 commit comments

Comments
 (0)