Skip to content

Commit bf369f1

Browse files
authored
Fix missing reference in js (#50509) (#53000)
1 parent b7b0b52 commit bf369f1

File tree

3 files changed

+246
-1
lines changed

3 files changed

+246
-1
lines changed

src/compiler/checker.ts

Lines changed: 15 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -45149,6 +45149,20 @@ export function createTypeChecker(host: TypeCheckerHost): TypeChecker {
4514945149
return undefined;
4515045150
}
4515145151

45152+
function isThisPropertyAndThisTyped(node: PropertyAccessExpression) {
45153+
if (node.expression.kind === SyntaxKind.ThisKeyword) {
45154+
const container = getThisContainer(node, /*includeArrowFunctions*/ false, /*includeClassComputedPropertyName*/ false);
45155+
if (isFunctionLike(container)) {
45156+
const containingLiteral = getContainingObjectLiteral(container);
45157+
if (containingLiteral) {
45158+
const contextualType = getApparentTypeOfContextualType(containingLiteral, /*contextFlags*/ undefined);
45159+
const type = contextualType && getThisTypeFromContextualType(contextualType);
45160+
return type && !isTypeAny(type);
45161+
}
45162+
}
45163+
}
45164+
}
45165+
4515245166
function getSymbolOfNameOrPropertyAccessExpression(name: EntityName | PrivateIdentifier | PropertyAccessExpression | JSDocMemberName): Symbol | undefined {
4515345167
if (isDeclarationName(name)) {
4515445168
return getSymbolOfNode(name.parent);
@@ -45158,7 +45172,7 @@ export function createTypeChecker(host: TypeCheckerHost): TypeChecker {
4515845172
name.parent.kind === SyntaxKind.PropertyAccessExpression &&
4515945173
name.parent === (name.parent.parent as BinaryExpression).left) {
4516045174
// Check if this is a special property assignment
45161-
if (!isPrivateIdentifier(name) && !isJSDocMemberName(name)) {
45175+
if (!isPrivateIdentifier(name) && !isJSDocMemberName(name) && !isThisPropertyAndThisTyped(name.parent as PropertyAccessExpression)) {
4516245176
const specialPropertyAssignmentSymbol = getSpecialPropertyAssignmentSymbolFromEntityName(name);
4516345177
if (specialPropertyAssignmentSymbol) {
4516445178
return specialPropertyAssignmentSymbol;
Lines changed: 200 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,200 @@
1+
// === /tests/cases/fourslash/infer.d.ts ===
2+
// export declare function infer(o: { m(): void } & ThisType<{ [|x|]: number }>): void;
3+
4+
// === /tests/cases/fourslash/a.js ===
5+
// import { infer } from "./infer";
6+
// infer({
7+
// m() {
8+
// this.[|x|] = 1;
9+
// this./*FIND ALL REFS*/[|x|];
10+
// },
11+
// });
12+
13+
[
14+
{
15+
"definition": {
16+
"containerKind": "",
17+
"containerName": "",
18+
"fileName": "/tests/cases/fourslash/infer.d.ts",
19+
"kind": "property",
20+
"name": "(property) x: number",
21+
"textSpan": {
22+
"start": 60,
23+
"length": 1
24+
},
25+
"displayParts": [
26+
{
27+
"text": "(",
28+
"kind": "punctuation"
29+
},
30+
{
31+
"text": "property",
32+
"kind": "text"
33+
},
34+
{
35+
"text": ")",
36+
"kind": "punctuation"
37+
},
38+
{
39+
"text": " ",
40+
"kind": "space"
41+
},
42+
{
43+
"text": "x",
44+
"kind": "propertyName"
45+
},
46+
{
47+
"text": ":",
48+
"kind": "punctuation"
49+
},
50+
{
51+
"text": " ",
52+
"kind": "space"
53+
},
54+
{
55+
"text": "number",
56+
"kind": "keyword"
57+
}
58+
],
59+
"contextSpan": {
60+
"start": 60,
61+
"length": 9
62+
}
63+
},
64+
"references": [
65+
{
66+
"textSpan": {
67+
"start": 60,
68+
"length": 1
69+
},
70+
"fileName": "/tests/cases/fourslash/infer.d.ts",
71+
"contextSpan": {
72+
"start": 60,
73+
"length": 9
74+
},
75+
"isWriteAccess": true
76+
},
77+
{
78+
"textSpan": {
79+
"start": 64,
80+
"length": 1
81+
},
82+
"fileName": "/tests/cases/fourslash/a.js",
83+
"contextSpan": {
84+
"start": 59,
85+
"length": 11
86+
},
87+
"isWriteAccess": true
88+
},
89+
{
90+
"textSpan": {
91+
"start": 84,
92+
"length": 1
93+
},
94+
"fileName": "/tests/cases/fourslash/a.js",
95+
"isWriteAccess": false
96+
}
97+
]
98+
}
99+
]
100+
101+
// === /tests/cases/fourslash/b.js ===
102+
// /**
103+
// * @template T
104+
// * @param {{m(): void} & ThisType<{[|x|]: number}>} o
105+
// */
106+
// function infer(o) {}
107+
// infer({
108+
// m() {
109+
// this.[|x|] = 2;
110+
// this./*FIND ALL REFS*/[|x|];
111+
// },
112+
// });
113+
114+
[
115+
{
116+
"definition": {
117+
"containerKind": "",
118+
"containerName": "",
119+
"fileName": "/tests/cases/fourslash/b.js",
120+
"kind": "property",
121+
"name": "(property) x: number",
122+
"textSpan": {
123+
"start": 54,
124+
"length": 1
125+
},
126+
"displayParts": [
127+
{
128+
"text": "(",
129+
"kind": "punctuation"
130+
},
131+
{
132+
"text": "property",
133+
"kind": "text"
134+
},
135+
{
136+
"text": ")",
137+
"kind": "punctuation"
138+
},
139+
{
140+
"text": " ",
141+
"kind": "space"
142+
},
143+
{
144+
"text": "x",
145+
"kind": "propertyName"
146+
},
147+
{
148+
"text": ":",
149+
"kind": "punctuation"
150+
},
151+
{
152+
"text": " ",
153+
"kind": "space"
154+
},
155+
{
156+
"text": "number",
157+
"kind": "keyword"
158+
}
159+
],
160+
"contextSpan": {
161+
"start": 54,
162+
"length": 9
163+
}
164+
},
165+
"references": [
166+
{
167+
"textSpan": {
168+
"start": 54,
169+
"length": 1
170+
},
171+
"fileName": "/tests/cases/fourslash/b.js",
172+
"contextSpan": {
173+
"start": 54,
174+
"length": 9
175+
},
176+
"isWriteAccess": false
177+
},
178+
{
179+
"textSpan": {
180+
"start": 125,
181+
"length": 1
182+
},
183+
"fileName": "/tests/cases/fourslash/b.js",
184+
"contextSpan": {
185+
"start": 120,
186+
"length": 11
187+
},
188+
"isWriteAccess": true
189+
},
190+
{
191+
"textSpan": {
192+
"start": 145,
193+
"length": 1
194+
},
195+
"fileName": "/tests/cases/fourslash/b.js",
196+
"isWriteAccess": false
197+
}
198+
]
199+
}
200+
]
Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
/// <reference path="fourslash.ts" />
2+
3+
// @allowJs: true
4+
// @noImplicitThis: true
5+
6+
// @Filename: infer.d.ts
7+
//// export declare function infer(o: { m(): void } & ThisType<{ x: number }>): void;
8+
9+
// @Filename: a.js
10+
//// import { infer } from "./infer";
11+
//// infer({
12+
//// m() {
13+
//// this.x = 1;
14+
//// this./*1*/x;
15+
//// },
16+
//// });
17+
18+
// @Filename: b.js
19+
//// /**
20+
//// * @template T
21+
//// * @param {{m(): void} & ThisType<{x: number}>} o
22+
//// */
23+
//// function infer(o) {}
24+
//// infer({
25+
//// m() {
26+
//// this.x = 2;
27+
//// this./*2*/x;
28+
//// },
29+
//// });
30+
31+
verify.baselineFindAllReferences("1", "2");

0 commit comments

Comments
 (0)