Skip to content

Enable CFA on this keyword unconditionally #21490

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 1 commit into from
Feb 2, 2018
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
6 changes: 3 additions & 3 deletions src/compiler/checker.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13317,13 +13317,13 @@ namespace ts {
.expression; // x
const classSymbol = checkExpression(className).symbol;
if (classSymbol && classSymbol.members && (classSymbol.flags & SymbolFlags.Function)) {
return getInferredClassType(classSymbol);
return getFlowTypeOfReference(node, getInferredClassType(classSymbol));
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can you find out if there’s a performance hit for this PR? I would expect this line especially to be costly, since there are probably a lot of this.property references in an OO codebase.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Never mind, I looked the file locally, outside github’s 3 lines of context, and see that this is for JS.

}
}

const thisType = getThisTypeOfDeclaration(container) || getContextualThisParameterType(container);
if (thisType) {
return thisType;
return getFlowTypeOfReference(node, thisType);
}
}

Expand All @@ -13336,7 +13336,7 @@ namespace ts {
if (isInJavaScriptFile(node)) {
const type = getTypeForThisExpressionFromJSDoc(container);
if (type && type !== unknownType) {
return type;
return getFlowTypeOfReference(node, type);
}
}
}
Expand Down
26 changes: 26 additions & 0 deletions tests/baselines/reference/controlFlowAnalysisOnBareThisKeyword.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
//// [controlFlowAnalysisOnBareThisKeyword.ts]
declare function isBig(x: any): x is { big: true };
function bigger(this: {}) {
if (isBig(this)) {
this.big; // Expect property to exist
}
}

function bar(this: string | number) {
if (typeof this === "string") {
const x: string = this;
}
}

//// [controlFlowAnalysisOnBareThisKeyword.js]
"use strict";
function bigger() {
if (isBig(this)) {
this.big; // Expect property to exist
}
}
function bar() {
if (typeof this === "string") {
var x = this;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
=== tests/cases/compiler/controlFlowAnalysisOnBareThisKeyword.ts ===
declare function isBig(x: any): x is { big: true };
>isBig : Symbol(isBig, Decl(controlFlowAnalysisOnBareThisKeyword.ts, 0, 0))
>x : Symbol(x, Decl(controlFlowAnalysisOnBareThisKeyword.ts, 0, 23))
>x : Symbol(x, Decl(controlFlowAnalysisOnBareThisKeyword.ts, 0, 23))
>big : Symbol(big, Decl(controlFlowAnalysisOnBareThisKeyword.ts, 0, 38))

function bigger(this: {}) {
>bigger : Symbol(bigger, Decl(controlFlowAnalysisOnBareThisKeyword.ts, 0, 51))
>this : Symbol(this, Decl(controlFlowAnalysisOnBareThisKeyword.ts, 1, 16))

if (isBig(this)) {
>isBig : Symbol(isBig, Decl(controlFlowAnalysisOnBareThisKeyword.ts, 0, 0))
>this : Symbol(this, Decl(controlFlowAnalysisOnBareThisKeyword.ts, 1, 16))

this.big; // Expect property to exist
>this.big : Symbol(big, Decl(controlFlowAnalysisOnBareThisKeyword.ts, 0, 38))
>this : Symbol(this, Decl(controlFlowAnalysisOnBareThisKeyword.ts, 1, 16))
>big : Symbol(big, Decl(controlFlowAnalysisOnBareThisKeyword.ts, 0, 38))
}
}

function bar(this: string | number) {
>bar : Symbol(bar, Decl(controlFlowAnalysisOnBareThisKeyword.ts, 5, 1))
>this : Symbol(this, Decl(controlFlowAnalysisOnBareThisKeyword.ts, 7, 13))

if (typeof this === "string") {
>this : Symbol(this, Decl(controlFlowAnalysisOnBareThisKeyword.ts, 7, 13))

const x: string = this;
>x : Symbol(x, Decl(controlFlowAnalysisOnBareThisKeyword.ts, 9, 13))
>this : Symbol(this, Decl(controlFlowAnalysisOnBareThisKeyword.ts, 7, 13))
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
=== tests/cases/compiler/controlFlowAnalysisOnBareThisKeyword.ts ===
declare function isBig(x: any): x is { big: true };
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Shouldn’t there be way more type differences than this, since we are now narrowing this in classes? Maybe I’m missing something about the actual change.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

[Later] Yes, I did. But github doesn’t display the “Are you sure you want to delete your comment?” message on the iPad, so I can’t delete it.

>isBig : (x: any) => x is { big: true; }
>x : any
>x : any
>big : true
>true : true

function bigger(this: {}) {
>bigger : (this: {}) => void
>this : {}

if (isBig(this)) {
>isBig(this) : boolean
>isBig : (x: any) => x is { big: true; }
>this : {}

this.big; // Expect property to exist
>this.big : true
>this : { big: true; }
>big : true
}
}

function bar(this: string | number) {
>bar : (this: string | number) => void
>this : string | number

if (typeof this === "string") {
>typeof this === "string" : boolean
>typeof this : "string" | "number" | "boolean" | "symbol" | "undefined" | "object" | "function"
>this : string | number
>"string" : "string"

const x: string = this;
>x : string
>this : string
}
}
13 changes: 13 additions & 0 deletions tests/cases/compiler/controlFlowAnalysisOnBareThisKeyword.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
// @strict: true
declare function isBig(x: any): x is { big: true };
function bigger(this: {}) {
if (isBig(this)) {
this.big; // Expect property to exist
}
}

function bar(this: string | number) {
if (typeof this === "string") {
const x: string = this;
}
}