Skip to content

Commit 03515b4

Browse files
kallentuCommit Queue
authored andcommitted
[analyzer] Dot shorthands: Update indexing.
Adds implementation for dot shorthand invocations, property accesses and constructors in indexing and added search tests. Unit tests passing for index and search. Bug: #59835 Change-Id: I2ee3ddbabb7f8ee0ead6538906a9a55f54bf20a2 Reviewed-on: https://dart-review.googlesource.com/c/sdk/+/430563 Commit-Queue: Kallen Tu <[email protected]> Reviewed-by: Konstantin Shcheglov <[email protected]> Reviewed-by: Chloe Stefantsova <[email protected]>
1 parent 15f49c9 commit 03515b4

File tree

3 files changed

+130
-0
lines changed

3 files changed

+130
-0
lines changed

pkg/analyzer/lib/src/dart/analysis/index.dart

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -860,6 +860,41 @@ class _IndexContributor extends GeneralizingAstVisitor {
860860
node.type.accept(this);
861861
}
862862

863+
@override
864+
void visitDotShorthandConstructorInvocation(
865+
DotShorthandConstructorInvocation node,
866+
) {
867+
var element = _getActualConstructorElement(node.element?.baseElement);
868+
recordRelation(
869+
element,
870+
IndexRelationKind.IS_INVOKED_BY,
871+
node.constructorName,
872+
true,
873+
);
874+
}
875+
876+
@override
877+
void visitDotShorthandInvocation(DotShorthandInvocation node) {
878+
var name = node.memberName;
879+
var element = name.element;
880+
recordRelation(element, IndexRelationKind.IS_INVOKED_BY, name, true);
881+
node.typeArguments?.accept(this);
882+
node.argumentList.accept(this);
883+
}
884+
885+
@override
886+
void visitDotShorthandPropertyAccess(DotShorthandPropertyAccess node) {
887+
IndexRelationKind kind;
888+
var element = node.propertyName.element;
889+
if (element is ConstructorElementMixin2) {
890+
element = _getActualConstructorElement(element);
891+
kind = IndexRelationKind.IS_REFERENCED_BY_CONSTRUCTOR_TEAR_OFF;
892+
} else {
893+
kind = IndexRelationKind.IS_REFERENCED_BY;
894+
}
895+
recordRelation(element, kind, node.propertyName, true);
896+
}
897+
863898
@override
864899
void visitEnumConstantDeclaration(EnumConstantDeclaration node) {
865900
var constructorElement = node.constructorElement2;

pkg/analyzer/test/src/dart/analysis/index_test.dart

Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1280,6 +1280,21 @@ void f() {
12801280
// No additional validation, but it should not fail with stack overflow.
12811281
}
12821282

1283+
test_isReferencedBy_ConstructorElement_dotShorthand() async {
1284+
await _indexTestUnit('''
1285+
class A {}
1286+
void f() {
1287+
A a = .new(); // 1
1288+
A tearOff = .new; // 2, is also a compile-time error
1289+
}
1290+
''');
1291+
var element = findElement2.unnamedConstructor('A');
1292+
assertElementIndexText(element, r'''
1293+
31 3:10 |new| IS_INVOKED_BY qualified
1294+
58 4:16 |new| IS_REFERENCED_BY_CONSTRUCTOR_TEAR_OFF qualified
1295+
''');
1296+
}
1297+
12831298
test_isReferencedBy_ConstructorElement_enum_named() async {
12841299
await _indexTestUnit('''
12851300
/// [new E.foo] 1
@@ -1706,6 +1721,21 @@ class A {
17061721
''');
17071722
}
17081723

1724+
test_isReferencedBy_FieldElement_dotShorthand() async {
1725+
await _indexTestUnit('''
1726+
class A {
1727+
static A field = A();
1728+
}
1729+
void f() {
1730+
A a = .field; // 1
1731+
}
1732+
''');
1733+
var element = findElement2.field('field').getter2!;
1734+
assertElementIndexText(element, r'''
1735+
56 5:10 |field| IS_REFERENCED_BY qualified
1736+
''');
1737+
}
1738+
17091739
test_isReferencedBy_FieldElement_enum() async {
17101740
await _indexTestUnit('''
17111741
enum E {
@@ -1953,6 +1983,21 @@ class A {
19531983
''');
19541984
}
19551985

1986+
test_isReferencedBy_MethodElement_dotShorthand() async {
1987+
await _indexTestUnit('''
1988+
class A {
1989+
static A method() => A();
1990+
}
1991+
void f() {
1992+
A a = .method(); // 1
1993+
}
1994+
''');
1995+
var element = findElement2.method('method');
1996+
assertElementIndexText(element, r'''
1997+
60 5:10 |method| IS_INVOKED_BY qualified
1998+
''');
1999+
}
2000+
19562001
test_isReferencedBy_MethodElement_enum() async {
19572002
await _indexTestUnit('''
19582003
enum E {

pkg/analyzer/test/src/dart/analysis/search_test.dart

Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1304,6 +1304,22 @@ void f() {
13041304
''');
13051305
}
13061306

1307+
test_searchReferences_ConstructorElement_dotShorthand() async {
1308+
await resolveTestCode('''
1309+
class A {}
1310+
void main() {
1311+
A a = .new(); // 1
1312+
A tearOff = .new; // 2, is also a compile-time error
1313+
}
1314+
''');
1315+
var element = findElement2.unnamedConstructor('A');
1316+
await assertElementReferencesText(element, r'''
1317+
<testLibraryFragment>::@function::main
1318+
34 3:10 |new| INVOCATION qualified
1319+
61 4:16 |new| REFERENCE_BY_CONSTRUCTOR_TEAR_OFF qualified
1320+
''');
1321+
}
1322+
13071323
test_searchReferences_ConstructorElement_enum_named() async {
13081324
await resolveTestCode('''
13091325
/// [new E.named] 1
@@ -1496,6 +1512,22 @@ class A {
14961512
''');
14971513
}
14981514

1515+
test_searchReferences_FieldElement_dotShorthand() async {
1516+
await resolveTestCode('''
1517+
class A {
1518+
static A field = A();
1519+
}
1520+
void main() {
1521+
A a = .field; // 1
1522+
}
1523+
''');
1524+
var element = findElement2.field('field');
1525+
await assertElementReferencesText(element, r'''
1526+
<testLibraryFragment>::@function::main
1527+
59 5:10 |field| READ qualified
1528+
''');
1529+
}
1530+
14991531
test_searchReferences_FieldElement_enum() async {
15001532
await resolveTestCode('''
15011533
enum E {
@@ -1938,6 +1970,24 @@ class A {
19381970
''');
19391971
}
19401972

1973+
test_searchReferences_MethodElement_dotShorthand() async {
1974+
await resolveTestCode('''
1975+
class A {
1976+
static A method() => A();
1977+
}
1978+
void main() {
1979+
A a = .method(); // 1
1980+
A aa = .method; // 2, is also a compile-time error
1981+
}
1982+
''');
1983+
var element = findElement2.method('method');
1984+
await assertElementReferencesText(element, r'''
1985+
<testLibraryFragment>::@function::main
1986+
63 5:10 |method| INVOCATION qualified
1987+
88 6:11 |method| REFERENCE qualified
1988+
''');
1989+
}
1990+
19411991
test_searchReferences_MethodElement_enum() async {
19421992
await resolveTestCode('''
19431993
enum E {

0 commit comments

Comments
 (0)