Fix truthiness call check for this-property access (#38163)

This commit is contained in:
Andrew Branch 2020-04-24 13:02:17 -07:00 committed by GitHub
parent 38ff7762ec
commit fe140acc09
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
6 changed files with 47 additions and 1 deletions

View file

@ -31909,7 +31909,9 @@ namespace ts {
let childExpression = childNode.parent;
while (testedExpression && childExpression) {
if (isIdentifier(testedExpression) && isIdentifier(childExpression)) {
if (isIdentifier(testedExpression) && isIdentifier(childExpression) ||
testedExpression.kind === SyntaxKind.ThisKeyword && childExpression.kind === SyntaxKind.ThisKeyword
) {
return getSymbolAtLocation(testedExpression) === getSymbolAtLocation(childExpression);
}

View file

@ -80,6 +80,11 @@ tests/cases/compiler/truthinessCallExpressionCoercion1.ts(61,9): error TS2774: T
// ok
this.maybeIsUser ? console.log('this.maybeIsUser') : undefined;
// ok
if (this.isUser) {
this.isUser();
}
}
}

View file

@ -63,6 +63,11 @@ class Foo {
// ok
this.maybeIsUser ? console.log('this.maybeIsUser') : undefined;
// ok
if (this.isUser) {
this.isUser();
}
}
}
@ -117,6 +122,10 @@ var Foo = /** @class */ (function () {
this.isUser ? console.log('this.isUser') : undefined;
// ok
this.maybeIsUser ? console.log('this.maybeIsUser') : undefined;
// ok
if (this.isUser) {
this.isUser();
}
};
return Foo;
}());

View file

@ -170,6 +170,18 @@ class Foo {
>console : Symbol(console, Decl(lib.dom.d.ts, --, --))
>log : Symbol(Console.log, Decl(lib.dom.d.ts, --, --))
>undefined : Symbol(undefined)
// ok
if (this.isUser) {
>this.isUser : Symbol(Foo.isUser, Decl(truthinessCallExpressionCoercion1.ts, 52, 32))
>this : Symbol(Foo, Decl(truthinessCallExpressionCoercion1.ts, 49, 1))
>isUser : Symbol(Foo.isUser, Decl(truthinessCallExpressionCoercion1.ts, 52, 32))
this.isUser();
>this.isUser : Symbol(Foo.isUser, Decl(truthinessCallExpressionCoercion1.ts, 52, 32))
>this : Symbol(Foo, Decl(truthinessCallExpressionCoercion1.ts, 49, 1))
>isUser : Symbol(Foo.isUser, Decl(truthinessCallExpressionCoercion1.ts, 52, 32))
}
}
}

View file

@ -223,6 +223,19 @@ class Foo {
>log : (...data: any[]) => void
>'this.maybeIsUser' : "this.maybeIsUser"
>undefined : undefined
// ok
if (this.isUser) {
>this.isUser : () => boolean
>this : this
>isUser : () => boolean
this.isUser();
>this.isUser() : boolean
>this.isUser : () => boolean
>this : this
>isUser : () => boolean
}
}
}

View file

@ -64,5 +64,10 @@ class Foo {
// ok
this.maybeIsUser ? console.log('this.maybeIsUser') : undefined;
// ok
if (this.isUser) {
this.isUser();
}
}
}