Skip to content

Commit bff7817

Browse files
DanTupCommit Queue
authored andcommitted
[analyzer] Update ElementLocator2 to handle both local + top level variables
Change-Id: I3f84a381c4c103d478e74a28d7ebbe85ee46606c Reviewed-on: https://dart-review.googlesource.com/c/sdk/+/391441 Reviewed-by: Brian Wilkerson <[email protected]> Commit-Queue: Brian Wilkerson <[email protected]> Reviewed-by: Konstantin Shcheglov <[email protected]>
1 parent 9062cf4 commit bff7817

File tree

3 files changed

+15
-2
lines changed

3 files changed

+15
-2
lines changed

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -559,6 +559,6 @@ class _ElementMapper2 extends GeneralizingAstVisitor<Element2> {
559559

560560
@override
561561
Element2? visitVariableDeclaration(VariableDeclaration node) {
562-
return node.declaredFragment?.element;
562+
return node.declaredFragment?.element ?? node.declaredElement2;
563563
}
564564
}

pkg/analyzer/test/src/dart/ast/element_locator_test.dart

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -884,7 +884,18 @@ void f(int a) {
884884
expect(element, isLibraryElement2);
885885
}
886886

887-
test_locate_VariableDeclaration() async {
887+
test_locate_VariableDeclaration_Local() async {
888+
await resolveTestCode(r'''
889+
f() {
890+
var x = 42;
891+
}
892+
''');
893+
var node = findNode.variableDeclaration('x =');
894+
var element = ElementLocator.locate2(node);
895+
expect(element, isLocalVariableElement2);
896+
}
897+
898+
test_locate_VariableDeclaration_TopLevel() async {
888899
await resolveTestCode('var x = 42;');
889900
var node = findNode.variableDeclaration('x =');
890901
var element = ElementLocator.locate2(node);

pkg/analyzer/test/util/element_type_matchers.dart

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -53,6 +53,8 @@ const isLocalFunctionElement = TypeMatcher<LocalFunctionElement>();
5353

5454
const isLocalVariableElement = TypeMatcher<LocalVariableElement>();
5555

56+
const isLocalVariableElement2 = TypeMatcher<LocalVariableElement2>();
57+
5658
const isMethodElement = TypeMatcher<MethodElement>();
5759

5860
const isMethodElement2 = TypeMatcher<MethodElement2>();

0 commit comments

Comments
 (0)