Skip to content

Fix object literals lack of this references #43572

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
Apr 20, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 4 additions & 1 deletion src/services/findAllReferences.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1924,6 +1924,8 @@ namespace ts.FindAllReferences {
case SyntaxKind.MethodDeclaration:
case SyntaxKind.MethodSignature:
if (isObjectLiteralMethod(searchSpaceNode)) {
staticFlag &= getSyntacticModifierFlags(searchSpaceNode);
searchSpaceNode = searchSpaceNode.parent; // re-assign to be the owning object literals
break;
}
// falls through
Expand Down Expand Up @@ -1965,7 +1967,8 @@ namespace ts.FindAllReferences {
return isObjectLiteralMethod(searchSpaceNode) && searchSpaceNode.symbol === container.symbol;
case SyntaxKind.ClassExpression:
case SyntaxKind.ClassDeclaration:
// Make sure the container belongs to the same class
case SyntaxKind.ObjectLiteralExpression:
// Make sure the container belongs to the same class/object literals
// and has the appropriate static modifier from the original container.
return container.parent && searchSpaceNode.symbol === container.parent.symbol && (getSyntacticModifierFlags(container) & ModifierFlags.Static) === staticFlag;
case SyntaxKind.SourceFile:
Expand Down
55 changes: 46 additions & 9 deletions tests/cases/fourslash/getOccurrencesThis6.ts
Original file line number Diff line number Diff line change
Expand Up @@ -90,7 +90,7 @@
//// }
////
//// public static staticB = this.staticMethod1;
////
////
//// public static staticMethod1() {
//// this;
//// this;
Expand Down Expand Up @@ -133,23 +133,60 @@
////}
////
////var x = {
//// a: /*4*/this,
////
//// f() {
//// this/*4*/;
//// this/*5*/;
//// function foo() {
//// this;
//// }
//// const bar = () => {
//// this;
//// }
//// },
////
//// g() {
//// this/*5*/;
//// }
////}
//// this;
//// },
////
//// get h() {
//// /*7*/this;
//// function foo() {
//// this;
//// }
//// const bar = () => {
//// this;
//// }
//// return;
//// },
////
//// set h(foo: any) {
//// this;
//// },
////
//// l: () => {
//// /*8*/this;
//// function foo() {
//// this;
//// }
//// const bar = () => {
//// this;
//// }
//// },
////};
////


function verifyOccurrencesAtMarker(marker: string, count: number) {
goTo.marker(marker);
verify.occurrencesAtPositionCount(count);
}

verifyOccurrencesAtMarker("1", 2);
verifyOccurrencesAtMarker("1", 5);
verifyOccurrencesAtMarker("2", 6);
verifyOccurrencesAtMarker("3", 1);
verifyOccurrencesAtMarker("4", 1);
verifyOccurrencesAtMarker("5", 1);
verifyOccurrencesAtMarker("6", 0);
verifyOccurrencesAtMarker("4", 5);
verifyOccurrencesAtMarker("5", 6);
verifyOccurrencesAtMarker("6", 0);
verifyOccurrencesAtMarker("7", 6);
verifyOccurrencesAtMarker("8", 5);