Skip to content

Allow nested assignments in type guards #9013

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 2 commits into from
Jun 8, 2016
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
28 changes: 22 additions & 6 deletions src/compiler/checker.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7662,6 +7662,21 @@ namespace ts {
getInitialTypeOfBindingElement(<BindingElement>node);
}

function getReferenceFromExpression(node: Expression): Expression {
switch (node.kind) {
case SyntaxKind.ParenthesizedExpression:
return getReferenceFromExpression((<ParenthesizedExpression>node).expression);
case SyntaxKind.BinaryExpression:
switch ((<BinaryExpression>node).operatorToken.kind) {
case SyntaxKind.EqualsToken:
Copy link
Member

Choose a reason for hiding this comment

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

Technically you should consider the compound assignment operators here as well (e.g. +=, -= etc)

Copy link
Member Author

Choose a reason for hiding this comment

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

Compound operators aren't included in control flow analysis in our current design, so that would be a separate issue. But as I see it they wouldn't affect the type because they both a read and a write operation (you're basically storing a value of the same type back into the variable). Did you have something in mind?

Copy link
Member

@DanielRosenwasser DanielRosenwasser Jun 8, 2016

Choose a reason for hiding this comment

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

Nothing other than odd cases like this:

var x: number | string = getNumberOrString();
x += "hello";
x; // currently has type 'string | number'

whereas in this case you get a different result

var x: number | string = getNumberOrString();
x = x + "hello";
x; // currently has type 'string'

I don't know how often something like that comes up in practice though.

return getReferenceFromExpression((<BinaryExpression>node).left);
case SyntaxKind.CommaToken:
return getReferenceFromExpression((<BinaryExpression>node).right);
}
}
return node;
}

function getFlowTypeOfReference(reference: Node, declaredType: Type, assumeInitialized: boolean, includeOuterFunctions: boolean) {
let key: string;
if (!reference.flowNode || assumeInitialized && !(declaredType.flags & TypeFlags.Narrowable)) {
Expand Down Expand Up @@ -7882,7 +7897,7 @@ namespace ts {
if (operator === SyntaxKind.ExclamationEqualsToken || operator === SyntaxKind.ExclamationEqualsEqualsToken) {
assumeTrue = !assumeTrue;
}
if (!strictNullChecks || !isMatchingReference(reference, expr.left)) {
if (!strictNullChecks || !isMatchingReference(reference, getReferenceFromExpression(expr.left))) {
return type;
}
const doubleEquals = operator === SyntaxKind.EqualsEqualsToken || operator === SyntaxKind.ExclamationEqualsToken;
Expand All @@ -7897,12 +7912,12 @@ namespace ts {
function narrowTypeByTypeof(type: Type, expr: BinaryExpression, assumeTrue: boolean): Type {
// We have '==', '!=', '====', or !==' operator with 'typeof xxx' on the left
// and string literal on the right
const left = <TypeOfExpression>expr.left;
const left = getReferenceFromExpression((<TypeOfExpression>expr.left).expression);
const right = <LiteralExpression>expr.right;
if (!isMatchingReference(reference, left.expression)) {
if (!isMatchingReference(reference, left)) {
// For a reference of the form 'x.y', a 'typeof x === ...' type guard resets the
// narrowed type of 'y' to its declared type.
if (containsMatchingReference(reference, left.expression)) {
if (containsMatchingReference(reference, left)) {
return declaredType;
}
return type;
Expand All @@ -7927,10 +7942,11 @@ namespace ts {
}

function narrowTypeByInstanceof(type: Type, expr: BinaryExpression, assumeTrue: boolean): Type {
if (!isMatchingReference(reference, expr.left)) {
const left = getReferenceFromExpression(expr.left);
if (!isMatchingReference(reference, left)) {
// For a reference of the form 'x.y', an 'x instanceof T' type guard resets the
// narrowed type of 'y' to its declared type.
if (containsMatchingReference(reference, expr.left)) {
if (containsMatchingReference(reference, left)) {
return declaredType;
}
return type;
Expand Down
86 changes: 86 additions & 0 deletions tests/baselines/reference/typeGuardsNestedAssignments.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,86 @@
//// [typeGuardsNestedAssignments.ts]

class Foo {
x: string;
}

declare function getFooOrNull(): Foo | null;
declare function getStringOrNumberOrNull(): string | number | null;

function f1() {
let foo: Foo | null;
if ((foo = getFooOrNull()) !== null) {
foo; // Foo
}
}

function f2() {
let foo1: Foo | null;
let foo2: Foo | null;
if ((foo1 = getFooOrNull(), foo2 = foo1) !== null) {
foo1; // Foo | null
foo2; // Foo
}
}

function f3() {
let obj: Object | null;
if ((obj = getFooOrNull()) instanceof Foo) {
obj;
}
}

function f4() {
let x: string | number | null;
if (typeof (x = getStringOrNumberOrNull()) === "number") {
x;
}
}

// Repro from #8851

const re = /./g
let match: RegExpExecArray | null

while ((match = re.exec("xxx")) != null) {
const length = match[1].length + match[2].length
}

//// [typeGuardsNestedAssignments.js]
var Foo = (function () {
function Foo() {
}
return Foo;
}());
function f1() {
var foo;
if ((foo = getFooOrNull()) !== null) {
foo; // Foo
}
}
function f2() {
var foo1;
var foo2;
if ((foo1 = getFooOrNull(), foo2 = foo1) !== null) {
foo1; // Foo | null
foo2; // Foo
}
}
function f3() {
var obj;
if ((obj = getFooOrNull()) instanceof Foo) {
obj;
}
}
function f4() {
var x;
if (typeof (x = getStringOrNumberOrNull()) === "number") {
x;
}
}
// Repro from #8851
var re = /./g;
var match;
while ((match = re.exec("xxx")) != null) {
var length = match[1].length + match[2].length;
}
113 changes: 113 additions & 0 deletions tests/baselines/reference/typeGuardsNestedAssignments.symbols
Original file line number Diff line number Diff line change
@@ -0,0 +1,113 @@
=== tests/cases/conformance/controlFlow/typeGuardsNestedAssignments.ts ===

class Foo {
>Foo : Symbol(Foo, Decl(typeGuardsNestedAssignments.ts, 0, 0))

x: string;
>x : Symbol(Foo.x, Decl(typeGuardsNestedAssignments.ts, 1, 11))
}

declare function getFooOrNull(): Foo | null;
>getFooOrNull : Symbol(getFooOrNull, Decl(typeGuardsNestedAssignments.ts, 3, 1))
>Foo : Symbol(Foo, Decl(typeGuardsNestedAssignments.ts, 0, 0))

declare function getStringOrNumberOrNull(): string | number | null;
>getStringOrNumberOrNull : Symbol(getStringOrNumberOrNull, Decl(typeGuardsNestedAssignments.ts, 5, 44))

function f1() {
>f1 : Symbol(f1, Decl(typeGuardsNestedAssignments.ts, 6, 67))

let foo: Foo | null;
>foo : Symbol(foo, Decl(typeGuardsNestedAssignments.ts, 9, 7))
>Foo : Symbol(Foo, Decl(typeGuardsNestedAssignments.ts, 0, 0))

if ((foo = getFooOrNull()) !== null) {
>foo : Symbol(foo, Decl(typeGuardsNestedAssignments.ts, 9, 7))
>getFooOrNull : Symbol(getFooOrNull, Decl(typeGuardsNestedAssignments.ts, 3, 1))

foo; // Foo
>foo : Symbol(foo, Decl(typeGuardsNestedAssignments.ts, 9, 7))
}
}

function f2() {
>f2 : Symbol(f2, Decl(typeGuardsNestedAssignments.ts, 13, 1))

let foo1: Foo | null;
>foo1 : Symbol(foo1, Decl(typeGuardsNestedAssignments.ts, 16, 7))
>Foo : Symbol(Foo, Decl(typeGuardsNestedAssignments.ts, 0, 0))

let foo2: Foo | null;
>foo2 : Symbol(foo2, Decl(typeGuardsNestedAssignments.ts, 17, 7))
>Foo : Symbol(Foo, Decl(typeGuardsNestedAssignments.ts, 0, 0))

if ((foo1 = getFooOrNull(), foo2 = foo1) !== null) {
>foo1 : Symbol(foo1, Decl(typeGuardsNestedAssignments.ts, 16, 7))
>getFooOrNull : Symbol(getFooOrNull, Decl(typeGuardsNestedAssignments.ts, 3, 1))
>foo2 : Symbol(foo2, Decl(typeGuardsNestedAssignments.ts, 17, 7))
>foo1 : Symbol(foo1, Decl(typeGuardsNestedAssignments.ts, 16, 7))

foo1; // Foo | null
>foo1 : Symbol(foo1, Decl(typeGuardsNestedAssignments.ts, 16, 7))

foo2; // Foo
>foo2 : Symbol(foo2, Decl(typeGuardsNestedAssignments.ts, 17, 7))
}
}

function f3() {
>f3 : Symbol(f3, Decl(typeGuardsNestedAssignments.ts, 22, 1))

let obj: Object | null;
>obj : Symbol(obj, Decl(typeGuardsNestedAssignments.ts, 25, 7))
>Object : Symbol(Object, Decl(lib.d.ts, --, --), Decl(lib.d.ts, --, --))

if ((obj = getFooOrNull()) instanceof Foo) {
>obj : Symbol(obj, Decl(typeGuardsNestedAssignments.ts, 25, 7))
>getFooOrNull : Symbol(getFooOrNull, Decl(typeGuardsNestedAssignments.ts, 3, 1))
>Foo : Symbol(Foo, Decl(typeGuardsNestedAssignments.ts, 0, 0))

obj;
>obj : Symbol(obj, Decl(typeGuardsNestedAssignments.ts, 25, 7))
}
}

function f4() {
>f4 : Symbol(f4, Decl(typeGuardsNestedAssignments.ts, 29, 1))

let x: string | number | null;
>x : Symbol(x, Decl(typeGuardsNestedAssignments.ts, 32, 7))

if (typeof (x = getStringOrNumberOrNull()) === "number") {
>x : Symbol(x, Decl(typeGuardsNestedAssignments.ts, 32, 7))
>getStringOrNumberOrNull : Symbol(getStringOrNumberOrNull, Decl(typeGuardsNestedAssignments.ts, 5, 44))

x;
>x : Symbol(x, Decl(typeGuardsNestedAssignments.ts, 32, 7))
}
}

// Repro from #8851

const re = /./g
>re : Symbol(re, Decl(typeGuardsNestedAssignments.ts, 40, 5))

let match: RegExpExecArray | null
>match : Symbol(match, Decl(typeGuardsNestedAssignments.ts, 41, 3))
>RegExpExecArray : Symbol(RegExpExecArray, Decl(lib.d.ts, --, --))

while ((match = re.exec("xxx")) != null) {
>match : Symbol(match, Decl(typeGuardsNestedAssignments.ts, 41, 3))
>re.exec : Symbol(RegExp.exec, Decl(lib.d.ts, --, --))
>re : Symbol(re, Decl(typeGuardsNestedAssignments.ts, 40, 5))
>exec : Symbol(RegExp.exec, Decl(lib.d.ts, --, --))

const length = match[1].length + match[2].length
>length : Symbol(length, Decl(typeGuardsNestedAssignments.ts, 44, 9))
>match[1].length : Symbol(String.length, Decl(lib.d.ts, --, --))
>match : Symbol(match, Decl(typeGuardsNestedAssignments.ts, 41, 3))
>length : Symbol(String.length, Decl(lib.d.ts, --, --))
>match[2].length : Symbol(String.length, Decl(lib.d.ts, --, --))
>match : Symbol(match, Decl(typeGuardsNestedAssignments.ts, 41, 3))
>length : Symbol(String.length, Decl(lib.d.ts, --, --))
}
Loading