Skip to content

Commit f270529

Browse files
authored
Fix object literals lack of this references (#43572)
* fix: object literals lack of this references * test: improve cases
1 parent 8513f78 commit f270529

File tree

2 files changed

+50
-10
lines changed

2 files changed

+50
-10
lines changed

src/services/findAllReferences.ts

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1929,6 +1929,8 @@ namespace ts.FindAllReferences {
19291929
case SyntaxKind.MethodDeclaration:
19301930
case SyntaxKind.MethodSignature:
19311931
if (isObjectLiteralMethod(searchSpaceNode)) {
1932+
staticFlag &= getSyntacticModifierFlags(searchSpaceNode);
1933+
searchSpaceNode = searchSpaceNode.parent; // re-assign to be the owning object literals
19321934
break;
19331935
}
19341936
// falls through
@@ -1970,7 +1972,8 @@ namespace ts.FindAllReferences {
19701972
return isObjectLiteralMethod(searchSpaceNode) && searchSpaceNode.symbol === container.symbol;
19711973
case SyntaxKind.ClassExpression:
19721974
case SyntaxKind.ClassDeclaration:
1973-
// Make sure the container belongs to the same class
1975+
case SyntaxKind.ObjectLiteralExpression:
1976+
// Make sure the container belongs to the same class/object literals
19741977
// and has the appropriate static modifier from the original container.
19751978
return container.parent && searchSpaceNode.symbol === container.parent.symbol && (getSyntacticModifierFlags(container) & ModifierFlags.Static) === staticFlag;
19761979
case SyntaxKind.SourceFile:

tests/cases/fourslash/getOccurrencesThis6.ts

Lines changed: 46 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -90,7 +90,7 @@
9090
//// }
9191
////
9292
//// public static staticB = this.staticMethod1;
93-
////
93+
////
9494
//// public static staticMethod1() {
9595
//// this;
9696
//// this;
@@ -133,23 +133,60 @@
133133
////}
134134
////
135135
////var x = {
136+
//// a: /*4*/this,
137+
////
136138
//// f() {
137-
//// this/*4*/;
139+
//// this/*5*/;
140+
//// function foo() {
141+
//// this;
142+
//// }
143+
//// const bar = () => {
144+
//// this;
145+
//// }
138146
//// },
147+
////
139148
//// g() {
140-
//// this/*5*/;
141-
//// }
142-
////}
149+
//// this;
150+
//// },
151+
////
152+
//// get h() {
153+
//// /*7*/this;
154+
//// function foo() {
155+
//// this;
156+
//// }
157+
//// const bar = () => {
158+
//// this;
159+
//// }
160+
//// return;
161+
//// },
162+
////
163+
//// set h(foo: any) {
164+
//// this;
165+
//// },
166+
////
167+
//// l: () => {
168+
//// /*8*/this;
169+
//// function foo() {
170+
//// this;
171+
//// }
172+
//// const bar = () => {
173+
//// this;
174+
//// }
175+
//// },
176+
////};
177+
////
143178

144179

145180
function verifyOccurrencesAtMarker(marker: string, count: number) {
146181
goTo.marker(marker);
147182
verify.occurrencesAtPositionCount(count);
148183
}
149184

150-
verifyOccurrencesAtMarker("1", 2);
185+
verifyOccurrencesAtMarker("1", 5);
151186
verifyOccurrencesAtMarker("2", 6);
152187
verifyOccurrencesAtMarker("3", 1);
153-
verifyOccurrencesAtMarker("4", 1);
154-
verifyOccurrencesAtMarker("5", 1);
155-
verifyOccurrencesAtMarker("6", 0);
188+
verifyOccurrencesAtMarker("4", 5);
189+
verifyOccurrencesAtMarker("5", 6);
190+
verifyOccurrencesAtMarker("6", 0);
191+
verifyOccurrencesAtMarker("7", 6);
192+
verifyOccurrencesAtMarker("8", 5);

0 commit comments

Comments
 (0)