Skip to content

Commit fe140ac

Browse files
authored
Fix truthiness call check for this-property access (#38163)
1 parent 38ff776 commit fe140ac

File tree

6 files changed

+47
-1
lines changed

6 files changed

+47
-1
lines changed

src/compiler/checker.ts

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -31909,7 +31909,9 @@ namespace ts {
3190931909
let childExpression = childNode.parent;
3191031910
while (testedExpression && childExpression) {
3191131911

31912-
if (isIdentifier(testedExpression) && isIdentifier(childExpression)) {
31912+
if (isIdentifier(testedExpression) && isIdentifier(childExpression) ||
31913+
testedExpression.kind === SyntaxKind.ThisKeyword && childExpression.kind === SyntaxKind.ThisKeyword
31914+
) {
3191331915
return getSymbolAtLocation(testedExpression) === getSymbolAtLocation(childExpression);
3191431916
}
3191531917

tests/baselines/reference/truthinessCallExpressionCoercion1.errors.txt

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -80,6 +80,11 @@ tests/cases/compiler/truthinessCallExpressionCoercion1.ts(61,9): error TS2774: T
8080

8181
// ok
8282
this.maybeIsUser ? console.log('this.maybeIsUser') : undefined;
83+
84+
// ok
85+
if (this.isUser) {
86+
this.isUser();
87+
}
8388
}
8489
}
8590

tests/baselines/reference/truthinessCallExpressionCoercion1.js

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -63,6 +63,11 @@ class Foo {
6363

6464
// ok
6565
this.maybeIsUser ? console.log('this.maybeIsUser') : undefined;
66+
67+
// ok
68+
if (this.isUser) {
69+
this.isUser();
70+
}
6671
}
6772
}
6873

@@ -117,6 +122,10 @@ var Foo = /** @class */ (function () {
117122
this.isUser ? console.log('this.isUser') : undefined;
118123
// ok
119124
this.maybeIsUser ? console.log('this.maybeIsUser') : undefined;
125+
// ok
126+
if (this.isUser) {
127+
this.isUser();
128+
}
120129
};
121130
return Foo;
122131
}());

tests/baselines/reference/truthinessCallExpressionCoercion1.symbols

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -170,6 +170,18 @@ class Foo {
170170
>console : Symbol(console, Decl(lib.dom.d.ts, --, --))
171171
>log : Symbol(Console.log, Decl(lib.dom.d.ts, --, --))
172172
>undefined : Symbol(undefined)
173+
174+
// ok
175+
if (this.isUser) {
176+
>this.isUser : Symbol(Foo.isUser, Decl(truthinessCallExpressionCoercion1.ts, 52, 32))
177+
>this : Symbol(Foo, Decl(truthinessCallExpressionCoercion1.ts, 49, 1))
178+
>isUser : Symbol(Foo.isUser, Decl(truthinessCallExpressionCoercion1.ts, 52, 32))
179+
180+
this.isUser();
181+
>this.isUser : Symbol(Foo.isUser, Decl(truthinessCallExpressionCoercion1.ts, 52, 32))
182+
>this : Symbol(Foo, Decl(truthinessCallExpressionCoercion1.ts, 49, 1))
183+
>isUser : Symbol(Foo.isUser, Decl(truthinessCallExpressionCoercion1.ts, 52, 32))
184+
}
173185
}
174186
}
175187

tests/baselines/reference/truthinessCallExpressionCoercion1.types

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -223,6 +223,19 @@ class Foo {
223223
>log : (...data: any[]) => void
224224
>'this.maybeIsUser' : "this.maybeIsUser"
225225
>undefined : undefined
226+
227+
// ok
228+
if (this.isUser) {
229+
>this.isUser : () => boolean
230+
>this : this
231+
>isUser : () => boolean
232+
233+
this.isUser();
234+
>this.isUser() : boolean
235+
>this.isUser : () => boolean
236+
>this : this
237+
>isUser : () => boolean
238+
}
226239
}
227240
}
228241

tests/cases/compiler/truthinessCallExpressionCoercion1.ts

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -64,5 +64,10 @@ class Foo {
6464

6565
// ok
6666
this.maybeIsUser ? console.log('this.maybeIsUser') : undefined;
67+
68+
// ok
69+
if (this.isUser) {
70+
this.isUser();
71+
}
6772
}
6873
}

0 commit comments

Comments
 (0)