Merge branch 'master' into use-common-directory-for-out

This commit is contained in:
Wesley Wigham 2015-11-19 12:42:02 -08:00
commit 5ecc271c27
9 changed files with 134 additions and 19 deletions

View File

@ -6427,6 +6427,8 @@ namespace ts {
// Only narrow when symbol is variable of type any or an object, union, or type parameter type
if (node && symbol.flags & SymbolFlags.Variable) {
if (isTypeAny(type) || type.flags & (TypeFlags.ObjectType | TypeFlags.Union | TypeFlags.TypeParameter)) {
const declaration = getDeclarationOfKind(symbol, SyntaxKind.VariableDeclaration);
const top = declaration && getDeclarationContainer(declaration);
const originalType = type;
const nodeStack: {node: Node, child: Node}[] = [];
loop: while (node.parent) {
@ -6440,15 +6442,12 @@ namespace ts {
break;
case SyntaxKind.SourceFile:
case SyntaxKind.ModuleDeclaration:
case SyntaxKind.FunctionDeclaration:
case SyntaxKind.MethodDeclaration:
case SyntaxKind.MethodSignature:
case SyntaxKind.GetAccessor:
case SyntaxKind.SetAccessor:
case SyntaxKind.Constructor:
// Stop at the first containing function or module declaration
// Stop at the first containing file or module declaration
break loop;
}
if (node === top) {
break;
}
}
let nodes: {node: Node, child: Node};

View File

@ -0,0 +1,37 @@
//// [typeGuardInClass.ts]
let x: string | number;
if (typeof x === "string") {
let n = class {
constructor() {
let y: string = x;
}
}
}
else {
let m = class {
constructor() {
let y: number = x;
}
}
}
//// [typeGuardInClass.js]
var x;
if (typeof x === "string") {
var n = (function () {
function class_1() {
var y = x;
}
return class_1;
})();
}
else {
var m = (function () {
function class_2() {
var y = x;
}
return class_2;
})();
}

View File

@ -0,0 +1,29 @@
=== tests/cases/conformance/expressions/typeGuards/typeGuardInClass.ts ===
let x: string | number;
>x : Symbol(x, Decl(typeGuardInClass.ts, 0, 3))
if (typeof x === "string") {
>x : Symbol(x, Decl(typeGuardInClass.ts, 0, 3))
let n = class {
>n : Symbol(n, Decl(typeGuardInClass.ts, 3, 7))
constructor() {
let y: string = x;
>y : Symbol(y, Decl(typeGuardInClass.ts, 5, 15))
>x : Symbol(x, Decl(typeGuardInClass.ts, 0, 3))
}
}
}
else {
let m = class {
>m : Symbol(m, Decl(typeGuardInClass.ts, 10, 7))
constructor() {
let y: number = x;
>y : Symbol(y, Decl(typeGuardInClass.ts, 12, 15))
>x : Symbol(x, Decl(typeGuardInClass.ts, 0, 3))
}
}
}

View File

@ -0,0 +1,34 @@
=== tests/cases/conformance/expressions/typeGuards/typeGuardInClass.ts ===
let x: string | number;
>x : string | number
if (typeof x === "string") {
>typeof x === "string" : boolean
>typeof x : string
>x : string | number
>"string" : string
let n = class {
>n : typeof (Anonymous class)
>class { constructor() { let y: string = x; } } : typeof (Anonymous class)
constructor() {
let y: string = x;
>y : string
>x : string
}
}
}
else {
let m = class {
>m : typeof (Anonymous class)
>class { constructor() { let y: number = x; } } : typeof (Anonymous class)
constructor() {
let y: number = x;
>y : number
>x : number
}
}
}

View File

@ -41,12 +41,12 @@ function foo4(x: number | string | boolean) {
: x.toString(); // number
})(x); // x here is narrowed to number | boolean
}
// Type guards affect nested function expressions, but not nested function declarations
// Type guards affect nested function expressions and nested function declarations
function foo5(x: number | string | boolean) {
if (typeof x === "string") {
var y = x; // string;
function foo() {
var z = x; // number | string | boolean, type guard has no effect
var z = x; // string
}
}
}
@ -121,12 +121,12 @@ function foo4(x) {
: x.toString(); // number
})(x); // x here is narrowed to number | boolean
}
// Type guards affect nested function expressions, but not nested function declarations
// Type guards affect nested function expressions and nested function declarations
function foo5(x) {
if (typeof x === "string") {
var y = x; // string;
function foo() {
var z = x; // number | string | boolean, type guard has no effect
var z = x; // string
}
}
}

View File

@ -130,7 +130,7 @@ function foo4(x: number | string | boolean) {
})(x); // x here is narrowed to number | boolean
>x : Symbol(x, Decl(typeGuardsInFunctionAndModuleBlock.ts, 32, 14))
}
// Type guards affect nested function expressions, but not nested function declarations
// Type guards affect nested function expressions and nested function declarations
function foo5(x: number | string | boolean) {
>foo5 : Symbol(foo5, Decl(typeGuardsInFunctionAndModuleBlock.ts, 41, 1))
>x : Symbol(x, Decl(typeGuardsInFunctionAndModuleBlock.ts, 43, 14))
@ -145,7 +145,7 @@ function foo5(x: number | string | boolean) {
function foo() {
>foo : Symbol(foo, Decl(typeGuardsInFunctionAndModuleBlock.ts, 45, 18))
var z = x; // number | string | boolean, type guard has no effect
var z = x; // string
>z : Symbol(z, Decl(typeGuardsInFunctionAndModuleBlock.ts, 47, 15))
>x : Symbol(x, Decl(typeGuardsInFunctionAndModuleBlock.ts, 43, 14))
}

View File

@ -181,7 +181,7 @@ function foo4(x: number | string | boolean) {
})(x); // x here is narrowed to number | boolean
>x : number | boolean
}
// Type guards affect nested function expressions, but not nested function declarations
// Type guards affect nested function expressions and nested function declarations
function foo5(x: number | string | boolean) {
>foo5 : (x: number | string | boolean) => void
>x : number | string | boolean
@ -199,9 +199,9 @@ function foo5(x: number | string | boolean) {
function foo() {
>foo : () => void
var z = x; // number | string | boolean, type guard has no effect
>z : number | string | boolean
>x : number | string | boolean
var z = x; // string
>z : string
>x : string
}
}
}

View File

@ -0,0 +1,16 @@
let x: string | number;
if (typeof x === "string") {
let n = class {
constructor() {
let y: string = x;
}
}
}
else {
let m = class {
constructor() {
let y: number = x;
}
}
}

View File

@ -40,12 +40,12 @@ function foo4(x: number | string | boolean) {
: x.toString(); // number
})(x); // x here is narrowed to number | boolean
}
// Type guards affect nested function expressions, but not nested function declarations
// Type guards affect nested function expressions and nested function declarations
function foo5(x: number | string | boolean) {
if (typeof x === "string") {
var y = x; // string;
function foo() {
var z = x; // number | string | boolean, type guard has no effect
var z = x; // string
}
}
}