Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Appearance settings

Commit fe140ac

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

6 files changed

+47-1Lines changed: 47 additions & 1 deletion
Expand file treeCollapse file tree
Open diff view settings
Collapse file

‎src/compiler/checker.ts‎

Copy file name to clipboardExpand all lines: src/compiler/checker.ts
+3-1Lines 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

Collapse file

‎tests/baselines/reference/truthinessCallExpressionCoercion1.errors.txt‎

Copy file name to clipboardExpand all lines: tests/baselines/reference/truthinessCallExpressionCoercion1.errors.txt
+5Lines 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

Collapse file

‎tests/baselines/reference/truthinessCallExpressionCoercion1.js‎

Copy file name to clipboardExpand all lines: tests/baselines/reference/truthinessCallExpressionCoercion1.js
+9Lines 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
}());
Collapse file

‎tests/baselines/reference/truthinessCallExpressionCoercion1.symbols‎

Copy file name to clipboardExpand all lines: tests/baselines/reference/truthinessCallExpressionCoercion1.symbols
+12Lines 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

Collapse file

‎tests/baselines/reference/truthinessCallExpressionCoercion1.types‎

Copy file name to clipboardExpand all lines: tests/baselines/reference/truthinessCallExpressionCoercion1.types
+13Lines 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

Collapse file

‎tests/cases/compiler/truthinessCallExpressionCoercion1.ts‎

Copy file name to clipboardExpand all lines: tests/cases/compiler/truthinessCallExpressionCoercion1.ts
+5Lines 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 (0)
Morty Proxy This is a proxified and sanitized view of the page, visit original site.