Skip to content

Commit f11bd03

Browse files
DanTupCommit Queue
authored andcommitted
[analyzer] Update ElementLocator2 to handle both local + top level functions
I previously fixed this for variables, but functions are the same and can have elements but no fragments too. Change-Id: Ia1349cd1aca058da1155d7c867b5dc4384e3050f Reviewed-on: https://dart-review.googlesource.com/c/sdk/+/391603 Reviewed-by: Brian Wilkerson <[email protected]> Reviewed-by: Konstantin Shcheglov <[email protected]> Commit-Queue: Brian Wilkerson <[email protected]>
1 parent 06756f1 commit f11bd03

File tree

2 files changed

+13
-2
lines changed

2 files changed

+13
-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
@@ -401,7 +401,7 @@ class _ElementMapper2 extends GeneralizingAstVisitor<Element2> {
401401

402402
@override
403403
Element2? visitFunctionDeclaration(FunctionDeclaration node) {
404-
return node.declaredFragment?.element;
404+
return node.declaredElement2 ?? node.declaredFragment?.element;
405405
}
406406

407407
@override

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

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -586,7 +586,18 @@ enum E {
586586
expect(element, findElement2.extensionType('A'));
587587
}
588588

589-
test_locate_FunctionDeclaration() async {
589+
test_locate_FunctionDeclaration_local() async {
590+
await resolveTestCode(r'''
591+
void f() {
592+
int g() => 3;
593+
}
594+
''');
595+
var node = findNode.functionDeclaration('g');
596+
var element = ElementLocator.locate2(node);
597+
expect(element, isLocalFunctionElement);
598+
}
599+
600+
test_locate_FunctionDeclaration_topLevel() async {
590601
await resolveTestCode('int f() => 3;');
591602
var node = findNode.functionDeclaration('f');
592603
var element = ElementLocator.locate2(node);

0 commit comments

Comments
 (0)