From df91ef0b382464c7c05419ffb2d35b50252a3a3a Mon Sep 17 00:00:00 2001 From: zhengbli Date: Mon, 28 Mar 2016 12:37:37 -0700 Subject: [PATCH 01/41] Simple case and scoping --- src/compiler/binder.ts | 14 +++-- src/compiler/checker.ts | 24 +++++--- src/compiler/diagnosticMessages.json | 4 ++ src/compiler/parser.ts | 86 +++++++++++++++++++++++----- src/compiler/types.ts | 29 +++++++++- 5 files changed, 130 insertions(+), 27 deletions(-) diff --git a/src/compiler/binder.ts b/src/compiler/binder.ts index 6957be484f3..881ad81cb2e 100644 --- a/src/compiler/binder.ts +++ b/src/compiler/binder.ts @@ -404,6 +404,13 @@ namespace ts { // This node will now be set as the parent of all of its children as we recurse into them. parent = node; + // Binding of JsDocComment should be done before the current block scope container changes. + // because the scope of JsDocComment should not be affected by whether the current node is a + // container or not. + if (isInJavaScriptFile(node) && node.jsDocComment) { + bind(node.jsDocComment); + } + // Depending on what kind of node this is, we may have to adjust the current container // and block-container. If the current node is a container, then it is automatically // considered the current block-container as well. Also, for containers that we know @@ -468,10 +475,6 @@ namespace ts { labelStack = labelIndexMap = implicitLabels = undefined; } - if (isInJavaScriptFile(node) && node.jsDocComment) { - bind(node.jsDocComment); - } - bindReachableStatement(node); if (currentReachabilityState === Reachability.Reachable && isFunctionLikeKind(kind) && nodeIsPresent((node).body)) { @@ -857,7 +860,7 @@ namespace ts { // their container in the tree. To accomplish this, we simply add their declared // symbol to the 'locals' of the container. These symbols can then be found as // the type checker walks up the containers, checking them for matching names. - return declareSymbol(container.locals, undefined, node, symbolFlags, symbolExcludes); + return declareSymbol(container.locals, /*parent*/ undefined, node, symbolFlags, symbolExcludes); } } @@ -1343,6 +1346,7 @@ namespace ts { return bindClassLikeDeclaration(node); case SyntaxKind.InterfaceDeclaration: return bindBlockScopedDeclaration(node, SymbolFlags.Interface, SymbolFlags.InterfaceExcludes); + case SyntaxKind.JSDocTypedefTag: case SyntaxKind.TypeAliasDeclaration: return bindBlockScopedDeclaration(node, SymbolFlags.TypeAlias, SymbolFlags.TypeAliasExcludes); case SyntaxKind.EnumDeclaration: diff --git a/src/compiler/checker.ts b/src/compiler/checker.ts index 9509c286236..ff416ec2cc5 100644 --- a/src/compiler/checker.ts +++ b/src/compiler/checker.ts @@ -3403,7 +3403,7 @@ namespace ts { return links.declaredType; } - function getDeclaredTypeOfTypeAlias(symbol: Symbol): Type { + function getDeclaredTypeOfTypeAlias(symbol: Symbol, node?: Node): Type { const links = getSymbolLinks(symbol); if (!links.declaredType) { // Note that we use the links object as the target here because the symbol object is used as the unique @@ -3411,8 +3411,18 @@ namespace ts { if (!pushTypeResolution(symbol, TypeSystemPropertyName.DeclaredType)) { return unknownType; } - const declaration = getDeclarationOfKind(symbol, SyntaxKind.TypeAliasDeclaration); - let type = getTypeFromTypeNode(declaration.type); + + let typeNode: TypeNode; + let name: Identifier; + if (node && (node.flags & NodeFlags.JavaScriptFile)) { + const declaration = getDeclarationOfKind(symbol, SyntaxKind.JSDocTypedefTag); + typeNode = declaration.typeExpression.type; + } + if (!typeNode) { + const declaration = getDeclarationOfKind(symbol, SyntaxKind.TypeAliasDeclaration); + typeNode = declaration.type; + } + let type = getTypeFromTypeNode(typeNode); if (popTypeResolution()) { links.typeParameters = getLocalTypeParametersOfClassOrInterfaceOrTypeAlias(symbol); if (links.typeParameters) { @@ -3424,7 +3434,7 @@ namespace ts { } else { type = unknownType; - error(declaration.name, Diagnostics.Type_alias_0_circularly_references_itself, symbolToString(symbol)); + error(name, Diagnostics.Type_alias_0_circularly_references_itself, symbolToString(symbol)); } links.declaredType = type; } @@ -3462,13 +3472,13 @@ namespace ts { return links.declaredType; } - function getDeclaredTypeOfSymbol(symbol: Symbol): Type { + function getDeclaredTypeOfSymbol(symbol: Symbol, node?: Node): Type { Debug.assert((symbol.flags & SymbolFlags.Instantiated) === 0); if (symbol.flags & (SymbolFlags.Class | SymbolFlags.Interface)) { return getDeclaredTypeOfClassOrInterface(symbol); } if (symbol.flags & SymbolFlags.TypeAlias) { - return getDeclaredTypeOfTypeAlias(symbol); + return getDeclaredTypeOfTypeAlias(symbol, node); } if (symbol.flags & SymbolFlags.Enum) { return getDeclaredTypeOfEnum(symbol); @@ -4564,7 +4574,7 @@ namespace ts { // references to the type parameters of the alias. We replace those with the actual type arguments by instantiating the // declared type. Instantiations are cached using the type identities of the type arguments as the key. function getTypeFromTypeAliasReference(node: TypeReferenceNode | ExpressionWithTypeArguments | JSDocTypeReference, symbol: Symbol): Type { - const type = getDeclaredTypeOfSymbol(symbol); + const type = getDeclaredTypeOfSymbol(symbol, node); const links = getSymbolLinks(symbol); const typeParameters = links.typeParameters; if (typeParameters) { diff --git a/src/compiler/diagnosticMessages.json b/src/compiler/diagnosticMessages.json index d7c2fac2710..c887a9c7112 100644 --- a/src/compiler/diagnosticMessages.json +++ b/src/compiler/diagnosticMessages.json @@ -811,6 +811,10 @@ "category": "Error", "code": 1249 }, + "'{0}' tag cannot be used independently as a top level JSDoc tag.": { + "category": "Error", + "code": 1250 + }, "'with' statements are not allowed in an async function block.": { "category": "Error", "code": 1300 diff --git a/src/compiler/parser.ts b/src/compiler/parser.ts index b7c011631ee..91f225931d2 100644 --- a/src/compiler/parser.ts +++ b/src/compiler/parser.ts @@ -401,6 +401,8 @@ namespace ts { return visitNode(cbNode, (node).typeExpression); case SyntaxKind.JSDocTemplateTag: return visitNodes(cbNodes, (node).typeParameters); + case SyntaxKind.JSDocTypedefTag: + return visitNode(cbNode, (node).typeExpression); } } @@ -886,7 +888,7 @@ namespace ts { /** Invokes the provided callback then unconditionally restores the parser to the state it * was in immediately prior to invoking the callback. The result of invoking the callback - * is returned from this function. + * is returned from this function. */ function lookAhead(callback: () => T): T { return speculationHelper(callback, /*isLookAhead*/ true); @@ -2068,11 +2070,11 @@ namespace ts { } function fillSignature( - returnToken: SyntaxKind, - yieldContext: boolean, - awaitContext: boolean, - requireCompleteParameterList: boolean, - signature: SignatureDeclaration): void { + returnToken: SyntaxKind, + yieldContext: boolean, + awaitContext: boolean, + requireCompleteParameterList: boolean, + signature: SignatureDeclaration): void { const returnTokenRequired = returnToken === SyntaxKind.EqualsGreaterThanToken; signature.typeParameters = parseTypeParameters(); @@ -3341,8 +3343,8 @@ namespace ts { if (sourceFile.languageVariant !== LanguageVariant.JSX) { return false; } - // We are in JSX context and the token is part of JSXElement. - // Fall through + // We are in JSX context and the token is part of JSXElement. + // Fall through default: return true; } @@ -4044,9 +4046,9 @@ namespace ts { const isAsync = !!(node.flags & NodeFlags.Async); node.name = isGenerator && isAsync ? doInYieldAndAwaitContext(parseOptionalIdentifier) : - isGenerator ? doInYieldContext(parseOptionalIdentifier) : - isAsync ? doInAwaitContext(parseOptionalIdentifier) : - parseOptionalIdentifier(); + isGenerator ? doInYieldContext(parseOptionalIdentifier) : + isAsync ? doInAwaitContext(parseOptionalIdentifier) : + parseOptionalIdentifier(); fillSignature(SyntaxKind.ColonToken, /*yieldContext*/ isGenerator, /*awaitContext*/ isAsync, /*requireCompleteParameterList*/ false, node); node.body = parseFunctionBlock(/*allowYield*/ isGenerator, /*allowAwait*/ isAsync, /*ignoreMissingOpenBrace*/ false); @@ -4984,7 +4986,7 @@ namespace ts { if (token === SyntaxKind.ConstKeyword && permitInvalidConstAsModifier) { // We need to ensure that any subsequent modifiers appear on the same line - // so that when 'const' is a standalone declaration, we don't issue an error. + // so that when 'const' is a standalone declaration, we don't issue an error. if (!tryParse(nextTokenIsOnSameLineAndCanFollowModifier)) { break; } @@ -5247,7 +5249,7 @@ namespace ts { node.decorators = decorators; setModifiers(node, modifiers); if (token === SyntaxKind.GlobalKeyword) { - // parse 'global' as name of global scope augmentation + // parse 'global' as name of global scope augmentation node.name = parseIdentifier(); node.flags |= NodeFlags.GlobalAugmentation; } @@ -5829,7 +5831,7 @@ namespace ts { } function checkForEmptyTypeArgumentList(typeArguments: NodeArray) { - if (parseDiagnostics.length === 0 && typeArguments && typeArguments.length === 0) { + if (parseDiagnostics.length === 0 && typeArguments && typeArguments.length === 0) { const start = typeArguments.pos - "<".length; const end = skipTrivia(sourceText, typeArguments.end) + ">".length; return parseErrorAtPosition(start, end - start, Diagnostics.Type_argument_list_cannot_be_empty); @@ -5990,6 +5992,7 @@ namespace ts { Debug.assert(end <= content.length); let tags: NodeArray; + let currentParentJSDocDeclaration: Declaration; let result: JSDocComment; @@ -6097,6 +6100,11 @@ namespace ts { return handleTemplateTag(atToken, tagName); case "type": return handleTypeTag(atToken, tagName); + case "typedef": + return handleTypedefTag(atToken, tagName); + case "property": + case "prop": + return handlePropertyTag(atToken, tagName); } } @@ -6204,6 +6212,56 @@ namespace ts { return finishNode(result); } + function handlePropertyTag(atToken: Node, tagName: Identifier): JSDocPropertyTag { + if (!currentParentJSDocDeclaration) { + parseErrorAtPosition(tagName.pos, scanner.getTokenPos() - tagName.pos, Diagnostics._0_tag_cannot_be_used_independently_as_a_top_level_JSDoc_tag, tagName.text); + return undefined; + } + + const typeExpression = tryParseTypeExpression(); + skipWhitespace(); + const name = parseJSDocIdentifier(); + if (!name) { + parseErrorAtPosition(scanner.getStartPos(), /*length*/ 0, Diagnostics.Identifier_expected); + return undefined; + } + + const result = createNode(SyntaxKind.JSDocPropertyTag, atToken.pos); + result.atToken = atToken; + result.tagName = tagName; + result.name = name; + result.typeExpression = typeExpression; + return finishNode(result); + } + + function handleTypedefTag(atToken: Node, tagName: Identifier): JSDocTypedefTag { + const typeExpression = tryParseTypeExpression(); + skipWhitespace(); + const name = parseJSDocIdentifier(); + if (!name) { + parseErrorAtPosition(scanner.getStartPos(), 0, Diagnostics.Identifier_expected); + return undefined; + } + + const result = createNode(SyntaxKind.JSDocTypedefTag, atToken.pos); + result.atToken = atToken; + result.tagName = tagName; + result.name = name; + result.typeExpression = typeExpression; + + // if (typeExpression && typeExpression.type.kind === SyntaxKind.JSDocTypeReference) { + // const jsDocTypeReference = typeExpression.type; + // if (jsDocTypeReference.name.kind === SyntaxKind.Identifier) { + // const name = jsDocTypeReference.name; + // if (name.text === "Object") { + // currentParentJSDocDeclaration = declaration; + // } + // } + // } + + return result; + } + function handleTemplateTag(atToken: Node, tagName: Identifier): JSDocTemplateTag { if (forEach(tags, t => t.kind === SyntaxKind.JSDocTemplateTag)) { parseErrorAtPosition(tagName.pos, scanner.getTokenPos() - tagName.pos, Diagnostics._0_tag_already_specified, tagName.text); diff --git a/src/compiler/types.ts b/src/compiler/types.ts index 546d1631945..a0cc8d054e1 100644 --- a/src/compiler/types.ts +++ b/src/compiler/types.ts @@ -342,6 +342,10 @@ namespace ts { JSDocReturnTag, JSDocTypeTag, JSDocTemplateTag, + JSDocTypedefTag, + JSDocPropertyTag, + JSDocTypedefDeclaration, + JSDocTypeLiteral, // Synthesized list SyntaxList, @@ -1510,6 +1514,29 @@ namespace ts { typeExpression: JSDocTypeExpression; } + // @kind(SyntaxKind.JSDocTypedefTag) + export interface JSDocTypedefTag extends JSDocTag, Declaration { + name: Identifier; + typeExpression: JSDocTypeExpression; + } + + // @kind(SyntaxKind.JSDocPropertyTag) + export interface JSDocPropertyTag extends JSDocTag, TypeElement { + name: Identifier; + typeExpression: JSDocTypeExpression; + } + + // @kind(SyntaxKind.JSDocTypedefDeclaration) + export interface JSDocTypedefDeclaration extends Declaration { + name: Identifier; + type: TypeNode; + } + + // @kind(SyntaxKind.JSDocTypeLiteral) + export interface JSDocTypeLiteral extends TypeNode { + members: NodeArray; + } + // @kind(SyntaxKind.JSDocParameterTag) export interface JSDocParameterTag extends JSDocTag { preParameterName?: Identifier; @@ -2094,7 +2121,7 @@ namespace ts { jsxFlags?: JsxFlags; // flags for knowing what kind of element/attributes we're dealing with resolvedJsxType?: Type; // resolved element attributes type of a JSX openinglike element hasSuperCall?: boolean; // recorded result when we try to find super-call. We only try to find one if this flag is undefined, indicating that we haven't made an attempt. - superCall?: ExpressionStatement; // Cached first super-call found in the constructor. Used in checking whether super is called before this-accessing + superCall?: ExpressionStatement; // Cached first super-call found in the constructor. Used in checking whether super is called before this-accessing } export const enum TypeFlags { From 23bca4eda6b220a88771a6c922842f8da1c3b366 Mon Sep 17 00:00:00 2001 From: Zhengbo Li Date: Mon, 4 Apr 2016 00:44:51 -0700 Subject: [PATCH 02/41] Support the case with @property and @type --- src/compiler/binder.ts | 4 + src/compiler/checker.ts | 16 +- src/compiler/parser.ts | 142 +++++++++++++++--- src/compiler/types.ts | 19 +-- .../fourslash/server/jsdocCompletions.ts | 15 ++ 5 files changed, 160 insertions(+), 36 deletions(-) create mode 100644 tests/cases/fourslash/server/jsdocCompletions.ts diff --git a/src/compiler/binder.ts b/src/compiler/binder.ts index 881ad81cb2e..b3f163bbef1 100644 --- a/src/compiler/binder.ts +++ b/src/compiler/binder.ts @@ -744,6 +744,7 @@ namespace ts { case SyntaxKind.EnumDeclaration: case SyntaxKind.ObjectLiteralExpression: case SyntaxKind.TypeLiteral: + case SyntaxKind.JSDocTypeLiteral: case SyntaxKind.JSDocRecordType: return ContainerFlags.IsContainer; @@ -832,6 +833,7 @@ namespace ts { case SyntaxKind.ObjectLiteralExpression: case SyntaxKind.InterfaceDeclaration: case SyntaxKind.JSDocRecordType: + case SyntaxKind.JSDocTypeLiteral: // Interface/Object-types always have their children added to the 'members' of // their container. They are only accessible through an instance of their // container, and are never in scope otherwise (even inside the body of the @@ -1294,6 +1296,7 @@ namespace ts { case SyntaxKind.PropertyDeclaration: case SyntaxKind.PropertySignature: case SyntaxKind.JSDocRecordMember: + case SyntaxKind.JSDocPropertyTag: return bindPropertyOrMethodOrAccessor(node, SymbolFlags.Property | ((node).questionToken ? SymbolFlags.Optional : SymbolFlags.None), SymbolFlags.PropertyExcludes); case SyntaxKind.PropertyAssignment: case SyntaxKind.ShorthandPropertyAssignment: @@ -1326,6 +1329,7 @@ namespace ts { case SyntaxKind.JSDocFunctionType: return bindFunctionOrConstructorType(node); case SyntaxKind.TypeLiteral: + case SyntaxKind.JSDocTypeLiteral: case SyntaxKind.JSDocRecordType: return bindAnonymousDeclaration(node, SymbolFlags.TypeLiteral, "__type"); case SyntaxKind.ObjectLiteralExpression: diff --git a/src/compiler/checker.ts b/src/compiler/checker.ts index ff416ec2cc5..e66e5e6a9d8 100644 --- a/src/compiler/checker.ts +++ b/src/compiler/checker.ts @@ -3412,17 +3412,14 @@ namespace ts { return unknownType; } - let typeNode: TypeNode; - let name: Identifier; + let declaration: JSDocTypedefTag | TypeAliasDeclaration; if (node && (node.flags & NodeFlags.JavaScriptFile)) { - const declaration = getDeclarationOfKind(symbol, SyntaxKind.JSDocTypedefTag); - typeNode = declaration.typeExpression.type; + declaration = getDeclarationOfKind(symbol, SyntaxKind.JSDocTypedefTag); } - if (!typeNode) { - const declaration = getDeclarationOfKind(symbol, SyntaxKind.TypeAliasDeclaration); - typeNode = declaration.type; + if (!declaration) { + declaration = getDeclarationOfKind(symbol, SyntaxKind.TypeAliasDeclaration); } - let type = getTypeFromTypeNode(typeNode); + let type = getTypeFromTypeNode(declaration.type); if (popTypeResolution()) { links.typeParameters = getLocalTypeParametersOfClassOrInterfaceOrTypeAlias(symbol); if (links.typeParameters) { @@ -3434,7 +3431,7 @@ namespace ts { } else { type = unknownType; - error(name, Diagnostics.Type_alias_0_circularly_references_itself, symbolToString(symbol)); + error(declaration.name, Diagnostics.Type_alias_0_circularly_references_itself, symbolToString(symbol)); } links.declaredType = type; } @@ -5067,6 +5064,7 @@ namespace ts { case SyntaxKind.FunctionType: case SyntaxKind.ConstructorType: case SyntaxKind.TypeLiteral: + case SyntaxKind.JSDocTypeLiteral: case SyntaxKind.JSDocFunctionType: case SyntaxKind.JSDocRecordType: return getTypeFromTypeLiteralOrFunctionOrConstructorTypeNode(node); diff --git a/src/compiler/parser.ts b/src/compiler/parser.ts index 91f225931d2..5de1369ee09 100644 --- a/src/compiler/parser.ts +++ b/src/compiler/parser.ts @@ -402,7 +402,15 @@ namespace ts { case SyntaxKind.JSDocTemplateTag: return visitNodes(cbNodes, (node).typeParameters); case SyntaxKind.JSDocTypedefTag: - return visitNode(cbNode, (node).typeExpression); + return visitNode(cbNode, (node).name) || + visitNode(cbNode, (node).typeExpression) || + visitNode(cbNode, (node).type); + case SyntaxKind.JSDocTypeLiteral: + return visitNodes(cbNodes, (node).members); + case SyntaxKind.JSDocPropertyTag: + return visitNode(cbNode, (node).name) || + visitNode(cbNode, (node).typeExpression) || + visitNode(cbNode, (node).type); } } @@ -5992,7 +6000,8 @@ namespace ts { Debug.assert(end <= content.length); let tags: NodeArray; - let currentParentJSDocDeclaration: Declaration; + let currentParentJSDocTag: JSDocParentTag; + let currentParentJSDocTagEnd: number; let result: JSDocComment; @@ -6050,6 +6059,10 @@ namespace ts { nextJSDocToken(); } + if (currentParentJSDocTag) { + finishCurrentParentTag(); + } + result = createJSDocComment(); }); @@ -6073,6 +6086,40 @@ namespace ts { } } + function finishCurrentParentTag(): void { + if (!currentParentJSDocTag) { + return; + } + + if (currentParentJSDocTag.kind === SyntaxKind.JSDocTypedefTag) { + const typedefTag = currentParentJSDocTag; + if (typedefTag.jsDocTypeTag) { + if (typedefTag.jsDocTypeTag.typeExpression.type.kind === SyntaxKind.JSDocTypeReference) { + const typeTagtype = typedefTag.jsDocTypeTag.typeExpression.type; + if ((typeTagtype.name.kind !== SyntaxKind.Identifier) || + (typeTagtype.name).text !== "Object") { + typedefTag.type = typedefTag.jsDocTypeTag.typeExpression.type; + } + } + else { + typedefTag.type = typedefTag.jsDocTypeTag.typeExpression.type; + } + } + if (!typedefTag.type) { + const tagType = createNode(SyntaxKind.JSDocTypeLiteral, currentParentJSDocTag.pos); + if (typedefTag.jsDocPropertyTags){ + tagType.members = >[]; + addRange(tagType.members, typedefTag.jsDocPropertyTags); + } + typedefTag.type = finishNode(tagType, currentParentJSDocTagEnd); + } + } + + addTag(finishNode(currentParentJSDocTag, currentParentJSDocTagEnd)); + currentParentJSDocTag = undefined; + currentParentJSDocTagEnd = undefined; + } + function parseTag(): void { Debug.assert(token === SyntaxKind.AtToken); const atToken = createNode(SyntaxKind.AtToken, scanner.getTokenPos()); @@ -6085,22 +6132,30 @@ namespace ts { } const tag = handleTag(atToken, tagName) || handleUnknownTag(atToken, tagName); - addTag(tag); + if (!currentParentJSDocTag) { + addTag(tag); + } } function handleTag(atToken: Node, tagName: Identifier): JSDocTag { if (tagName) { switch (tagName.text) { case "param": + finishCurrentParentTag(); return handleParamTag(atToken, tagName); case "return": case "returns": + finishCurrentParentTag(); return handleReturnTag(atToken, tagName); case "template": + finishCurrentParentTag(); return handleTemplateTag(atToken, tagName); case "type": + // @typedef tag is allowed to have one @type tag, therefore seeing + // a @type tag may not indicate the end of the current parent tag. return handleTypeTag(atToken, tagName); case "typedef": + finishCurrentParentTag(); return handleTypedefTag(atToken, tagName); case "property": case "prop": @@ -6130,6 +6185,25 @@ namespace ts { } } + function addToCurrentParentTag(tag: JSDocTag): void { + if (!currentParentJSDocTag) { + return; + } + switch (tag.kind) { + case SyntaxKind.JSDocPropertyTag: + if (!currentParentJSDocTag.jsDocPropertyTags) { + currentParentJSDocTag.jsDocPropertyTags = >[]; + } + currentParentJSDocTag.jsDocPropertyTags.push(tag); + break; + case SyntaxKind.JSDocTypeTag: + if (!currentParentJSDocTag.jsDocTypeTag) { + currentParentJSDocTag.jsDocTypeTag = tag; + } + break; + } + } + function tryParseTypeExpression(): JSDocTypeExpression { if (token !== SyntaxKind.OpenBraceToken) { return undefined; @@ -6205,15 +6279,33 @@ namespace ts { parseErrorAtPosition(tagName.pos, scanner.getTokenPos() - tagName.pos, Diagnostics._0_tag_already_specified, tagName.text); } - const result = createNode(SyntaxKind.JSDocTypeTag, atToken.pos); + let result = createNode(SyntaxKind.JSDocTypeTag, atToken.pos); result.atToken = atToken; result.tagName = tagName; result.typeExpression = tryParseTypeExpression(); - return finishNode(result); + result = finishNode(result); + + let typeTagPartOfParentTag = false; + if (currentParentJSDocTag && currentParentJSDocTag.kind === SyntaxKind.JSDocTypedefTag) { + const parentTag = currentParentJSDocTag; + if (!parentTag.typeExpression && !parentTag.jsDocTypeTag) { + typeTagPartOfParentTag = true; + parentTag.jsDocTypeTag = result; + currentParentJSDocTagEnd = scanner.getStartPos(); + } + } + if (!typeTagPartOfParentTag) { + // If this @type tag is not part of the current parent tag, then + // it denotes the end of the current parent tag. + finishCurrentParentTag(); + return result; + } + + return undefined; } function handlePropertyTag(atToken: Node, tagName: Identifier): JSDocPropertyTag { - if (!currentParentJSDocDeclaration) { + if (!currentParentJSDocTag) { parseErrorAtPosition(tagName.pos, scanner.getTokenPos() - tagName.pos, Diagnostics._0_tag_cannot_be_used_independently_as_a_top_level_JSDoc_tag, tagName.text); return undefined; } @@ -6226,12 +6318,17 @@ namespace ts { return undefined; } - const result = createNode(SyntaxKind.JSDocPropertyTag, atToken.pos); + let result = createNode(SyntaxKind.JSDocPropertyTag, atToken.pos); result.atToken = atToken; result.tagName = tagName; result.name = name; result.typeExpression = typeExpression; - return finishNode(result); + result.type = typeExpression.type; + result = finishNode(result); + + addToCurrentParentTag(result); + currentParentJSDocTagEnd = scanner.getStartPos(); + return undefined; } function handleTypedefTag(atToken: Node, tagName: Identifier): JSDocTypedefTag { @@ -6249,17 +6346,26 @@ namespace ts { result.name = name; result.typeExpression = typeExpression; - // if (typeExpression && typeExpression.type.kind === SyntaxKind.JSDocTypeReference) { - // const jsDocTypeReference = typeExpression.type; - // if (jsDocTypeReference.name.kind === SyntaxKind.Identifier) { - // const name = jsDocTypeReference.name; - // if (name.text === "Object") { - // currentParentJSDocDeclaration = declaration; - // } - // } - // } + if (typeExpression && typeExpression.type.kind === SyntaxKind.JSDocTypeReference) { + const jsDocTypeReference = typeExpression.type; + if (jsDocTypeReference.name.kind === SyntaxKind.Identifier) { + const name = jsDocTypeReference.name; + if (name.text === "Object") { + currentParentJSDocTag = result; + } + } + } + else if (!typeExpression) { + currentParentJSDocTag = result; + } - return result; + if (!currentParentJSDocTag) { + result.type = result.typeExpression.type; + return finishNode(result); + } + + currentParentJSDocTagEnd = scanner.getStartPos(); + return undefined; } function handleTemplateTag(atToken: Node, tagName: Identifier): JSDocTemplateTag { diff --git a/src/compiler/types.ts b/src/compiler/types.ts index a0cc8d054e1..31c2c8eb068 100644 --- a/src/compiler/types.ts +++ b/src/compiler/types.ts @@ -344,7 +344,6 @@ namespace ts { JSDocTemplateTag, JSDocTypedefTag, JSDocPropertyTag, - JSDocTypedefDeclaration, JSDocTypeLiteral, // Synthesized list @@ -616,6 +615,7 @@ namespace ts { // SyntaxKind.PropertyAssignment // SyntaxKind.ShorthandPropertyAssignment // SyntaxKind.EnumMember + // SyntaxKind.JSDocPropertyTag export interface VariableLikeDeclaration extends Declaration { propertyName?: PropertyName; dotDotDotToken?: Node; @@ -1515,25 +1515,26 @@ namespace ts { } // @kind(SyntaxKind.JSDocTypedefTag) - export interface JSDocTypedefTag extends JSDocTag, Declaration { + export interface JSDocTypedefTag extends JSDocTag, Declaration, JSDocParentTag { name: Identifier; - typeExpression: JSDocTypeExpression; + typeExpression?: JSDocTypeExpression; + type: JSDocType; + } + + export interface JSDocParentTag extends JSDocTag { + jsDocPropertyTags?: NodeArray; + jsDocTypeTag?: JSDocTypeTag; } // @kind(SyntaxKind.JSDocPropertyTag) export interface JSDocPropertyTag extends JSDocTag, TypeElement { name: Identifier; typeExpression: JSDocTypeExpression; - } - - // @kind(SyntaxKind.JSDocTypedefDeclaration) - export interface JSDocTypedefDeclaration extends Declaration { - name: Identifier; type: TypeNode; } // @kind(SyntaxKind.JSDocTypeLiteral) - export interface JSDocTypeLiteral extends TypeNode { + export interface JSDocTypeLiteral extends JSDocType { members: NodeArray; } diff --git a/tests/cases/fourslash/server/jsdocCompletions.ts b/tests/cases/fourslash/server/jsdocCompletions.ts new file mode 100644 index 00000000000..c8dc1bbe481 --- /dev/null +++ b/tests/cases/fourslash/server/jsdocCompletions.ts @@ -0,0 +1,15 @@ +/// + +// @allowNonTsExtensions: true +// @Filename: jsdocCompletion_typedef.js +//// /** +//// * @typedef {Object} Person +//// * @property {string} personName +//// * @type {Person} +//// */ +//// var x1; +//// x1/**/ + +goTo.marker(); +edit.insert('.'); +verify.memberListContains('personName'); \ No newline at end of file From 9a8f6395e44ed494876f7a671b2d2750b336b929 Mon Sep 17 00:00:00 2001 From: Zhengbo Li Date: Wed, 6 Apr 2016 20:22:23 -0700 Subject: [PATCH 03/41] Support the case with variable name as @typedef type name --- src/compiler/checker.ts | 13 +++++-------- src/compiler/parser.ts | 22 +++++++++++++++++----- 2 files changed, 22 insertions(+), 13 deletions(-) diff --git a/src/compiler/checker.ts b/src/compiler/checker.ts index e66e5e6a9d8..35ae6777fe2 100644 --- a/src/compiler/checker.ts +++ b/src/compiler/checker.ts @@ -3403,7 +3403,7 @@ namespace ts { return links.declaredType; } - function getDeclaredTypeOfTypeAlias(symbol: Symbol, node?: Node): Type { + function getDeclaredTypeOfTypeAlias(symbol: Symbol): Type { const links = getSymbolLinks(symbol); if (!links.declaredType) { // Note that we use the links object as the target here because the symbol object is used as the unique @@ -3412,10 +3412,7 @@ namespace ts { return unknownType; } - let declaration: JSDocTypedefTag | TypeAliasDeclaration; - if (node && (node.flags & NodeFlags.JavaScriptFile)) { - declaration = getDeclarationOfKind(symbol, SyntaxKind.JSDocTypedefTag); - } + let declaration: JSDocTypedefTag | TypeAliasDeclaration = getDeclarationOfKind(symbol, SyntaxKind.JSDocTypedefTag); if (!declaration) { declaration = getDeclarationOfKind(symbol, SyntaxKind.TypeAliasDeclaration); } @@ -3469,13 +3466,13 @@ namespace ts { return links.declaredType; } - function getDeclaredTypeOfSymbol(symbol: Symbol, node?: Node): Type { + function getDeclaredTypeOfSymbol(symbol: Symbol): Type { Debug.assert((symbol.flags & SymbolFlags.Instantiated) === 0); if (symbol.flags & (SymbolFlags.Class | SymbolFlags.Interface)) { return getDeclaredTypeOfClassOrInterface(symbol); } if (symbol.flags & SymbolFlags.TypeAlias) { - return getDeclaredTypeOfTypeAlias(symbol, node); + return getDeclaredTypeOfTypeAlias(symbol); } if (symbol.flags & SymbolFlags.Enum) { return getDeclaredTypeOfEnum(symbol); @@ -4571,7 +4568,7 @@ namespace ts { // references to the type parameters of the alias. We replace those with the actual type arguments by instantiating the // declared type. Instantiations are cached using the type identities of the type arguments as the key. function getTypeFromTypeAliasReference(node: TypeReferenceNode | ExpressionWithTypeArguments | JSDocTypeReference, symbol: Symbol): Type { - const type = getDeclaredTypeOfSymbol(symbol, node); + const type = getDeclaredTypeOfSymbol(symbol); const links = getSymbolLinks(symbol); const typeParameters = links.typeParameters; if (typeParameters) { diff --git a/src/compiler/parser.ts b/src/compiler/parser.ts index 5de1369ee09..0b4a98bd986 100644 --- a/src/compiler/parser.ts +++ b/src/compiler/parser.ts @@ -5977,7 +5977,7 @@ namespace ts { const saveParseDiagnosticsLength = parseDiagnostics.length; const saveParseErrorBeforeNextFinishedNode = parseErrorBeforeNextFinishedNode; - const comment = parseJSDocCommentWorker(start, length); + const comment = parseJSDocCommentWorker(start, length, parent); if (comment) { comment.parent = parent; } @@ -5989,7 +5989,7 @@ namespace ts { return comment; } - export function parseJSDocCommentWorker(start: number, length: number): JSDocComment { + export function parseJSDocCommentWorker(start: number, length: number, parentNode?: Node): JSDocComment { const content = sourceText; start = start || 0; const end = length === undefined ? content.length : start + length; @@ -6334,10 +6334,22 @@ namespace ts { function handleTypedefTag(atToken: Node, tagName: Identifier): JSDocTypedefTag { const typeExpression = tryParseTypeExpression(); skipWhitespace(); - const name = parseJSDocIdentifier(); + let name = parseJSDocIdentifier(); if (!name) { - parseErrorAtPosition(scanner.getStartPos(), 0, Diagnostics.Identifier_expected); - return undefined; + let foundNameFromParentNode = false; + if (parentNode && parentNode.kind === SyntaxKind.VariableStatement) { + if ((parentNode).declarationList.declarations.length > 0) { + const nameFromParentNode = (parentNode).declarationList.declarations[0].name; + if (nameFromParentNode.kind === SyntaxKind.Identifier) { + foundNameFromParentNode = true; + name = nameFromParentNode; + } + } + } + if (!foundNameFromParentNode) { + parseErrorAtPosition(scanner.getStartPos(), 0, Diagnostics.Identifier_expected); + return undefined; + } } const result = createNode(SyntaxKind.JSDocTypedefTag, atToken.pos); From 2945f64e3b88f8658ed7863d4d3146ad9d5c6b52 Mon Sep 17 00:00:00 2001 From: zhengbli Date: Fri, 8 Apr 2016 20:19:39 -0700 Subject: [PATCH 04/41] Add tests for renaming and incremental parsing. Changed how the AST was hydrated with jsdocComment nodes. --- src/compiler/binder.ts | 6 +- src/compiler/checker.ts | 4 +- src/compiler/parser.ts | 44 +-- src/compiler/scanner.ts | 5 +- src/compiler/types.ts | 9 +- src/compiler/utilities.ts | 89 ++++-- src/harness/harness.ts | 22 +- src/services/services.ts | 53 +++- src/services/utilities.ts | 61 ++-- tests/cases/fourslash/commentsVariables.ts | 80 ++--- tests/cases/fourslash/doesnotwork.ts | 45 +++ .../fourslash/server/jsdocCompletions.ts | 15 - .../cases/fourslash/server/jsdocTypedefTag.ts | 64 ++++ .../server/jsdocTypedefTagGoToDefinition.ts | 29 ++ .../server/jsdocTypedefTagRename01.ts | 20 ++ .../server/jsdocTypedefTagRename02.ts | 15 + .../server/jsdocTypedefTagRename03.ts | 20 ++ .../server/jsdocTypedefTagRename04.ts | 24 ++ tests/cases/unittests/jsDocParsing.ts | 3 +- tests/webTestServer.js | 288 ++++++++++++++++++ tests/webTestServer.js.map | 1 + 21 files changed, 757 insertions(+), 140 deletions(-) create mode 100644 tests/cases/fourslash/doesnotwork.ts delete mode 100644 tests/cases/fourslash/server/jsdocCompletions.ts create mode 100644 tests/cases/fourslash/server/jsdocTypedefTag.ts create mode 100644 tests/cases/fourslash/server/jsdocTypedefTagGoToDefinition.ts create mode 100644 tests/cases/fourslash/server/jsdocTypedefTagRename01.ts create mode 100644 tests/cases/fourslash/server/jsdocTypedefTagRename02.ts create mode 100644 tests/cases/fourslash/server/jsdocTypedefTagRename03.ts create mode 100644 tests/cases/fourslash/server/jsdocTypedefTagRename04.ts create mode 100644 tests/webTestServer.js create mode 100644 tests/webTestServer.js.map diff --git a/src/compiler/binder.ts b/src/compiler/binder.ts index b3f163bbef1..45629da8adf 100644 --- a/src/compiler/binder.ts +++ b/src/compiler/binder.ts @@ -407,8 +407,10 @@ namespace ts { // Binding of JsDocComment should be done before the current block scope container changes. // because the scope of JsDocComment should not be affected by whether the current node is a // container or not. - if (isInJavaScriptFile(node) && node.jsDocComment) { - bind(node.jsDocComment); + if (isInJavaScriptFile(node) && node.jsDocComments) { + for (const jsDocComment of node.jsDocComments) { + bind(jsDocComment); + } } // Depending on what kind of node this is, we may have to adjust the current container diff --git a/src/compiler/checker.ts b/src/compiler/checker.ts index 35ae6777fe2..c903696f88f 100644 --- a/src/compiler/checker.ts +++ b/src/compiler/checker.ts @@ -16117,7 +16117,7 @@ namespace ts { node = node.parent; } - return node.parent && node.parent.kind === SyntaxKind.TypeReference; + return node.parent && (node.parent.kind === SyntaxKind.TypeReference || node.parent.kind === SyntaxKind.JSDocTypeReference) ; } function isHeritageClauseElementIdentifier(entityName: Node): boolean { @@ -16251,7 +16251,7 @@ namespace ts { } } else if (isTypeReferenceIdentifier(entityName)) { - let meaning = entityName.parent.kind === SyntaxKind.TypeReference ? SymbolFlags.Type : SymbolFlags.Namespace; + let meaning = (entityName.parent.kind === SyntaxKind.TypeReference || entityName.parent.kind === SyntaxKind.JSDocTypeReference) ? SymbolFlags.Type : SymbolFlags.Namespace; // Include aliases in the meaning, this ensures that we do not follow aliases to where they point and instead // return the alias symbol. meaning |= SymbolFlags.Alias; diff --git a/src/compiler/parser.ts b/src/compiler/parser.ts index 0b4a98bd986..009a3e13fa2 100644 --- a/src/compiler/parser.ts +++ b/src/compiler/parser.ts @@ -402,15 +402,15 @@ namespace ts { case SyntaxKind.JSDocTemplateTag: return visitNodes(cbNodes, (node).typeParameters); case SyntaxKind.JSDocTypedefTag: - return visitNode(cbNode, (node).name) || - visitNode(cbNode, (node).typeExpression) || + return visitNode(cbNode, (node).typeExpression) || + visitNode(cbNode, (node).name) || visitNode(cbNode, (node).type); case SyntaxKind.JSDocTypeLiteral: return visitNodes(cbNodes, (node).members); case SyntaxKind.JSDocPropertyTag: - return visitNode(cbNode, (node).name) || - visitNode(cbNode, (node).typeExpression) || - visitNode(cbNode, (node).type); + return visitNode(cbNode, (node).typeExpression) || + visitNode(cbNode, (node).type) || + visitNode(cbNode, (node).name); } } @@ -638,9 +638,14 @@ namespace ts { if (comments) { for (const comment of comments) { const jsDocComment = JSDocParser.parseJSDocComment(node, comment.pos, comment.end - comment.pos); - if (jsDocComment) { - node.jsDocComment = jsDocComment; + if (!jsDocComment) { + continue; } + + if (!node.jsDocComments) { + node.jsDocComments = []; + } + node.jsDocComments.push(jsDocComment); } } } @@ -6105,13 +6110,15 @@ namespace ts { typedefTag.type = typedefTag.jsDocTypeTag.typeExpression.type; } } - if (!typedefTag.type) { - const tagType = createNode(SyntaxKind.JSDocTypeLiteral, currentParentJSDocTag.pos); - if (typedefTag.jsDocPropertyTags){ - tagType.members = >[]; - addRange(tagType.members, typedefTag.jsDocPropertyTags); - } - typedefTag.type = finishNode(tagType, currentParentJSDocTagEnd); + if (!typedefTag.type && typedefTag.jsDocPropertyTags) { + const childrenTagPoses = ts.map(typedefTag.jsDocPropertyTags, tag => tag.pos); + const childrenTagEnds = ts.map(typedefTag.jsDocPropertyTags, tag => tag.end); + const pos = Math.min(...childrenTagPoses); + const end = Math.max(...childrenTagEnds); + const tagType = createNode(SyntaxKind.JSDocTypeLiteral, pos); + tagType.members = >[]; + addRange(tagType.members, typedefTag.jsDocPropertyTags); + typedefTag.type = finishNode(tagType, end); } } @@ -6545,10 +6552,6 @@ namespace ts { node._children = undefined; } - if (node.jsDocComment) { - node.jsDocComment = undefined; - } - node.pos += delta; node.end += delta; @@ -6557,6 +6560,11 @@ namespace ts { } forEachChild(node, visitNode, visitArray); + if (node.jsDocComments) { + for (const jsDocComment of node.jsDocComments) { + forEachChild(jsDocComment, visitNode, visitArray); + } + } checkNodePositions(node, aggressiveChecks); } diff --git a/src/compiler/scanner.ts b/src/compiler/scanner.ts index 8979814a7a2..d4206749fae 100644 --- a/src/compiler/scanner.ts +++ b/src/compiler/scanner.ts @@ -433,7 +433,7 @@ namespace ts { } /* @internal */ - export function skipTrivia(text: string, pos: number, stopAfterLineBreak?: boolean): number { + export function skipTrivia(text: string, pos: number, stopAfterLineBreak?: boolean, stopAtComments = false): number { // Using ! with a greater than test is a fast way of testing the following conditions: // pos === undefined || pos === null || isNaN(pos) || pos < 0; if (!(pos >= 0)) { @@ -461,6 +461,9 @@ namespace ts { pos++; continue; case CharacterCodes.slash: + if (stopAtComments) { + break; + } if (text.charCodeAt(pos + 1) === CharacterCodes.slash) { pos += 2; while (pos < text.length) { diff --git a/src/compiler/types.ts b/src/compiler/types.ts index 31c2c8eb068..838b5d951f2 100644 --- a/src/compiler/types.ts +++ b/src/compiler/types.ts @@ -374,6 +374,8 @@ namespace ts { FirstBinaryOperator = LessThanToken, LastBinaryOperator = CaretEqualsToken, FirstNode = QualifiedName, + FirstJSDocNode = JSDocTypeExpression, + LastJSDocNode = JSDocTypeLiteral } export const enum NodeFlags { @@ -447,7 +449,7 @@ namespace ts { modifiers?: ModifiersArray; // Array of modifiers /* @internal */ id?: number; // Unique id (used to look up NodeLinks) parent?: Node; // Parent node (initialized by binding - /* @internal */ jsDocComment?: JSDocComment; // JSDoc for the node, if it has any. Only for .js files. + /* @internal */ jsDocComments?: JSDocComment[]; // JSDoc for the node, if it has any. Only for .js files. /* @internal */ symbol?: Symbol; // Symbol declared by node (initialized by binding) /* @internal */ locals?: SymbolTable; // Locals associated with node (initialized by binding) /* @internal */ nextContainer?: Node; // Next container in declaration order (initialized by binding) @@ -2827,4 +2829,9 @@ namespace ts { /* @internal */ reattachFileDiagnostics(newFile: SourceFile): void; } + + // SyntaxKind.SyntaxList + export interface SyntaxList extends Node { + _children: Node[]; + } } diff --git a/src/compiler/utilities.ts b/src/compiler/utilities.ts index 4356486d3b5..fab5e9f8f8b 100644 --- a/src/compiler/utilities.ts +++ b/src/compiler/utilities.ts @@ -203,16 +203,54 @@ namespace ts { return !nodeIsMissing(node); } - export function getTokenPosOfNode(node: Node, sourceFile?: SourceFile): number { + export function getTokenPosOfNode(node: Node, sourceFile?: SourceFile, includeJsDocComment?: boolean): number { // With nodes that have no width (i.e. 'Missing' nodes), we actually *don't* // want to skip trivia because this will launch us forward to the next token. if (nodeIsMissing(node)) { return node.pos; } + if (isJSDocNode(node)) { + return skipTrivia((sourceFile || getSourceFileOfNode(node)).text, node.pos, /*stopAfterLineBreak*/ false, /*stopAtComments*/ true); + } + + if (node.jsDocComments && node.jsDocComments.length > 0 && includeJsDocComment) { + return getTokenPosOfNode(node.jsDocComments[0]); + } + + if (node.kind === SyntaxKind.SyntaxList) { + const childrenPoses = ts.map((node)._children, child => getTokenPosOfNode(child, sourceFile, includeJsDocComment)); + return Math.min(...childrenPoses); + } + return skipTrivia((sourceFile || getSourceFileOfNode(node)).text, node.pos); } + export function isJSDocNode(node: Node) { + return node.kind >= SyntaxKind.FirstJSDocNode && node.kind <= SyntaxKind.LastJSDocNode; + } + export function isJSDocType(node: Node) { + switch (node.kind) { + case SyntaxKind.JSDocAllType: + case SyntaxKind.JSDocUnknownType: + case SyntaxKind.JSDocArrayType: + case SyntaxKind.JSDocUnionType: + case SyntaxKind.JSDocTupleType: + case SyntaxKind.JSDocNullableType: + case SyntaxKind.JSDocNonNullableType: + case SyntaxKind.JSDocRecordType: + case SyntaxKind.JSDocRecordMember: + case SyntaxKind.JSDocTypeReference: + case SyntaxKind.JSDocOptionalType: + case SyntaxKind.JSDocFunctionType: + case SyntaxKind.JSDocVariadicType: + case SyntaxKind.JSDocConstructorType: + case SyntaxKind.JSDocThisType: + return true; + } + return false; + } + export function getNonDecoratorTokenPosOfNode(node: Node, sourceFile?: SourceFile): number { if (nodeIsMissing(node) || !node.decorators) { return getTokenPosOfNode(node, sourceFile); @@ -1199,26 +1237,28 @@ namespace ts { return undefined; } - const jsDocComment = getJSDocComment(node, checkParentVariableStatement); - if (!jsDocComment) { + const jsDocComments = getJSDocComments(node, checkParentVariableStatement); + if (!jsDocComments) { return undefined; } - for (const tag of jsDocComment.tags) { - if (tag.kind === kind) { - return tag; + for (const jsDocComment of jsDocComments) { + for (const tag of jsDocComment.tags) { + if (tag.kind === kind) { + return tag; + } } } } - function getJSDocComment(node: Node, checkParentVariableStatement: boolean): JSDocComment { - if (node.jsDocComment) { - return node.jsDocComment; + function getJSDocComments(node: Node, checkParentVariableStatement: boolean): JSDocComment[] { + if (node.jsDocComments) { + return node.jsDocComments; } - // Try to recognize this pattern when node is initializer of variable declaration and JSDoc comments are on containing variable statement. - // /** + // Try to recognize this pattern when node is initializer of variable declaration and JSDoc comments are on containing variable statement. + // /** // * @param {number} name - // * @returns {number} + // * @returns {number} // */ // var x = function(name) { return name.length; } if (checkParentVariableStatement) { @@ -1229,7 +1269,7 @@ namespace ts { const variableStatementNode = isInitializerOfVariableDeclarationInStatement ? node.parent.parent.parent : undefined; if (variableStatementNode) { - return variableStatementNode.jsDocComment; + return variableStatementNode.jsDocComments; } // Also recognize when the node is the RHS of an assignment expression @@ -1240,12 +1280,12 @@ namespace ts { (parent as BinaryExpression).operatorToken.kind === SyntaxKind.EqualsToken && parent.parent.kind === SyntaxKind.ExpressionStatement; if (isSourceOfAssignmentExpressionStatement) { - return parent.parent.jsDocComment; + return parent.parent.jsDocComments; } const isPropertyAssignmentExpression = parent && parent.kind === SyntaxKind.PropertyAssignment; if (isPropertyAssignmentExpression) { - return parent.jsDocComment; + return parent.jsDocComments; } } @@ -1270,14 +1310,16 @@ namespace ts { // annotation. const parameterName = (parameter.name).text; - const jsDocComment = getJSDocComment(parameter.parent, /*checkParentVariableStatement*/ true); - if (jsDocComment) { - for (const tag of jsDocComment.tags) { - if (tag.kind === SyntaxKind.JSDocParameterTag) { - const parameterTag = tag; - const name = parameterTag.preParameterName || parameterTag.postParameterName; - if (name.text === parameterName) { - return parameterTag; + const jsDocComments = getJSDocComments(parameter.parent, /*checkParentVariableStatement*/ true); + if (jsDocComments) { + for (const jsDocComment of jsDocComments) { + for (const tag of jsDocComment.tags) { + if (tag.kind === SyntaxKind.JSDocParameterTag) { + const parameterTag = tag; + const name = parameterTag.preParameterName || parameterTag.postParameterName; + if (name.text === parameterName) { + return parameterTag; + } } } } @@ -1374,6 +1416,7 @@ namespace ts { case SyntaxKind.TypeAliasDeclaration: case SyntaxKind.TypeParameter: case SyntaxKind.VariableDeclaration: + case SyntaxKind.JSDocTypedefTag: return true; } return false; diff --git a/src/harness/harness.ts b/src/harness/harness.ts index ddaca642094..330f57cf5c9 100644 --- a/src/harness/harness.ts +++ b/src/harness/harness.ts @@ -1,7 +1,7 @@ // // Copyright (c) Microsoft Corporation. All rights reserved. -// +// // Licensed under the Apache License, Version 2.0 (the "License"); // you may not use this file except in compliance with the License. // You may obtain a copy of the License at @@ -217,6 +217,14 @@ namespace Utils { return k; } + if (k === (ts).SyntaxKind.FirstJSDocCommentNode || k === (ts).SyntaxKind.LastJSDocCommentNode) { + for (const kindName in (ts).SyntaxKind) { + if ((ts).SyntaxKind[kindName] === k) { + return kindName; + } + } + } + return (ts).SyntaxKind[k]; } @@ -346,7 +354,7 @@ namespace Utils { assert.equal(node1.end, node2.end, "node1.end !== node2.end"); assert.equal(node1.kind, node2.kind, "node1.kind !== node2.kind"); - // call this on both nodes to ensure all propagated flags have been set (and thus can be + // call this on both nodes to ensure all propagated flags have been set (and thus can be // compared). assert.equal(ts.containsParseError(node1), ts.containsParseError(node2)); assert.equal(node1.flags, node2.flags, "node1.flags !== node2.flags"); @@ -753,7 +761,7 @@ namespace Harness { (emittedFile: string, emittedLine: number, emittedColumn: number, sourceFile: string, sourceLine: number, sourceColumn: number, sourceName: string): void; } - // Settings + // Settings export let userSpecifiedRoot = ""; export let lightMode = false; @@ -792,7 +800,7 @@ namespace Harness { fileName: string, sourceText: string, languageVersion: ts.ScriptTarget) { - // We'll only assert invariants outside of light mode. + // We'll only assert invariants outside of light mode. const shouldAssertInvariants = !Harness.lightMode; // Only set the parent nodes if we're asserting invariants. We don't need them otherwise. @@ -887,7 +895,7 @@ namespace Harness { libFiles?: string; } - // Additional options not already in ts.optionDeclarations + // Additional options not already in ts.optionDeclarations const harnessOptionDeclarations: ts.CommandLineOption[] = [ { name: "allowNonTsExtensions", type: "boolean" }, { name: "useCaseSensitiveFileNames", type: "boolean" }, @@ -1135,7 +1143,7 @@ namespace Harness { errLines.forEach(e => outputLines.push(e)); // do not count errors from lib.d.ts here, they are computed separately as numLibraryDiagnostics - // if lib.d.ts is explicitly included in input files and there are some errors in it (i.e. because of duplicate identifiers) + // if lib.d.ts is explicitly included in input files and there are some errors in it (i.e. because of duplicate identifiers) // then they will be added twice thus triggering 'total errors' assertion with condition // 'totalErrorsReportedInNonLibraryFiles + numLibraryDiagnostics + numTest262HarnessDiagnostics, diagnostics.length @@ -1445,7 +1453,7 @@ namespace Harness { }; testUnitData.push(newTestFile2); - // unit tests always list files explicitly + // unit tests always list files explicitly const parseConfigHost: ts.ParseConfigHost = { readDirectory: (name) => [] }; diff --git a/src/services/services.ts b/src/services/services.ts index a634b2baf1a..d85d478fcbe 100644 --- a/src/services/services.ts +++ b/src/services/services.ts @@ -20,7 +20,7 @@ namespace ts { getChildCount(sourceFile?: SourceFile): number; getChildAt(index: number, sourceFile?: SourceFile): Node; getChildren(sourceFile?: SourceFile): Node[]; - getStart(sourceFile?: SourceFile): number; + getStart(sourceFile?: SourceFile, includeJsDocComment?: boolean): number; getFullStart(): number; getEnd(): number; getWidth(sourceFile?: SourceFile): number; @@ -170,6 +170,8 @@ namespace ts { "static", "throws", "type", + "typedef", + "property", "version" ]; let jsDocCompletionEntries: CompletionEntry[]; @@ -187,6 +189,7 @@ namespace ts { public end: number; public flags: NodeFlags; public parent: Node; + public jsDocComments: JSDocComment[]; private _children: Node[]; constructor(kind: SyntaxKind, pos: number, end: number) { @@ -201,8 +204,8 @@ namespace ts { return getSourceFileOfNode(this); } - public getStart(sourceFile?: SourceFile): number { - return getTokenPosOfNode(this, sourceFile); + public getStart(sourceFile?: SourceFile, includeJsDocComment?: boolean): number { + return getTokenPosOfNode(this, sourceFile, includeJsDocComment); } public getFullStart(): number { @@ -233,12 +236,14 @@ namespace ts { return (sourceFile || this.getSourceFile()).text.substring(this.getStart(), this.getEnd()); } - private addSyntheticNodes(nodes: Node[], pos: number, end: number): number { + private addSyntheticNodes(nodes: Node[], pos: number, end: number, useJSDocScanner?: boolean): number { scanner.setTextPos(pos); while (pos < end) { - const token = scanner.scan(); + const token = useJSDocScanner ? scanner.scanJSDocToken() : scanner.scan(); const textPos = scanner.getTextPos(); - nodes.push(createNode(token, pos, textPos, 0, this)); + if (textPos <= end) { + nodes.push(createNode(token, pos, textPos, 0, this)); + } pos = textPos; } return pos; @@ -268,20 +273,36 @@ namespace ts { scanner.setText((sourceFile || this.getSourceFile()).text); children = []; let pos = this.pos; + const isJSDocTag = + this.kind === SyntaxKind.JSDocComment || + this.kind === SyntaxKind.JSDocParameterTag || + this.kind === SyntaxKind.JSDocTag || + this.kind === SyntaxKind.JSDocParameterTag || + this.kind === SyntaxKind.JSDocReturnTag || + this.kind === SyntaxKind.JSDocTypeTag || + this.kind === SyntaxKind.JSDocTemplateTag || + this.kind === SyntaxKind.JSDocTypedefTag || + this.kind === SyntaxKind.JSDocPropertyTag; const processNode = (node: Node) => { if (pos < node.pos) { - pos = this.addSyntheticNodes(children, pos, node.pos); + pos = this.addSyntheticNodes(children, pos, node.pos, /*useJSDocScanner*/ isJSDocTag); } children.push(node); pos = node.end; }; const processNodes = (nodes: NodeArray) => { if (pos < nodes.pos) { - pos = this.addSyntheticNodes(children, pos, nodes.pos); + pos = this.addSyntheticNodes(children, pos, nodes.pos, /*useJSDocScanner*/ isJSDocTag); } children.push(this.createSyntaxList(>nodes)); pos = nodes.end; }; + // jsDocComments need to be the first children + if (this.jsDocComments) { + for (const jsDocComment of this.jsDocComments) { + processNode(jsDocComment); + } + } forEachChild(this, processNode, processNodes); if (pos < this.end) { this.addSyntheticNodes(children, pos, this.end); @@ -1883,7 +1904,7 @@ namespace ts { options.isolatedModules = true; - // transpileModule does not write anything to disk so there is no need to verify that there are no conflicts between input and output paths. + // transpileModule does not write anything to disk so there is no need to verify that there are no conflicts between input and output paths. options.suppressOutputPathCheck = true; // Filename can be non-ts file. @@ -5499,7 +5520,7 @@ namespace ts { const sourceFile = getValidSourceFile(fileName); - const node = getTouchingPropertyName(sourceFile, position); + const node = getTouchingPropertyName(sourceFile, position, /*includeJsDocComment*/ true); if (!node) { return undefined; } @@ -5808,7 +5829,8 @@ namespace ts { const sourceFile = container.getSourceFile(); const tripleSlashDirectivePrefixRegex = /^\/\/\/\s* 0) { + for (const jsDocComment of node.jsDocComments) { + forEachChild(jsDocComment, walk); + } + } } } } diff --git a/src/services/utilities.ts b/src/services/utilities.ts index 8006fea2c07..5ea011f2226 100644 --- a/src/services/utilities.ts +++ b/src/services/utilities.ts @@ -260,29 +260,29 @@ namespace ts { /* Gets the token whose text has range [start, end) and * position >= start and (position < end or (position === end && token is keyword or identifier)) */ - export function getTouchingWord(sourceFile: SourceFile, position: number): Node { - return getTouchingToken(sourceFile, position, n => isWord(n.kind)); + export function getTouchingWord(sourceFile: SourceFile, position: number, includeJsDocComment = false): Node { + return getTouchingToken(sourceFile, position, n => isWord(n.kind), includeJsDocComment); } /* Gets the token whose text has range [start, end) and position >= start * and (position < end or (position === end && token is keyword or identifier or numeric/string literal)) */ - export function getTouchingPropertyName(sourceFile: SourceFile, position: number): Node { - return getTouchingToken(sourceFile, position, n => isPropertyName(n.kind)); + export function getTouchingPropertyName(sourceFile: SourceFile, position: number, includeJsDocComment = false): Node { + return getTouchingToken(sourceFile, position, n => isPropertyName(n.kind), includeJsDocComment); } /** Returns the token if position is in [start, end) or if position === end and includeItemAtEndPosition(token) === true */ - export function getTouchingToken(sourceFile: SourceFile, position: number, includeItemAtEndPosition?: (n: Node) => boolean): Node { - return getTokenAtPositionWorker(sourceFile, position, /*allowPositionInLeadingTrivia*/ false, includeItemAtEndPosition); + export function getTouchingToken(sourceFile: SourceFile, position: number, includeItemAtEndPosition?: (n: Node) => boolean, includeJsDocComment = false): Node { + return getTokenAtPositionWorker(sourceFile, position, /*allowPositionInLeadingTrivia*/ false, includeItemAtEndPosition, includeJsDocComment); } /** Returns a token if position is in [start-of-leading-trivia, end) */ - export function getTokenAtPosition(sourceFile: SourceFile, position: number): Node { - return getTokenAtPositionWorker(sourceFile, position, /*allowPositionInLeadingTrivia*/ true, /*includeItemAtEndPosition*/ undefined); + export function getTokenAtPosition(sourceFile: SourceFile, position: number, includeJsDocComment = false): Node { + return getTokenAtPositionWorker(sourceFile, position, /*allowPositionInLeadingTrivia*/ true, /*includeItemAtEndPosition*/ undefined, includeJsDocComment); } /** Get the token whose text contains the position */ - function getTokenAtPositionWorker(sourceFile: SourceFile, position: number, allowPositionInLeadingTrivia: boolean, includeItemAtEndPosition: (n: Node) => boolean): Node { + function getTokenAtPositionWorker(sourceFile: SourceFile, position: number, allowPositionInLeadingTrivia: boolean, includeItemAtEndPosition: (n: Node) => boolean, includeJsDocComment = false): Node { let current: Node = sourceFile; outer: while (true) { if (isToken(current)) { @@ -290,10 +290,34 @@ namespace ts { return current; } + if (includeJsDocComment) { + const jsDocChildren = ts.filter(current.getChildren(), isJSDocNode); + for (const jsDocChild of jsDocChildren) { + let start = allowPositionInLeadingTrivia ? jsDocChild.getFullStart() : jsDocChild.getStart(sourceFile, includeJsDocComment); + if (start <= position) { + let end = jsDocChild.getEnd(); + if (position < end || (position === end && jsDocChild.kind === SyntaxKind.EndOfFileToken)) { + current = jsDocChild; + continue outer; + } + else if (includeItemAtEndPosition && end === position) { + let previousToken = findPrecedingToken(position, sourceFile, jsDocChild); + if (previousToken && includeItemAtEndPosition(previousToken)) { + return previousToken; + } + } + } + } + } + // find the child that contains 'position' for (let i = 0, n = current.getChildCount(sourceFile); i < n; i++) { let child = current.getChildAt(i); - let start = allowPositionInLeadingTrivia ? child.getFullStart() : child.getStart(sourceFile); + // all jsDocComment nodes were already visited + if (isJSDocNode(child)) { + continue; + } + let start = allowPositionInLeadingTrivia ? child.getFullStart() : child.getStart(sourceFile, includeJsDocComment); if (start <= position) { let end = child.getEnd(); if (position < end || (position === end && child.kind === SyntaxKind.EndOfFileToken)) { @@ -308,6 +332,7 @@ namespace ts { } } } + return current; } } @@ -501,11 +526,13 @@ namespace ts { } if (node) { - let jsDocComment = node.jsDocComment; - if (jsDocComment) { - for (let tag of jsDocComment.tags) { - if (tag.pos <= position && position <= tag.end) { - return tag; + let jsDocComments = node.jsDocComments; + if (jsDocComments) { + for (const jsDocComment of jsDocComments) { + for (const tag of jsDocComment.tags) { + if (tag.pos <= position && position <= tag.end) { + return tag; + } } } } @@ -615,7 +642,7 @@ namespace ts { // [a,b,c] from: // [a, b, c] = someExpression; if (node.parent.kind === SyntaxKind.BinaryExpression && - (node.parent).left === node && + (node.parent).left === node && (node.parent).operatorToken.kind === SyntaxKind.EqualsToken) { return true; } @@ -629,7 +656,7 @@ namespace ts { // [a, b, c] of // [x, [a, b, c] ] = someExpression - // or + // or // {x, a: {a, b, c} } = someExpression if (isArrayLiteralOrObjectLiteralDestructuringPattern(node.parent.kind === SyntaxKind.PropertyAssignment ? node.parent.parent : node.parent)) { return true; diff --git a/tests/cases/fourslash/commentsVariables.ts b/tests/cases/fourslash/commentsVariables.ts index d62e1e9bee8..b2cf6c9cb9b 100644 --- a/tests/cases/fourslash/commentsVariables.ts +++ b/tests/cases/fourslash/commentsVariables.ts @@ -41,60 +41,60 @@ ////} ////var x = fo/*15*/o2; -goTo.marker('1'); -verify.quickInfoIs("var myVariable: number", "This is my variable"); +// goTo.marker('1'); +// verify.quickInfoIs("var myVariable: number", "This is my variable"); -goTo.marker('2'); -verify.completionListContains("myVariable", "var myVariable: number", "This is my variable"); +// goTo.marker('2'); +// verify.completionListContains("myVariable", "var myVariable: number", "This is my variable"); -goTo.marker('3'); -verify.completionListContains("myVariable", "var myVariable: number", "This is my variable"); -verify.completionListContains("d", "var d: number", "d variable"); +// goTo.marker('3'); +// verify.completionListContains("myVariable", "var myVariable: number", "This is my variable"); +// verify.completionListContains("d", "var d: number", "d variable"); goTo.marker('4'); verify.completionListContains("foo", "function foo(): void", "foos comment"); verify.completionListContains("fooVar", "var fooVar: () => void", "fooVar comment"); -goTo.marker('5'); -verify.currentSignatureHelpDocCommentIs("foos comment"); -goTo.marker('5q'); -verify.quickInfoIs("function foo(): void", "foos comment"); +// goTo.marker('5'); +// verify.currentSignatureHelpDocCommentIs("foos comment"); +// goTo.marker('5q'); +// verify.quickInfoIs("function foo(): void", "foos comment"); -goTo.marker('6'); -verify.currentSignatureHelpDocCommentIs(""); -goTo.marker('6q'); -verify.quickInfoIs("var fooVar: () => void", ""); +// goTo.marker('6'); +// verify.currentSignatureHelpDocCommentIs(""); +// goTo.marker('6q'); +// verify.quickInfoIs("var fooVar: () => void", ""); -goTo.marker('7'); -verify.completionListContains("foo", "function foo(): void", "foos comment"); -verify.completionListContains("fooVar", "var fooVar: () => void", "fooVar comment"); +// goTo.marker('7'); +// verify.completionListContains("foo", "function foo(): void", "foos comment"); +// verify.completionListContains("fooVar", "var fooVar: () => void", "fooVar comment"); -goTo.marker('8'); -verify.currentSignatureHelpDocCommentIs("foos comment"); -goTo.marker('8q'); -verify.quickInfoIs("function foo(): void", "foos comment"); +// goTo.marker('8'); +// verify.currentSignatureHelpDocCommentIs("foos comment"); +// goTo.marker('8q'); +// verify.quickInfoIs("function foo(): void", "foos comment"); -goTo.marker('9'); -verify.currentSignatureHelpDocCommentIs(""); -goTo.marker('9q'); -verify.quickInfoIs("var fooVar: () => void", ""); -goTo.marker('9aq'); -verify.quickInfoIs("var fooVar: () => void", "fooVar comment"); +// goTo.marker('9'); +// verify.currentSignatureHelpDocCommentIs(""); +// goTo.marker('9q'); +// verify.quickInfoIs("var fooVar: () => void", ""); +// goTo.marker('9aq'); +// verify.quickInfoIs("var fooVar: () => void", "fooVar comment"); -goTo.marker('10'); -verify.completionListContains("i", "var i: c", "instance comment"); +// goTo.marker('10'); +// verify.completionListContains("i", "var i: c", "instance comment"); -goTo.marker('11'); -verify.completionListContains("i1_i", "var i1_i: i1", "interface instance comments"); +// goTo.marker('11'); +// verify.completionListContains("i1_i", "var i1_i: i1", "interface instance comments"); -goTo.marker('12'); -verify.quickInfoIs("var fooVar: () => void", "fooVar comment"); +// goTo.marker('12'); +// verify.quickInfoIs("var fooVar: () => void", "fooVar comment"); -goTo.marker('13'); -verify.quickInfoIs("var fooVar: () => void", "fooVar comment"); +// goTo.marker('13'); +// verify.quickInfoIs("var fooVar: () => void", "fooVar comment"); -goTo.marker('14'); -verify.quickInfoIs("function foo(): void", "foos comment"); +// goTo.marker('14'); +// verify.quickInfoIs("function foo(): void", "foos comment"); -goTo.marker('15'); -verify.quickInfoIs("function foo2(a: number): void (+1 overload)", ""); +// goTo.marker('15'); +// verify.quickInfoIs("function foo2(a: number): void (+1 overload)", ""); diff --git a/tests/cases/fourslash/doesnotwork.ts b/tests/cases/fourslash/doesnotwork.ts new file mode 100644 index 00000000000..2f415563c21 --- /dev/null +++ b/tests/cases/fourslash/doesnotwork.ts @@ -0,0 +1,45 @@ +/// + +/////** This is my variable*/ +////var myVariable = 10; +//// +/////** d variable*/ +////var d = 10; +////myVariable = d; +//// +/////** foos comment*/ +////function foo() { +////} +/////** fooVar comment*/ +////var fooVar: () => void; +/////*4*/ +////foo(); +////fooVar(); +////fooVar = foo; +//// +////foo(); +////fooVar(); +////var fooVarVar = fooVar; +/////**class comment*/ +////class c { +//// /** constructor comment*/ +//// constructor() { +//// } +////} +/////**instance comment*/ +////var i = new c(); +//// +/////** interface comments*/ +////interface i1 { +////} +/////**interface instance comments*/ +////var i1_i: i1; +//// +////function foo2(a: number): void; +////function foo2(b: string): void; +////function foo2(aOrb) { +////} +////var x = foo2; + +goTo.marker('4'); +verify.completionListContains("fooVar", "var fooVar: () => void", "fooVar comment"); diff --git a/tests/cases/fourslash/server/jsdocCompletions.ts b/tests/cases/fourslash/server/jsdocCompletions.ts deleted file mode 100644 index c8dc1bbe481..00000000000 --- a/tests/cases/fourslash/server/jsdocCompletions.ts +++ /dev/null @@ -1,15 +0,0 @@ -/// - -// @allowNonTsExtensions: true -// @Filename: jsdocCompletion_typedef.js -//// /** -//// * @typedef {Object} Person -//// * @property {string} personName -//// * @type {Person} -//// */ -//// var x1; -//// x1/**/ - -goTo.marker(); -edit.insert('.'); -verify.memberListContains('personName'); \ No newline at end of file diff --git a/tests/cases/fourslash/server/jsdocTypedefTag.ts b/tests/cases/fourslash/server/jsdocTypedefTag.ts new file mode 100644 index 00000000000..e645b518020 --- /dev/null +++ b/tests/cases/fourslash/server/jsdocTypedefTag.ts @@ -0,0 +1,64 @@ +/// + +// @allowNonTsExtensions: true +// @Filename: jsdocCompletion_typedef.js + +//// /** @typedef {(string | number)} NumberLike */ +//// +//// /** +//// * @typedef Animal +//// * @type {Object} +//// * @property {string} animalName +//// * @property {number} animalAge +//// */ +//// +//// /** +//// * @typedef {Object} Person +//// * @property {string} personName +//// * @property {number} personAge +//// */ +//// +//// /** +//// * @typedef {Object} +//// * @property {string} catName +//// * @property {number} catAge +//// */ +//// var Cat; +//// +//// /** @typedef {{ dogName: string, dogAge: number }} */ +//// var Dog; +//// +//// /** @type {NumberLike} */ +//// var numberLike; numberLike./*numberLike*/ +//// +//// /** @type {Person} */ +//// var p;p./*person*/ +//// +//// /** @type {Animal} */ +//// var a;a./*animal*/ +//// +//// /** @type {Cat} */ +//// var c;c./*cat*/ +//// +//// /** @type {Dog} */ +//// var d;d./*dog*/ + +goTo.marker('numberLike'); +verify.memberListContains('charAt'); +verify.memberListContains('toExponential'); + +goTo.marker('person'); +verify.memberListContains('personName'); +verify.memberListContains('personAge'); + +goTo.marker('animal'); +verify.memberListContains('animalName'); +verify.memberListContains('animalAge'); + +goTo.marker('dog'); +verify.memberListContains('dogName'); +verify.memberListContains('dogAge'); + +goTo.marker('cat'); +verify.memberListContains('catName'); +verify.memberListContains('catAge'); \ No newline at end of file diff --git a/tests/cases/fourslash/server/jsdocTypedefTagGoToDefinition.ts b/tests/cases/fourslash/server/jsdocTypedefTagGoToDefinition.ts new file mode 100644 index 00000000000..4db14611938 --- /dev/null +++ b/tests/cases/fourslash/server/jsdocTypedefTagGoToDefinition.ts @@ -0,0 +1,29 @@ +/// + +// @allowNonTsExtensions: true +// @Filename: jsdocCompletion_typedef.js + +//// /** +//// * @typedef {Object} Person +//// * /*1*/@property {string} personName +//// * @property {number} personAge +//// */ +//// +//// /** +//// * @typedef {{ /*2*/animalName: string, animalAge: number }} Animal +//// */ +//// +//// /** @type {Person} */ +//// var person; person.personName/*3*/ +//// +//// /** @type {Animal} */ +//// var animal; animal.animalName/*4*/ + +goTo.file('jsdocCompletion_typedef.js'); +goTo.marker('3'); +goTo.definition(); +verify.caretAtMarker('1'); + +goTo.marker('4'); +goTo.definition(); +verify.caretAtMarker('2'); diff --git a/tests/cases/fourslash/server/jsdocTypedefTagRename01.ts b/tests/cases/fourslash/server/jsdocTypedefTagRename01.ts new file mode 100644 index 00000000000..776d0180b06 --- /dev/null +++ b/tests/cases/fourslash/server/jsdocTypedefTagRename01.ts @@ -0,0 +1,20 @@ +/// + +// @allowNonTsExtensions: true +// @Filename: jsDocTypedef_form1.js +//// +//// /** @typedef {(string | number)} */ +//// var /*1*/[|NumberLike|]; +//// +//// /*2*/[|NumberLike|] = 10; +//// +//// /** @type {/*3*/[|NumberLike|]} */ +//// var numberLike; + +goTo.file('jsDocTypedef_form1.js') +goTo.marker('1'); +verify.renameLocations(/*findInStrings*/ false, /*findInComments*/ true); +goTo.marker('2'); +verify.renameLocations(/*findInStrings*/ false, /*findInComments*/ true); +goTo.marker('3'); +verify.renameLocations(/*findInStrings*/ false, /*findInComments*/ true); \ No newline at end of file diff --git a/tests/cases/fourslash/server/jsdocTypedefTagRename02.ts b/tests/cases/fourslash/server/jsdocTypedefTagRename02.ts new file mode 100644 index 00000000000..7f1d422d971 --- /dev/null +++ b/tests/cases/fourslash/server/jsdocTypedefTagRename02.ts @@ -0,0 +1,15 @@ +/// + +// @allowNonTsExtensions: true +// @Filename: jsDocTypedef_form2.js +//// +//// /** @typedef {(string | number)} /*1*/[|NumberLike|] */ +//// +//// /** @type {/*2*/[|NumberLike|]} */ +//// var numberLike; + +goTo.file('jsDocTypedef_form2.js') +goTo.marker('1'); +verify.renameLocations(/*findInStrings*/ false, /*findInComments*/ true); +goTo.marker('2'); +verify.renameLocations(/*findInStrings*/ false, /*findInComments*/ true); \ No newline at end of file diff --git a/tests/cases/fourslash/server/jsdocTypedefTagRename03.ts b/tests/cases/fourslash/server/jsdocTypedefTagRename03.ts new file mode 100644 index 00000000000..c1b38945806 --- /dev/null +++ b/tests/cases/fourslash/server/jsdocTypedefTagRename03.ts @@ -0,0 +1,20 @@ +/// + +// @allowNonTsExtensions: true +// @Filename: jsDocTypedef_form3.js +//// +//// /** +//// * @typedef /*1*/[|Person|] +//// * @type {Object} +//// * @property {number} age +//// * @property {string} name +//// */ +//// +//// /** @type {/*2*/[|Person|]} */ +//// var person; + +goTo.file('jsDocTypedef_form3.js') +goTo.marker('1'); +verify.renameLocations(/*findInStrings*/ false, /*findInComments*/ true); +goTo.marker('2'); +verify.renameLocations(/*findInStrings*/ false, /*findInComments*/ true); \ No newline at end of file diff --git a/tests/cases/fourslash/server/jsdocTypedefTagRename04.ts b/tests/cases/fourslash/server/jsdocTypedefTagRename04.ts new file mode 100644 index 00000000000..b7bf220512a --- /dev/null +++ b/tests/cases/fourslash/server/jsdocTypedefTagRename04.ts @@ -0,0 +1,24 @@ +/// + +// @allowNonTsExtensions: true +// @Filename: jsDocTypedef_form2.js +//// +//// function test1() { +//// /** @typedef {(string | number)} NumberLike */ +//// +//// /** @type {/*1*/NumberLike} */ +//// var numberLike; +//// } +//// function test2() { +//// /** @typedef {(string | number)} NumberLike2 */ +//// +//// /** @type {NumberLike2} */ +//// var n/*2*/umberLike2; +//// } + +goTo.marker('2'); +verify.quickInfoExists(); +goTo.marker('1'); +edit.insert('111'); +goTo.marker('2'); +verify.quickInfoExists(); \ No newline at end of file diff --git a/tests/cases/unittests/jsDocParsing.ts b/tests/cases/unittests/jsDocParsing.ts index 1bd7b1147ca..0540c02d135 100644 --- a/tests/cases/unittests/jsDocParsing.ts +++ b/tests/cases/unittests/jsDocParsing.ts @@ -1004,7 +1004,8 @@ module ts { if (result !== expected) { // Turn on a human-readable diff if (typeof require !== 'undefined') { - require('chai').config.showDiff = true; + const chai = require('chai'); + chai.config.showDiff = true; chai.expect(JSON.parse(result)).equal(JSON.parse(expected)); } else { diff --git a/tests/webTestServer.js b/tests/webTestServer.js new file mode 100644 index 00000000000..b47da7bfe76 --- /dev/null +++ b/tests/webTestServer.js @@ -0,0 +1,288 @@ +/// +"use strict"; +var http = require("http"); +var fs = require("fs"); +var path = require("path"); +var url = require("url"); +var child_process = require("child_process"); +var os = require("os"); +/// Command line processing /// +if (process.argv[2] == '--help') { + console.log('Runs a node server on port 8888 by default, looking for tests folder in the current directory\n'); + console.log('Syntax: node nodeServer.js [port] [typescriptEnlistmentDirectory] [tests] [--browser] [--verbose]\n'); + console.log('Examples: \n\tnode nodeServer.js 8888 .'); + console.log('\tnode nodeServer.js 3000 D:/src/typescript/public --verbose IE'); +} +function switchToForwardSlashes(path) { + return path.replace(/\\/g, "/").replace(/\/\//g, '/'); +} +var defaultPort = 8888; +var port = process.argv[2] || defaultPort; +var rootDir = switchToForwardSlashes(__dirname + '/../'); +var browser; +if (process.argv[3]) { + browser = process.argv[3]; + if (browser !== 'chrome' && browser !== 'IE') { + console.log('Invalid command line arguments. Got ' + browser + ' but expected chrome, IE or nothing.'); + } +} +var grep = process.argv[4]; +var verbose = false; +if (process.argv[5] == '--verbose') { + verbose = true; +} +else if (process.argv[5] && process.argv[5] !== '--verbose') { + console.log('Invalid command line arguments. Got ' + process.argv[5] + ' but expected --verbose or nothing.'); +} +/// Utils /// +function log(msg) { + if (verbose) { + console.log(msg); + } +} +// Copied from the compiler sources +function dir(path, spec, options) { + options = options || {}; + function filesInFolder(folder) { + var folder = switchToForwardSlashes(folder); + var paths = []; + // Everything after the current directory is relative + var baseDirectoryLength = process.cwd().length + 1; + try { + var files = fs.readdirSync(folder); + for (var i = 0; i < files.length; i++) { + var stat = fs.statSync(folder + "/" + files[i]); + if (options.recursive && stat.isDirectory()) { + paths = paths.concat(filesInFolder(folder + "/" + files[i])); + } + else if (stat.isFile() && (!spec || files[i].match(spec))) { + var relativePath = folder.substring(baseDirectoryLength); + paths.push(relativePath + "/" + files[i]); + } + } + } + catch (err) { + } + return paths; + } + return filesInFolder(path); +} +// fs.rmdirSync won't delete directories with files in it +function deleteFolderRecursive(path) { + if (fs.existsSync(path)) { + fs.readdirSync(path).forEach(function (file, index) { + var curPath = path + "/" + file; + if (fs.statSync(curPath).isDirectory()) { + deleteFolderRecursive(curPath); + } + else { + fs.unlinkSync(curPath); + } + }); + fs.rmdirSync(path); + } +} +; +function writeFile(path, data, opts) { + try { + fs.writeFileSync(path, data); + } + catch (e) { + // assume file was written to a directory that exists, if not, start recursively creating them as necessary + var parts = switchToForwardSlashes(path).split('/'); + for (var i = 0; i < parts.length; i++) { + var subDir = parts.slice(0, i).join('/'); + if (!fs.existsSync(subDir)) { + fs.mkdir(subDir); + } + } + fs.writeFileSync(path, data); + } +} +/// Request Handling /// +function handleResolutionRequest(filePath, res) { + var resolvedPath = path.resolve(filePath, ''); + resolvedPath = resolvedPath.substring(resolvedPath.indexOf('tests')); + resolvedPath = switchToForwardSlashes(resolvedPath); + send('success', res, resolvedPath); + return; +} +function send(result, res, contents, contentType) { + if (contentType === void 0) { contentType = "binary"; } + var responseCode = result === "success" ? 200 : result === "fail" ? 500 : result === 'unknown' ? 404 : parseInt(result); + res.writeHead(responseCode, { "Content-Type": contentType }); + res.end(contents); + return; +} +// Reads the data from a post request and passes it to the given callback +function processPost(req, res, callback) { + var queryData = ""; + if (typeof callback !== 'function') + return null; + if (req.method == 'POST') { + req.on('data', function (data) { + queryData += data; + if (queryData.length > 1e8) { + queryData = ""; + send("413", res, null); + console.log("ERROR: destroying connection"); + req.connection.destroy(); + } + }); + req.on('end', function () { + //res.post = url.parse(req.url).query; + callback(queryData); + }); + } + else { + send("405", res, null); + } +} +var RequestType; +(function (RequestType) { + RequestType[RequestType["GetFile"] = 0] = "GetFile"; + RequestType[RequestType["GetDir"] = 1] = "GetDir"; + RequestType[RequestType["ResolveFile"] = 2] = "ResolveFile"; + RequestType[RequestType["WriteFile"] = 3] = "WriteFile"; + RequestType[RequestType["DeleteFile"] = 4] = "DeleteFile"; + RequestType[RequestType["WriteDir"] = 5] = "WriteDir"; + RequestType[RequestType["DeleteDir"] = 6] = "DeleteDir"; + RequestType[RequestType["AppendFile"] = 7] = "AppendFile"; + RequestType[RequestType["Unknown"] = 8] = "Unknown"; +})(RequestType || (RequestType = {})); +function getRequestOperation(req, filename) { + if (req.method === 'GET' && req.url.indexOf('?') === -1) { + if (req.url.indexOf('.') !== -1) + return RequestType.GetFile; + else + return RequestType.GetDir; + } + else { + var queryData = url.parse(req.url, true).query; + if (req.method === 'GET' && queryData.resolve !== undefined) + return RequestType.ResolveFile; + // mocha uses ?grep= query string as equivalent to the --grep command line option used to filter tests + if (req.method === 'GET' && queryData.grep !== undefined) + return RequestType.GetFile; + if (req.method === 'POST' && queryData.action) { + var path = req.url.substr(0, req.url.lastIndexOf('?')); + var isFile = path.substring(path.lastIndexOf('/')).indexOf('.') !== -1; + switch (queryData.action.toUpperCase()) { + case 'WRITE': + return isFile ? RequestType.WriteFile : RequestType.WriteDir; + case 'DELETE': + return isFile ? RequestType.DeleteFile : RequestType.DeleteDir; + case 'APPEND': + return isFile ? RequestType.AppendFile : RequestType.Unknown; + } + } + return RequestType.Unknown; + } +} +function handleRequestOperation(req, res, operation, reqPath) { + switch (operation) { + case RequestType.GetDir: + var filesInFolder = dir(reqPath, "", { recursive: true }); + send('success', res, filesInFolder.join(',')); + break; + case RequestType.GetFile: + fs.readFile(reqPath, function (err, file) { + var ext = reqPath.substr(reqPath.lastIndexOf('.')); + var contentType = 'binary'; + if (ext === '.js') + contentType = 'text/javascript'; + else if (ext === '.css') + contentType = 'text/javascript'; + else if (ext === '.html') + contentType = 'text/html'; + err + ? send('fail', res, err.message, contentType) + : send('success', res, file, contentType); + }); + break; + case RequestType.ResolveFile: + var resolveRequest = req.url.match(/(.*)\?resolve/); + handleResolutionRequest(resolveRequest[1], res); + break; + case RequestType.WriteFile: + processPost(req, res, function (data) { + writeFile(reqPath, data, { recursive: true }); + }); + send('success', res, null); + break; + case RequestType.WriteDir: + fs.mkdirSync(reqPath); + send('success', res, null); + break; + case RequestType.DeleteFile: + if (fs.existsSync(reqPath)) { + fs.unlinkSync(reqPath); + } + send('success', res, null); + break; + case RequestType.DeleteDir: + if (fs.existsSync(reqPath)) { + fs.rmdirSync(reqPath); + } + send('success', res, null); + break; + case RequestType.AppendFile: + processPost(req, res, function (data) { + fs.appendFileSync(reqPath, data); + }); + send('success', res, null); + break; + case RequestType.Unknown: + default: + send('unknown', res, null); + break; + } +} +console.log("Static file server running at\n => http://localhost:" + port + "/\nCTRL + C to shutdown"); +http.createServer(function (req, res) { + log(req.method + ' ' + req.url); + var uri = url.parse(req.url).pathname; + var reqPath = path.join(process.cwd(), uri); + var operation = getRequestOperation(req, reqPath); + handleRequestOperation(req, res, operation, reqPath); +}).listen(8888); +var browserPath; +if ((browser && browser === 'chrome')) { + var defaultChromePath = ""; + switch (os.platform()) { + case "win32": + case "win64": + defaultChromePath = "C:/Program Files (x86)/Google/Chrome/Application/chrome.exe"; + break; + case "darwin": + defaultChromePath = "/Applications/Google Chrome.app/Contents/MacOS/Google Chrome"; + break; + case "linux": + defaultChromePath = "/opt/google/chrome/chrome"; + break; + default: + console.log("default Chrome location is unknown for platform '" + os.platform() + "'"); + break; + } + if (fs.existsSync(defaultChromePath)) { + browserPath = defaultChromePath; + } + else { + browserPath = browser; + } +} +else { + var defaultIEPath = 'C:/Program Files/Internet Explorer/iexplore.exe'; + if (fs.existsSync(defaultIEPath)) { + browserPath = defaultIEPath; + } + else { + browserPath = browser; + } +} +console.log('Using browser: ' + browserPath); +var queryString = grep ? "?grep=" + grep : ''; +child_process.spawn(browserPath, ['http://localhost:' + port + '/tests/webTestResults.html' + queryString], { + stdio: 'inherit' +}); +//# sourceMappingURL=file:///C:/Users/lizhe/Documents/github/TypeScript/tests/webTestServer.js.map \ No newline at end of file diff --git a/tests/webTestServer.js.map b/tests/webTestServer.js.map new file mode 100644 index 00000000000..1c924a872f4 --- /dev/null +++ b/tests/webTestServer.js.map @@ -0,0 +1 @@ +{"version":3,"file":"webTestServer.js","sourceRoot":"","sources":["file:///C:/Users/lizhe/Documents/github/TypeScript/tests/webTestServer.ts"],"names":[],"mappings":"AAAA,yDAAyD;;AAEzD,IAAO,IAAI,WAAW,MAAM,CAAC,CAAC;AAC9B,IAAO,EAAE,WAAW,IAAI,CAAC,CAAC;AAC1B,IAAO,IAAI,WAAW,MAAM,CAAC,CAAC;AAC9B,IAAO,GAAG,WAAW,KAAK,CAAC,CAAC;AAC5B,IAAO,aAAa,WAAW,eAAe,CAAC,CAAC;AAChD,IAAO,EAAE,WAAW,IAAI,CAAC,CAAC;AAE1B,+BAA+B;AAE/B,EAAE,CAAC,CAAC,OAAO,CAAC,IAAI,CAAC,CAAC,CAAC,IAAI,QAAQ,CAAC,CAAC,CAAC;IAC9B,OAAO,CAAC,GAAG,CAAC,iGAAiG,CAAC,CAAC;IAC/G,OAAO,CAAC,GAAG,CAAC,qGAAqG,CAAC,CAAC;IACnH,OAAO,CAAC,GAAG,CAAC,yCAAyC,CAAC,CAAC;IACvD,OAAO,CAAC,GAAG,CAAC,iEAAiE,CAAC,CAAC;AACnF,CAAC;AAED,gCAAgC,IAAY;IACxC,MAAM,CAAC,IAAI,CAAC,OAAO,CAAC,KAAK,EAAE,GAAG,CAAC,CAAC,OAAO,CAAC,OAAO,EAAE,GAAG,CAAC,CAAC;AAC1D,CAAC;AAED,IAAI,WAAW,GAAG,IAAI,CAAC;AACvB,IAAI,IAAI,GAAG,OAAO,CAAC,IAAI,CAAC,CAAC,CAAC,IAAI,WAAW,CAAC;AAC1C,IAAI,OAAO,GAAG,sBAAsB,CAAC,SAAS,GAAG,MAAM,CAAC,CAAC;AAEzD,IAAI,OAAe,CAA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AAC,IAAI,CAAC,CAAC;QACJ,WAAW,GAAG,OAAO,CAAC;IAC1B,CAAC;AACL,CAAC;AAAC,IAAI,CAAC,CAAC;IACJ,IAAI,aAAa,GAAG,iDAAiD,CAAC;IACtE,EAAE,CAAC,CAAC,EAAE,CAAC,UAAU,CAAC,aAAa,CAAC,CAAC,CAAC,CAAC;QAC/B,WAAW,GAAG,aAAa,CAAC;IAChC,CAAC;IAAC,IAAI,CAAC,CAAC;QACJ,WAAW,GAAG,OAAO,CAAC;IAC1B,CAAC;AACL,CAAC;AAED,OAAO,CAAC,GAAG,CAAC,iBAAiB,GAAG,WAAW,CAAC,CAAC;AAE7C,IAAI,WAAW,GAAG,IAAI,GAAG,QAAQ,GAAG,IAAI,GAAG,EAAE,CAAC;AAC9C,aAAa,CAAC,KAAK,CAAC,WAAW,EAAE,CAAC,mBAAmB,GAAG,IAAI,GAAG,4BAA4B,GAAG,WAAW,CAAC,EAAE;IACxG,KAAK,EAAE,SAAS;CACnB,CAAC,CAAC"} \ No newline at end of file From 0dddcf4b84ee3eba1413a1591984605ef4940708 Mon Sep 17 00:00:00 2001 From: zhengbli Date: Thu, 14 Apr 2016 16:51:22 -0700 Subject: [PATCH 05/41] code clean up --- src/compiler/parser.ts | 38 ++- src/compiler/types.ts | 2 + src/compiler/utilities.ts | 32 +-- src/harness/harness.ts | 4 +- src/services/services.ts | 9 +- tests/cases/fourslash/commentsVariables.ts | 80 +++--- tests/cases/fourslash/doesnotwork.ts | 45 ---- tests/webTestServer.js | 288 --------------------- tests/webTestServer.js.map | 1 - 9 files changed, 72 insertions(+), 427 deletions(-) delete mode 100644 tests/cases/fourslash/doesnotwork.ts delete mode 100644 tests/webTestServer.js delete mode 100644 tests/webTestServer.js.map diff --git a/src/compiler/parser.ts b/src/compiler/parser.ts index 058098eff1d..3a1c3c62f41 100644 --- a/src/compiler/parser.ts +++ b/src/compiler/parser.ts @@ -409,7 +409,6 @@ namespace ts { return visitNodes(cbNodes, (node).members); case SyntaxKind.JSDocPropertyTag: return visitNode(cbNode, (node).typeExpression) || - visitNode(cbNode, (node).type) || visitNode(cbNode, (node).name); } } @@ -4063,9 +4062,9 @@ namespace ts { const isAsync = !!(node.flags & NodeFlags.Async); node.name = isGenerator && isAsync ? doInYieldAndAwaitContext(parseOptionalIdentifier) : - isGenerator ? doInYieldContext(parseOptionalIdentifier) : - isAsync ? doInAwaitContext(parseOptionalIdentifier) : - parseOptionalIdentifier(); + isGenerator ? doInYieldContext(parseOptionalIdentifier) : + isAsync ? doInAwaitContext(parseOptionalIdentifier) : + parseOptionalIdentifier(); fillSignature(SyntaxKind.ColonToken, /*yieldContext*/ isGenerator, /*awaitContext*/ isAsync, /*requireCompleteParameterList*/ false, node); node.body = parseFunctionBlock(/*allowYield*/ isGenerator, /*allowAwait*/ isAsync, /*ignoreMissingOpenBrace*/ false); @@ -6121,15 +6120,13 @@ namespace ts { typedefTag.type = typedefTag.jsDocTypeTag.typeExpression.type; } } - if (!typedefTag.type && typedefTag.jsDocPropertyTags) { - const childrenTagPoses = ts.map(typedefTag.jsDocPropertyTags, tag => tag.pos); - const childrenTagEnds = ts.map(typedefTag.jsDocPropertyTags, tag => tag.end); - const pos = Math.min(...childrenTagPoses); - const end = Math.max(...childrenTagEnds); - const tagType = createNode(SyntaxKind.JSDocTypeLiteral, pos); - tagType.members = >[]; - addRange(tagType.members, typedefTag.jsDocPropertyTags); - typedefTag.type = finishNode(tagType, end); + if (!typedefTag.type && typedefTag.jsDocPropertyTags && typedefTag.jsDocPropertyTags.length > 0) { + const pos = typedefTag.jsDocPropertyTags[0].pos; + const end = typedefTag.jsDocPropertyTags[typedefTag.jsDocPropertyTags.length - 1].end; + const jsdocTypeLiteral = createNode(SyntaxKind.JSDocTypeLiteral, pos); + jsdocTypeLiteral.members = >[]; + addRange(jsdocTypeLiteral.members, typedefTag.jsDocPropertyTags); + typedefTag.type = finishNode(jsdocTypeLiteral, end); } } @@ -6303,23 +6300,18 @@ namespace ts { result.typeExpression = tryParseTypeExpression(); result = finishNode(result); - let typeTagPartOfParentTag = false; if (currentParentJSDocTag && currentParentJSDocTag.kind === SyntaxKind.JSDocTypedefTag) { const parentTag = currentParentJSDocTag; if (!parentTag.typeExpression && !parentTag.jsDocTypeTag) { - typeTagPartOfParentTag = true; parentTag.jsDocTypeTag = result; currentParentJSDocTagEnd = scanner.getStartPos(); + return result; } } - if (!typeTagPartOfParentTag) { - // If this @type tag is not part of the current parent tag, then - // it denotes the end of the current parent tag. - finishCurrentParentTag(); - return result; - } - - return undefined; + // If this @type tag is not part of the current parent tag, then + // it denotes the end of the current parent tag. + finishCurrentParentTag(); + return result; } function handlePropertyTag(atToken: Node, tagName: Identifier): JSDocPropertyTag { diff --git a/src/compiler/types.ts b/src/compiler/types.ts index 7743f7c35de..91ce1f1022f 100644 --- a/src/compiler/types.ts +++ b/src/compiler/types.ts @@ -1533,6 +1533,8 @@ namespace ts { export interface JSDocPropertyTag extends JSDocTag, TypeElement { name: Identifier; typeExpression: JSDocTypeExpression; + // Add a "type" property here to make the interface compatible + // with the "VariableLikeDeclaration" definition type: TypeNode; } diff --git a/src/compiler/utilities.ts b/src/compiler/utilities.ts index ebf6b42581a..2f29e477d69 100644 --- a/src/compiler/utilities.ts +++ b/src/compiler/utilities.ts @@ -279,13 +279,16 @@ namespace ts { return skipTrivia((sourceFile || getSourceFileOfNode(node)).text, node.pos, /*stopAfterLineBreak*/ false, /*stopAtComments*/ true); } - if (node.jsDocComments && node.jsDocComments.length > 0 && includeJsDocComment) { + if (includeJsDocComment && node.jsDocComments && node.jsDocComments.length > 0) { return getTokenPosOfNode(node.jsDocComments[0]); } - if (node.kind === SyntaxKind.SyntaxList) { - const childrenPoses = ts.map((node)._children, child => getTokenPosOfNode(child, sourceFile, includeJsDocComment)); - return Math.min(...childrenPoses); + // For a syntax list, it is possible that one of its children has JSDocComment nodes, while + // the syntax list itself considers them as normal trivia. Therefore if we simply skip + // trivia for the list, we may have skipped the JSDocComment as well. So we should process its + // first child to determine the actual position of its first token. + if (node.kind === SyntaxKind.SyntaxList && (node)._children.length > 0) { + return getTokenPosOfNode((node)._children[0], sourceFile, includeJsDocComment); } return skipTrivia((sourceFile || getSourceFileOfNode(node)).text, node.pos); @@ -294,27 +297,6 @@ namespace ts { export function isJSDocNode(node: Node) { return node.kind >= SyntaxKind.FirstJSDocNode && node.kind <= SyntaxKind.LastJSDocNode; } - export function isJSDocType(node: Node) { - switch (node.kind) { - case SyntaxKind.JSDocAllType: - case SyntaxKind.JSDocUnknownType: - case SyntaxKind.JSDocArrayType: - case SyntaxKind.JSDocUnionType: - case SyntaxKind.JSDocTupleType: - case SyntaxKind.JSDocNullableType: - case SyntaxKind.JSDocNonNullableType: - case SyntaxKind.JSDocRecordType: - case SyntaxKind.JSDocRecordMember: - case SyntaxKind.JSDocTypeReference: - case SyntaxKind.JSDocOptionalType: - case SyntaxKind.JSDocFunctionType: - case SyntaxKind.JSDocVariadicType: - case SyntaxKind.JSDocConstructorType: - case SyntaxKind.JSDocThisType: - return true; - } - return false; - } export function getNonDecoratorTokenPosOfNode(node: Node, sourceFile?: SourceFile): number { if (nodeIsMissing(node) || !node.decorators) { diff --git a/src/harness/harness.ts b/src/harness/harness.ts index 790ecebb75f..e07fe60ba86 100644 --- a/src/harness/harness.ts +++ b/src/harness/harness.ts @@ -222,7 +222,9 @@ namespace Utils { return k; } - if (k === (ts).SyntaxKind.FirstJSDocCommentNode || k === (ts).SyntaxKind.LastJSDocCommentNode) { + // For some markers in SyntaxKind, we should print its original syntax name instead of + // the marker name in tests. + if (k === (ts).SyntaxKind.FirstJSDocNode || k === (ts).SyntaxKind.LastJSDocNode) { for (const kindName in (ts).SyntaxKind) { if ((ts).SyntaxKind[kindName] === k) { return kindName; diff --git a/src/services/services.ts b/src/services/services.ts index f2906aaa8b4..ffbb174fe8c 100644 --- a/src/services/services.ts +++ b/src/services/services.ts @@ -173,6 +173,7 @@ namespace ts { "type", "typedef", "property", + "prop", "version" ]; let jsDocCompletionEntries: CompletionEntry[]; @@ -274,7 +275,7 @@ namespace ts { scanner.setText((sourceFile || this.getSourceFile()).text); children = []; let pos = this.pos; - const isJSDocTag = + const useJSDocScanner = this.kind === SyntaxKind.JSDocComment || this.kind === SyntaxKind.JSDocParameterTag || this.kind === SyntaxKind.JSDocTag || @@ -286,14 +287,14 @@ namespace ts { this.kind === SyntaxKind.JSDocPropertyTag; const processNode = (node: Node) => { if (pos < node.pos) { - pos = this.addSyntheticNodes(children, pos, node.pos, /*useJSDocScanner*/ isJSDocTag); + pos = this.addSyntheticNodes(children, pos, node.pos, useJSDocScanner); } children.push(node); pos = node.end; }; const processNodes = (nodes: NodeArray) => { if (pos < nodes.pos) { - pos = this.addSyntheticNodes(children, pos, nodes.pos, /*useJSDocScanner*/ isJSDocTag); + pos = this.addSyntheticNodes(children, pos, nodes.pos, useJSDocScanner); } children.push(this.createSyntaxList(>nodes)); pos = nodes.end; @@ -7803,7 +7804,7 @@ namespace ts { break; default: forEachChild(node, walk); - if (node.jsDocComments && node.jsDocComments.length > 0) { + if (node.jsDocComments) { for (const jsDocComment of node.jsDocComments) { forEachChild(jsDocComment, walk); } diff --git a/tests/cases/fourslash/commentsVariables.ts b/tests/cases/fourslash/commentsVariables.ts index b2cf6c9cb9b..d62e1e9bee8 100644 --- a/tests/cases/fourslash/commentsVariables.ts +++ b/tests/cases/fourslash/commentsVariables.ts @@ -41,60 +41,60 @@ ////} ////var x = fo/*15*/o2; -// goTo.marker('1'); -// verify.quickInfoIs("var myVariable: number", "This is my variable"); +goTo.marker('1'); +verify.quickInfoIs("var myVariable: number", "This is my variable"); -// goTo.marker('2'); -// verify.completionListContains("myVariable", "var myVariable: number", "This is my variable"); +goTo.marker('2'); +verify.completionListContains("myVariable", "var myVariable: number", "This is my variable"); -// goTo.marker('3'); -// verify.completionListContains("myVariable", "var myVariable: number", "This is my variable"); -// verify.completionListContains("d", "var d: number", "d variable"); +goTo.marker('3'); +verify.completionListContains("myVariable", "var myVariable: number", "This is my variable"); +verify.completionListContains("d", "var d: number", "d variable"); goTo.marker('4'); verify.completionListContains("foo", "function foo(): void", "foos comment"); verify.completionListContains("fooVar", "var fooVar: () => void", "fooVar comment"); -// goTo.marker('5'); -// verify.currentSignatureHelpDocCommentIs("foos comment"); -// goTo.marker('5q'); -// verify.quickInfoIs("function foo(): void", "foos comment"); +goTo.marker('5'); +verify.currentSignatureHelpDocCommentIs("foos comment"); +goTo.marker('5q'); +verify.quickInfoIs("function foo(): void", "foos comment"); -// goTo.marker('6'); -// verify.currentSignatureHelpDocCommentIs(""); -// goTo.marker('6q'); -// verify.quickInfoIs("var fooVar: () => void", ""); +goTo.marker('6'); +verify.currentSignatureHelpDocCommentIs(""); +goTo.marker('6q'); +verify.quickInfoIs("var fooVar: () => void", ""); -// goTo.marker('7'); -// verify.completionListContains("foo", "function foo(): void", "foos comment"); -// verify.completionListContains("fooVar", "var fooVar: () => void", "fooVar comment"); +goTo.marker('7'); +verify.completionListContains("foo", "function foo(): void", "foos comment"); +verify.completionListContains("fooVar", "var fooVar: () => void", "fooVar comment"); -// goTo.marker('8'); -// verify.currentSignatureHelpDocCommentIs("foos comment"); -// goTo.marker('8q'); -// verify.quickInfoIs("function foo(): void", "foos comment"); +goTo.marker('8'); +verify.currentSignatureHelpDocCommentIs("foos comment"); +goTo.marker('8q'); +verify.quickInfoIs("function foo(): void", "foos comment"); -// goTo.marker('9'); -// verify.currentSignatureHelpDocCommentIs(""); -// goTo.marker('9q'); -// verify.quickInfoIs("var fooVar: () => void", ""); -// goTo.marker('9aq'); -// verify.quickInfoIs("var fooVar: () => void", "fooVar comment"); +goTo.marker('9'); +verify.currentSignatureHelpDocCommentIs(""); +goTo.marker('9q'); +verify.quickInfoIs("var fooVar: () => void", ""); +goTo.marker('9aq'); +verify.quickInfoIs("var fooVar: () => void", "fooVar comment"); -// goTo.marker('10'); -// verify.completionListContains("i", "var i: c", "instance comment"); +goTo.marker('10'); +verify.completionListContains("i", "var i: c", "instance comment"); -// goTo.marker('11'); -// verify.completionListContains("i1_i", "var i1_i: i1", "interface instance comments"); +goTo.marker('11'); +verify.completionListContains("i1_i", "var i1_i: i1", "interface instance comments"); -// goTo.marker('12'); -// verify.quickInfoIs("var fooVar: () => void", "fooVar comment"); +goTo.marker('12'); +verify.quickInfoIs("var fooVar: () => void", "fooVar comment"); -// goTo.marker('13'); -// verify.quickInfoIs("var fooVar: () => void", "fooVar comment"); +goTo.marker('13'); +verify.quickInfoIs("var fooVar: () => void", "fooVar comment"); -// goTo.marker('14'); -// verify.quickInfoIs("function foo(): void", "foos comment"); +goTo.marker('14'); +verify.quickInfoIs("function foo(): void", "foos comment"); -// goTo.marker('15'); -// verify.quickInfoIs("function foo2(a: number): void (+1 overload)", ""); +goTo.marker('15'); +verify.quickInfoIs("function foo2(a: number): void (+1 overload)", ""); diff --git a/tests/cases/fourslash/doesnotwork.ts b/tests/cases/fourslash/doesnotwork.ts deleted file mode 100644 index 2f415563c21..00000000000 --- a/tests/cases/fourslash/doesnotwork.ts +++ /dev/null @@ -1,45 +0,0 @@ -/// - -/////** This is my variable*/ -////var myVariable = 10; -//// -/////** d variable*/ -////var d = 10; -////myVariable = d; -//// -/////** foos comment*/ -////function foo() { -////} -/////** fooVar comment*/ -////var fooVar: () => void; -/////*4*/ -////foo(); -////fooVar(); -////fooVar = foo; -//// -////foo(); -////fooVar(); -////var fooVarVar = fooVar; -/////**class comment*/ -////class c { -//// /** constructor comment*/ -//// constructor() { -//// } -////} -/////**instance comment*/ -////var i = new c(); -//// -/////** interface comments*/ -////interface i1 { -////} -/////**interface instance comments*/ -////var i1_i: i1; -//// -////function foo2(a: number): void; -////function foo2(b: string): void; -////function foo2(aOrb) { -////} -////var x = foo2; - -goTo.marker('4'); -verify.completionListContains("fooVar", "var fooVar: () => void", "fooVar comment"); diff --git a/tests/webTestServer.js b/tests/webTestServer.js deleted file mode 100644 index b47da7bfe76..00000000000 --- a/tests/webTestServer.js +++ /dev/null @@ -1,288 +0,0 @@ -/// -"use strict"; -var http = require("http"); -var fs = require("fs"); -var path = require("path"); -var url = require("url"); -var child_process = require("child_process"); -var os = require("os"); -/// Command line processing /// -if (process.argv[2] == '--help') { - console.log('Runs a node server on port 8888 by default, looking for tests folder in the current directory\n'); - console.log('Syntax: node nodeServer.js [port] [typescriptEnlistmentDirectory] [tests] [--browser] [--verbose]\n'); - console.log('Examples: \n\tnode nodeServer.js 8888 .'); - console.log('\tnode nodeServer.js 3000 D:/src/typescript/public --verbose IE'); -} -function switchToForwardSlashes(path) { - return path.replace(/\\/g, "/").replace(/\/\//g, '/'); -} -var defaultPort = 8888; -var port = process.argv[2] || defaultPort; -var rootDir = switchToForwardSlashes(__dirname + '/../'); -var browser; -if (process.argv[3]) { - browser = process.argv[3]; - if (browser !== 'chrome' && browser !== 'IE') { - console.log('Invalid command line arguments. Got ' + browser + ' but expected chrome, IE or nothing.'); - } -} -var grep = process.argv[4]; -var verbose = false; -if (process.argv[5] == '--verbose') { - verbose = true; -} -else if (process.argv[5] && process.argv[5] !== '--verbose') { - console.log('Invalid command line arguments. Got ' + process.argv[5] + ' but expected --verbose or nothing.'); -} -/// Utils /// -function log(msg) { - if (verbose) { - console.log(msg); - } -} -// Copied from the compiler sources -function dir(path, spec, options) { - options = options || {}; - function filesInFolder(folder) { - var folder = switchToForwardSlashes(folder); - var paths = []; - // Everything after the current directory is relative - var baseDirectoryLength = process.cwd().length + 1; - try { - var files = fs.readdirSync(folder); - for (var i = 0; i < files.length; i++) { - var stat = fs.statSync(folder + "/" + files[i]); - if (options.recursive && stat.isDirectory()) { - paths = paths.concat(filesInFolder(folder + "/" + files[i])); - } - else if (stat.isFile() && (!spec || files[i].match(spec))) { - var relativePath = folder.substring(baseDirectoryLength); - paths.push(relativePath + "/" + files[i]); - } - } - } - catch (err) { - } - return paths; - } - return filesInFolder(path); -} -// fs.rmdirSync won't delete directories with files in it -function deleteFolderRecursive(path) { - if (fs.existsSync(path)) { - fs.readdirSync(path).forEach(function (file, index) { - var curPath = path + "/" + file; - if (fs.statSync(curPath).isDirectory()) { - deleteFolderRecursive(curPath); - } - else { - fs.unlinkSync(curPath); - } - }); - fs.rmdirSync(path); - } -} -; -function writeFile(path, data, opts) { - try { - fs.writeFileSync(path, data); - } - catch (e) { - // assume file was written to a directory that exists, if not, start recursively creating them as necessary - var parts = switchToForwardSlashes(path).split('/'); - for (var i = 0; i < parts.length; i++) { - var subDir = parts.slice(0, i).join('/'); - if (!fs.existsSync(subDir)) { - fs.mkdir(subDir); - } - } - fs.writeFileSync(path, data); - } -} -/// Request Handling /// -function handleResolutionRequest(filePath, res) { - var resolvedPath = path.resolve(filePath, ''); - resolvedPath = resolvedPath.substring(resolvedPath.indexOf('tests')); - resolvedPath = switchToForwardSlashes(resolvedPath); - send('success', res, resolvedPath); - return; -} -function send(result, res, contents, contentType) { - if (contentType === void 0) { contentType = "binary"; } - var responseCode = result === "success" ? 200 : result === "fail" ? 500 : result === 'unknown' ? 404 : parseInt(result); - res.writeHead(responseCode, { "Content-Type": contentType }); - res.end(contents); - return; -} -// Reads the data from a post request and passes it to the given callback -function processPost(req, res, callback) { - var queryData = ""; - if (typeof callback !== 'function') - return null; - if (req.method == 'POST') { - req.on('data', function (data) { - queryData += data; - if (queryData.length > 1e8) { - queryData = ""; - send("413", res, null); - console.log("ERROR: destroying connection"); - req.connection.destroy(); - } - }); - req.on('end', function () { - //res.post = url.parse(req.url).query; - callback(queryData); - }); - } - else { - send("405", res, null); - } -} -var RequestType; -(function (RequestType) { - RequestType[RequestType["GetFile"] = 0] = "GetFile"; - RequestType[RequestType["GetDir"] = 1] = "GetDir"; - RequestType[RequestType["ResolveFile"] = 2] = "ResolveFile"; - RequestType[RequestType["WriteFile"] = 3] = "WriteFile"; - RequestType[RequestType["DeleteFile"] = 4] = "DeleteFile"; - RequestType[RequestType["WriteDir"] = 5] = "WriteDir"; - RequestType[RequestType["DeleteDir"] = 6] = "DeleteDir"; - RequestType[RequestType["AppendFile"] = 7] = "AppendFile"; - RequestType[RequestType["Unknown"] = 8] = "Unknown"; -})(RequestType || (RequestType = {})); -function getRequestOperation(req, filename) { - if (req.method === 'GET' && req.url.indexOf('?') === -1) { - if (req.url.indexOf('.') !== -1) - return RequestType.GetFile; - else - return RequestType.GetDir; - } - else { - var queryData = url.parse(req.url, true).query; - if (req.method === 'GET' && queryData.resolve !== undefined) - return RequestType.ResolveFile; - // mocha uses ?grep= query string as equivalent to the --grep command line option used to filter tests - if (req.method === 'GET' && queryData.grep !== undefined) - return RequestType.GetFile; - if (req.method === 'POST' && queryData.action) { - var path = req.url.substr(0, req.url.lastIndexOf('?')); - var isFile = path.substring(path.lastIndexOf('/')).indexOf('.') !== -1; - switch (queryData.action.toUpperCase()) { - case 'WRITE': - return isFile ? RequestType.WriteFile : RequestType.WriteDir; - case 'DELETE': - return isFile ? RequestType.DeleteFile : RequestType.DeleteDir; - case 'APPEND': - return isFile ? RequestType.AppendFile : RequestType.Unknown; - } - } - return RequestType.Unknown; - } -} -function handleRequestOperation(req, res, operation, reqPath) { - switch (operation) { - case RequestType.GetDir: - var filesInFolder = dir(reqPath, "", { recursive: true }); - send('success', res, filesInFolder.join(',')); - break; - case RequestType.GetFile: - fs.readFile(reqPath, function (err, file) { - var ext = reqPath.substr(reqPath.lastIndexOf('.')); - var contentType = 'binary'; - if (ext === '.js') - contentType = 'text/javascript'; - else if (ext === '.css') - contentType = 'text/javascript'; - else if (ext === '.html') - contentType = 'text/html'; - err - ? send('fail', res, err.message, contentType) - : send('success', res, file, contentType); - }); - break; - case RequestType.ResolveFile: - var resolveRequest = req.url.match(/(.*)\?resolve/); - handleResolutionRequest(resolveRequest[1], res); - break; - case RequestType.WriteFile: - processPost(req, res, function (data) { - writeFile(reqPath, data, { recursive: true }); - }); - send('success', res, null); - break; - case RequestType.WriteDir: - fs.mkdirSync(reqPath); - send('success', res, null); - break; - case RequestType.DeleteFile: - if (fs.existsSync(reqPath)) { - fs.unlinkSync(reqPath); - } - send('success', res, null); - break; - case RequestType.DeleteDir: - if (fs.existsSync(reqPath)) { - fs.rmdirSync(reqPath); - } - send('success', res, null); - break; - case RequestType.AppendFile: - processPost(req, res, function (data) { - fs.appendFileSync(reqPath, data); - }); - send('success', res, null); - break; - case RequestType.Unknown: - default: - send('unknown', res, null); - break; - } -} -console.log("Static file server running at\n => http://localhost:" + port + "/\nCTRL + C to shutdown"); -http.createServer(function (req, res) { - log(req.method + ' ' + req.url); - var uri = url.parse(req.url).pathname; - var reqPath = path.join(process.cwd(), uri); - var operation = getRequestOperation(req, reqPath); - handleRequestOperation(req, res, operation, reqPath); -}).listen(8888); -var browserPath; -if ((browser && browser === 'chrome')) { - var defaultChromePath = ""; - switch (os.platform()) { - case "win32": - case "win64": - defaultChromePath = "C:/Program Files (x86)/Google/Chrome/Application/chrome.exe"; - break; - case "darwin": - defaultChromePath = "/Applications/Google Chrome.app/Contents/MacOS/Google Chrome"; - break; - case "linux": - defaultChromePath = "/opt/google/chrome/chrome"; - break; - default: - console.log("default Chrome location is unknown for platform '" + os.platform() + "'"); - break; - } - if (fs.existsSync(defaultChromePath)) { - browserPath = defaultChromePath; - } - else { - browserPath = browser; - } -} -else { - var defaultIEPath = 'C:/Program Files/Internet Explorer/iexplore.exe'; - if (fs.existsSync(defaultIEPath)) { - browserPath = defaultIEPath; - } - else { - browserPath = browser; - } -} -console.log('Using browser: ' + browserPath); -var queryString = grep ? "?grep=" + grep : ''; -child_process.spawn(browserPath, ['http://localhost:' + port + '/tests/webTestResults.html' + queryString], { - stdio: 'inherit' -}); -//# sourceMappingURL=file:///C:/Users/lizhe/Documents/github/TypeScript/tests/webTestServer.js.map \ No newline at end of file diff --git a/tests/webTestServer.js.map b/tests/webTestServer.js.map deleted file mode 100644 index 1c924a872f4..00000000000 --- a/tests/webTestServer.js.map +++ /dev/null @@ -1 +0,0 @@ -{"version":3,"file":"webTestServer.js","sourceRoot":"","sources":["file:///C:/Users/lizhe/Documents/github/TypeScript/tests/webTestServer.ts"],"names":[],"mappings":"AAAA,yDAAyD;;AAEzD,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OAAO,CAAC;IAC1B,CAAC;AACL,CAAC;AAED,OAAO,CAAC,GAAG,CAAC,iBAAiB,GAAG,WAAW,CAAC,CAAC;AAE7C,IAAI,WAAW,GAAG,IAAI,GAAG,QAAQ,GAAG,IAAI,GAAG,EAAE,CAAC;AAC9C,aAAa,CAAC,KAAK,CAAC,WAAW,EAAE,CAAC,mBAAmB,GAAG,IAAI,GAAG,4BAA4B,GAAG,WAAW,CAAC,EAAE;IACxG,KAAK,EAAE,SAAS;CACnB,CAAC,CAAC"} \ No newline at end of file From 15cbb0216202eec3e9fa5f04c124607d4bf8c020 Mon Sep 17 00:00:00 2001 From: zhengbli Date: Fri, 22 Apr 2016 14:26:30 -0700 Subject: [PATCH 06/41] refactor --- src/compiler/types.ts | 2 +- src/services/utilities.ts | 7 +++---- 2 files changed, 4 insertions(+), 5 deletions(-) diff --git a/src/compiler/types.ts b/src/compiler/types.ts index 91ce1f1022f..f40237ba47e 100644 --- a/src/compiler/types.ts +++ b/src/compiler/types.ts @@ -450,7 +450,7 @@ namespace ts { modifiers?: ModifiersArray; // Array of modifiers /* @internal */ id?: number; // Unique id (used to look up NodeLinks) parent?: Node; // Parent node (initialized by binding - /* @internal */ jsDocComments?: JSDocComment[]; // JSDoc for the node, if it has any. Only for .js files. + /* @internal */ jsDocComments?: JSDocComment[]; // JSDoc for the node, if it has any. Only for .js files. /* @internal */ symbol?: Symbol; // Symbol declared by node (initialized by binding) /* @internal */ locals?: SymbolTable; // Locals associated with node (initialized by binding) /* @internal */ nextContainer?: Node; // Next container in declaration order (initialized by binding) diff --git a/src/services/utilities.ts b/src/services/utilities.ts index 73ac43911b5..0230195b0cc 100644 --- a/src/services/utilities.ts +++ b/src/services/utilities.ts @@ -267,7 +267,7 @@ namespace ts { if (includeJsDocComment) { const jsDocChildren = ts.filter(current.getChildren(), isJSDocNode); for (const jsDocChild of jsDocChildren) { - let start = allowPositionInLeadingTrivia ? jsDocChild.getFullStart() : jsDocChild.getStart(sourceFile, includeJsDocComment); + const start = allowPositionInLeadingTrivia ? jsDocChild.getFullStart() : jsDocChild.getStart(sourceFile, includeJsDocComment); if (start <= position) { let end = jsDocChild.getEnd(); if (position < end || (position === end && jsDocChild.kind === SyntaxKind.EndOfFileToken)) { @@ -500,9 +500,8 @@ namespace ts { } if (node) { - let jsDocComments = node.jsDocComments; - if (jsDocComments) { - for (const jsDocComment of jsDocComments) { + if (node.jsDocComments) { + for (const jsDocComment of node.jsDocComments) { for (const tag of jsDocComment.tags) { if (tag.pos <= position && position <= tag.end) { return tag; From d568d79b495d256e93907b28c3aeec912b41594f Mon Sep 17 00:00:00 2001 From: zhengbli Date: Fri, 22 Apr 2016 15:19:57 -0700 Subject: [PATCH 07/41] resolve build error --- src/compiler/parser.ts | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/compiler/parser.ts b/src/compiler/parser.ts index b9662126e65..612602f5658 100644 --- a/src/compiler/parser.ts +++ b/src/compiler/parser.ts @@ -6322,7 +6322,7 @@ namespace ts { const typeExpression = tryParseTypeExpression(); skipWhitespace(); - const name = parseJSDocIdentifier(); + const name = parseJSDocIdentifierName(); if (!name) { parseErrorAtPosition(scanner.getStartPos(), /*length*/ 0, Diagnostics.Identifier_expected); return undefined; @@ -6344,7 +6344,7 @@ namespace ts { function handleTypedefTag(atToken: Node, tagName: Identifier): JSDocTypedefTag { const typeExpression = tryParseTypeExpression(); skipWhitespace(); - let name = parseJSDocIdentifier(); + let name = parseJSDocIdentifierName(); if (!name) { let foundNameFromParentNode = false; if (parentNode && parentNode.kind === SyntaxKind.VariableStatement) { From 79bb4ba5b42d58c2f8ff8955f26f58a9ef24cddb Mon Sep 17 00:00:00 2001 From: Zhengbo Li Date: Mon, 25 Apr 2016 11:56:16 -0700 Subject: [PATCH 08/41] Fix broken test --- src/services/utilities.ts | 6 +++++- 1 file changed, 5 insertions(+), 1 deletion(-) diff --git a/src/services/utilities.ts b/src/services/utilities.ts index bf90a023c14..12baca1af54 100644 --- a/src/services/utilities.ts +++ b/src/services/utilities.ts @@ -445,6 +445,10 @@ namespace ts { return false; } + if (token.kind === SyntaxKind.JsxText) { + return true; + } + //
Hello |
if (token.kind === SyntaxKind.LessThanToken && token.parent.kind === SyntaxKind.JsxText) { return true; @@ -455,7 +459,7 @@ namespace ts { return true; } - //
{ + //
{ // | // } < /div> if (token && token.kind === SyntaxKind.CloseBraceToken && token.parent.kind === SyntaxKind.JsxExpression) { From 20d8a94d66ce467053bce183b0a1e0538a1ea96d Mon Sep 17 00:00:00 2001 From: Wesley Wigham Date: Thu, 25 Feb 2016 03:32:15 -0500 Subject: [PATCH 09/41] Remove check narrowing only certain types, add test showing issues with this --- .../typeGuardNarrowsPrimitiveIntersection.js | 38 ++++++++++ ...eGuardNarrowsPrimitiveIntersection.symbols | 70 +++++++++++++++++ ...ypeGuardNarrowsPrimitiveIntersection.types | 76 +++++++++++++++++++ .../typeGuardNarrowsPrimitiveIntersection.ts | 21 +++++ 4 files changed, 205 insertions(+) create mode 100644 tests/baselines/reference/typeGuardNarrowsPrimitiveIntersection.js create mode 100644 tests/baselines/reference/typeGuardNarrowsPrimitiveIntersection.symbols create mode 100644 tests/baselines/reference/typeGuardNarrowsPrimitiveIntersection.types create mode 100644 tests/cases/conformance/expressions/typeGuards/typeGuardNarrowsPrimitiveIntersection.ts diff --git a/tests/baselines/reference/typeGuardNarrowsPrimitiveIntersection.js b/tests/baselines/reference/typeGuardNarrowsPrimitiveIntersection.js new file mode 100644 index 00000000000..a4cba6f4ab5 --- /dev/null +++ b/tests/baselines/reference/typeGuardNarrowsPrimitiveIntersection.js @@ -0,0 +1,38 @@ +//// [typeGuardNarrowsPrimitiveIntersection.ts] +type Tag = {__tag: any}; +declare function isNonBlank(value: string) : value is (string & Tag); +declare function doThis(value: string & Tag): void; +declare function doThat(value: string) : void; +let value: string; +if (isNonBlank(value)) { + doThis(value); +} else { + doThat(value); +} + + +const enum Tag2 {} +declare function isNonBlank2(value: string) : value is (string & Tag2); +declare function doThis2(value: string & Tag2): void; +declare function doThat2(value: string) : void; +if (isNonBlank2(value)) { + doThis2(value); +} else { + doThat2(value); +} + + +//// [typeGuardNarrowsPrimitiveIntersection.js] +var value; +if (isNonBlank(value)) { + doThis(value); +} +else { + doThat(value); +} +if (isNonBlank2(value)) { + doThis2(value); +} +else { + doThat2(value); +} diff --git a/tests/baselines/reference/typeGuardNarrowsPrimitiveIntersection.symbols b/tests/baselines/reference/typeGuardNarrowsPrimitiveIntersection.symbols new file mode 100644 index 00000000000..da507fb94c5 --- /dev/null +++ b/tests/baselines/reference/typeGuardNarrowsPrimitiveIntersection.symbols @@ -0,0 +1,70 @@ +=== tests/cases/conformance/expressions/typeGuards/typeGuardNarrowsPrimitiveIntersection.ts === +type Tag = {__tag: any}; +>Tag : Symbol(Tag, Decl(typeGuardNarrowsPrimitiveIntersection.ts, 0, 0)) +>__tag : Symbol(__tag, Decl(typeGuardNarrowsPrimitiveIntersection.ts, 0, 12)) + +declare function isNonBlank(value: string) : value is (string & Tag); +>isNonBlank : Symbol(isNonBlank, Decl(typeGuardNarrowsPrimitiveIntersection.ts, 0, 24)) +>value : Symbol(value, Decl(typeGuardNarrowsPrimitiveIntersection.ts, 1, 28)) +>value : Symbol(value, Decl(typeGuardNarrowsPrimitiveIntersection.ts, 1, 28)) +>Tag : Symbol(Tag, Decl(typeGuardNarrowsPrimitiveIntersection.ts, 0, 0)) + +declare function doThis(value: string & Tag): void; +>doThis : Symbol(doThis, Decl(typeGuardNarrowsPrimitiveIntersection.ts, 1, 69)) +>value : Symbol(value, Decl(typeGuardNarrowsPrimitiveIntersection.ts, 2, 24)) +>Tag : Symbol(Tag, Decl(typeGuardNarrowsPrimitiveIntersection.ts, 0, 0)) + +declare function doThat(value: string) : void; +>doThat : Symbol(doThat, Decl(typeGuardNarrowsPrimitiveIntersection.ts, 2, 51)) +>value : Symbol(value, Decl(typeGuardNarrowsPrimitiveIntersection.ts, 3, 24)) + +let value: string; +>value : Symbol(value, Decl(typeGuardNarrowsPrimitiveIntersection.ts, 4, 3)) + +if (isNonBlank(value)) { +>isNonBlank : Symbol(isNonBlank, Decl(typeGuardNarrowsPrimitiveIntersection.ts, 0, 24)) +>value : Symbol(value, Decl(typeGuardNarrowsPrimitiveIntersection.ts, 4, 3)) + + doThis(value); +>doThis : Symbol(doThis, Decl(typeGuardNarrowsPrimitiveIntersection.ts, 1, 69)) +>value : Symbol(value, Decl(typeGuardNarrowsPrimitiveIntersection.ts, 4, 3)) + +} else { + doThat(value); +>doThat : Symbol(doThat, Decl(typeGuardNarrowsPrimitiveIntersection.ts, 2, 51)) +>value : Symbol(value, Decl(typeGuardNarrowsPrimitiveIntersection.ts, 4, 3)) +} + + +const enum Tag2 {} +>Tag2 : Symbol(Tag2, Decl(typeGuardNarrowsPrimitiveIntersection.ts, 9, 1)) + +declare function isNonBlank2(value: string) : value is (string & Tag2); +>isNonBlank2 : Symbol(isNonBlank2, Decl(typeGuardNarrowsPrimitiveIntersection.ts, 12, 18)) +>value : Symbol(value, Decl(typeGuardNarrowsPrimitiveIntersection.ts, 13, 29)) +>value : Symbol(value, Decl(typeGuardNarrowsPrimitiveIntersection.ts, 13, 29)) +>Tag2 : Symbol(Tag2, Decl(typeGuardNarrowsPrimitiveIntersection.ts, 9, 1)) + +declare function doThis2(value: string & Tag2): void; +>doThis2 : Symbol(doThis2, Decl(typeGuardNarrowsPrimitiveIntersection.ts, 13, 71)) +>value : Symbol(value, Decl(typeGuardNarrowsPrimitiveIntersection.ts, 14, 25)) +>Tag2 : Symbol(Tag2, Decl(typeGuardNarrowsPrimitiveIntersection.ts, 9, 1)) + +declare function doThat2(value: string) : void; +>doThat2 : Symbol(doThat2, Decl(typeGuardNarrowsPrimitiveIntersection.ts, 14, 53)) +>value : Symbol(value, Decl(typeGuardNarrowsPrimitiveIntersection.ts, 15, 25)) + +if (isNonBlank2(value)) { +>isNonBlank2 : Symbol(isNonBlank2, Decl(typeGuardNarrowsPrimitiveIntersection.ts, 12, 18)) +>value : Symbol(value, Decl(typeGuardNarrowsPrimitiveIntersection.ts, 4, 3)) + + doThis2(value); +>doThis2 : Symbol(doThis2, Decl(typeGuardNarrowsPrimitiveIntersection.ts, 13, 71)) +>value : Symbol(value, Decl(typeGuardNarrowsPrimitiveIntersection.ts, 4, 3)) + +} else { + doThat2(value); +>doThat2 : Symbol(doThat2, Decl(typeGuardNarrowsPrimitiveIntersection.ts, 14, 53)) +>value : Symbol(value, Decl(typeGuardNarrowsPrimitiveIntersection.ts, 4, 3)) +} + diff --git a/tests/baselines/reference/typeGuardNarrowsPrimitiveIntersection.types b/tests/baselines/reference/typeGuardNarrowsPrimitiveIntersection.types new file mode 100644 index 00000000000..478363669b4 --- /dev/null +++ b/tests/baselines/reference/typeGuardNarrowsPrimitiveIntersection.types @@ -0,0 +1,76 @@ +=== tests/cases/conformance/expressions/typeGuards/typeGuardNarrowsPrimitiveIntersection.ts === +type Tag = {__tag: any}; +>Tag : { __tag: any; } +>__tag : any + +declare function isNonBlank(value: string) : value is (string & Tag); +>isNonBlank : (value: string) => value is string & { __tag: any; } +>value : string +>value : any +>Tag : { __tag: any; } + +declare function doThis(value: string & Tag): void; +>doThis : (value: string & { __tag: any; }) => void +>value : string & { __tag: any; } +>Tag : { __tag: any; } + +declare function doThat(value: string) : void; +>doThat : (value: string) => void +>value : string + +let value: string; +>value : string + +if (isNonBlank(value)) { +>isNonBlank(value) : boolean +>isNonBlank : (value: string) => value is string & { __tag: any; } +>value : string + + doThis(value); +>doThis(value) : void +>doThis : (value: string & { __tag: any; }) => void +>value : string & { __tag: any; } + +} else { + doThat(value); +>doThat(value) : void +>doThat : (value: string) => void +>value : string +} + + +const enum Tag2 {} +>Tag2 : Tag2 + +declare function isNonBlank2(value: string) : value is (string & Tag2); +>isNonBlank2 : (value: string) => value is string & Tag2 +>value : string +>value : any +>Tag2 : Tag2 + +declare function doThis2(value: string & Tag2): void; +>doThis2 : (value: string & Tag2) => void +>value : string & Tag2 +>Tag2 : Tag2 + +declare function doThat2(value: string) : void; +>doThat2 : (value: string) => void +>value : string + +if (isNonBlank2(value)) { +>isNonBlank2(value) : boolean +>isNonBlank2 : (value: string) => value is string & Tag2 +>value : string + + doThis2(value); +>doThis2(value) : void +>doThis2 : (value: string & Tag2) => void +>value : string & Tag2 + +} else { + doThat2(value); +>doThat2(value) : void +>doThat2 : (value: string) => void +>value : string +} + diff --git a/tests/cases/conformance/expressions/typeGuards/typeGuardNarrowsPrimitiveIntersection.ts b/tests/cases/conformance/expressions/typeGuards/typeGuardNarrowsPrimitiveIntersection.ts new file mode 100644 index 00000000000..376a78275c8 --- /dev/null +++ b/tests/cases/conformance/expressions/typeGuards/typeGuardNarrowsPrimitiveIntersection.ts @@ -0,0 +1,21 @@ +type Tag = {__tag: any}; +declare function isNonBlank(value: string) : value is (string & Tag); +declare function doThis(value: string & Tag): void; +declare function doThat(value: string) : void; +let value: string; +if (isNonBlank(value)) { + doThis(value); +} else { + doThat(value); +} + + +const enum Tag2 {} +declare function isNonBlank2(value: string) : value is (string & Tag2); +declare function doThis2(value: string & Tag2): void; +declare function doThat2(value: string) : void; +if (isNonBlank2(value)) { + doThis2(value); +} else { + doThat2(value); +} From a2feb0ee7dc07aad3ef9d05e2ea151c547fd460d Mon Sep 17 00:00:00 2001 From: Wesley Wigham Date: Fri, 11 Mar 2016 02:24:26 -0500 Subject: [PATCH 10/41] string literal case test --- .../typeGuardNarrowsToLiteralType.js | 21 +++++++++++ .../typeGuardNarrowsToLiteralType.symbols | 32 +++++++++++++++++ .../typeGuardNarrowsToLiteralType.types | 35 +++++++++++++++++++ .../typeGuardNarrowsToLiteralType.ts | 10 ++++++ 4 files changed, 98 insertions(+) create mode 100644 tests/baselines/reference/typeGuardNarrowsToLiteralType.js create mode 100644 tests/baselines/reference/typeGuardNarrowsToLiteralType.symbols create mode 100644 tests/baselines/reference/typeGuardNarrowsToLiteralType.types create mode 100644 tests/cases/conformance/expressions/typeGuards/typeGuardNarrowsToLiteralType.ts diff --git a/tests/baselines/reference/typeGuardNarrowsToLiteralType.js b/tests/baselines/reference/typeGuardNarrowsToLiteralType.js new file mode 100644 index 00000000000..fdd2ee3fb7f --- /dev/null +++ b/tests/baselines/reference/typeGuardNarrowsToLiteralType.js @@ -0,0 +1,21 @@ +//// [typeGuardNarrowsToLiteralType.ts] +declare function isFoo(value: string) : value is "foo"; +declare function doThis(value: "foo"): void; +declare function doThat(value: string) : void; +let value: string; +if (isFoo(value)) { + doThis(value); +} else { + doThat(value); +} + + + +//// [typeGuardNarrowsToLiteralType.js] +var value; +if (isFoo(value)) { + doThis(value); +} +else { + doThat(value); +} diff --git a/tests/baselines/reference/typeGuardNarrowsToLiteralType.symbols b/tests/baselines/reference/typeGuardNarrowsToLiteralType.symbols new file mode 100644 index 00000000000..0d0ddc5f00d --- /dev/null +++ b/tests/baselines/reference/typeGuardNarrowsToLiteralType.symbols @@ -0,0 +1,32 @@ +=== tests/cases/conformance/expressions/typeGuards/typeGuardNarrowsToLiteralType.ts === +declare function isFoo(value: string) : value is "foo"; +>isFoo : Symbol(isFoo, Decl(typeGuardNarrowsToLiteralType.ts, 0, 0)) +>value : Symbol(value, Decl(typeGuardNarrowsToLiteralType.ts, 0, 23)) +>value : Symbol(value, Decl(typeGuardNarrowsToLiteralType.ts, 0, 23)) + +declare function doThis(value: "foo"): void; +>doThis : Symbol(doThis, Decl(typeGuardNarrowsToLiteralType.ts, 0, 55)) +>value : Symbol(value, Decl(typeGuardNarrowsToLiteralType.ts, 1, 24)) + +declare function doThat(value: string) : void; +>doThat : Symbol(doThat, Decl(typeGuardNarrowsToLiteralType.ts, 1, 44)) +>value : Symbol(value, Decl(typeGuardNarrowsToLiteralType.ts, 2, 24)) + +let value: string; +>value : Symbol(value, Decl(typeGuardNarrowsToLiteralType.ts, 3, 3)) + +if (isFoo(value)) { +>isFoo : Symbol(isFoo, Decl(typeGuardNarrowsToLiteralType.ts, 0, 0)) +>value : Symbol(value, Decl(typeGuardNarrowsToLiteralType.ts, 3, 3)) + + doThis(value); +>doThis : Symbol(doThis, Decl(typeGuardNarrowsToLiteralType.ts, 0, 55)) +>value : Symbol(value, Decl(typeGuardNarrowsToLiteralType.ts, 3, 3)) + +} else { + doThat(value); +>doThat : Symbol(doThat, Decl(typeGuardNarrowsToLiteralType.ts, 1, 44)) +>value : Symbol(value, Decl(typeGuardNarrowsToLiteralType.ts, 3, 3)) +} + + diff --git a/tests/baselines/reference/typeGuardNarrowsToLiteralType.types b/tests/baselines/reference/typeGuardNarrowsToLiteralType.types new file mode 100644 index 00000000000..9835206deb9 --- /dev/null +++ b/tests/baselines/reference/typeGuardNarrowsToLiteralType.types @@ -0,0 +1,35 @@ +=== tests/cases/conformance/expressions/typeGuards/typeGuardNarrowsToLiteralType.ts === +declare function isFoo(value: string) : value is "foo"; +>isFoo : (value: string) => value is "foo" +>value : string +>value : any + +declare function doThis(value: "foo"): void; +>doThis : (value: "foo") => void +>value : "foo" + +declare function doThat(value: string) : void; +>doThat : (value: string) => void +>value : string + +let value: string; +>value : string + +if (isFoo(value)) { +>isFoo(value) : boolean +>isFoo : (value: string) => value is "foo" +>value : string + + doThis(value); +>doThis(value) : void +>doThis : (value: "foo") => void +>value : "foo" + +} else { + doThat(value); +>doThat(value) : void +>doThat : (value: string) => void +>value : string +} + + diff --git a/tests/cases/conformance/expressions/typeGuards/typeGuardNarrowsToLiteralType.ts b/tests/cases/conformance/expressions/typeGuards/typeGuardNarrowsToLiteralType.ts new file mode 100644 index 00000000000..3b7d5bba21a --- /dev/null +++ b/tests/cases/conformance/expressions/typeGuards/typeGuardNarrowsToLiteralType.ts @@ -0,0 +1,10 @@ +declare function isFoo(value: string) : value is "foo"; +declare function doThis(value: "foo"): void; +declare function doThat(value: string) : void; +let value: string; +if (isFoo(value)) { + doThis(value); +} else { + doThat(value); +} + From 92bf91dd581b98e9c9fd0c8d69dae6132af762ef Mon Sep 17 00:00:00 2001 From: Wesley Wigham Date: Tue, 26 Apr 2016 04:14:00 -0400 Subject: [PATCH 11/41] Reconcile fix with CFA work --- src/compiler/checker.ts | 4 ++-- src/compiler/types.ts | 8 +++++++- 2 files changed, 9 insertions(+), 3 deletions(-) diff --git a/src/compiler/checker.ts b/src/compiler/checker.ts index 6a42dc43d51..ca0d61c8106 100644 --- a/src/compiler/checker.ts +++ b/src/compiler/checker.ts @@ -7555,7 +7555,7 @@ namespace ts { } function getNarrowedTypeOfReference(type: Type, reference: Node) { - if (!(type.flags & TypeFlags.Narrowable) || !isNarrowableReference(reference)) { + if (type.flags & TypeFlags.Defaultable || !isNarrowableReference(reference)) { return type; } const leftmostNode = getLeftmostIdentifierOrThis(reference); @@ -7975,7 +7975,7 @@ namespace ts { const defaultsToDeclaredType = !strictNullChecks || type.flags & TypeFlags.Any || !declaration || declaration.kind === SyntaxKind.Parameter || isInAmbientContext(declaration) || getContainingFunctionOrModule(declaration) !== getContainingFunctionOrModule(node); - if (defaultsToDeclaredType && !(type.flags & TypeFlags.Narrowable)) { + if (defaultsToDeclaredType && type.flags & TypeFlags.Defaultable) { return type; } const flowType = getFlowTypeOfReference(node, type, defaultsToDeclaredType ? type : undefinedType); diff --git a/src/compiler/types.ts b/src/compiler/types.ts index d0d411a710e..5f181bc982e 100644 --- a/src/compiler/types.ts +++ b/src/compiler/types.ts @@ -2175,7 +2175,13 @@ namespace ts { ObjectType = Class | Interface | Reference | Tuple | Anonymous, UnionOrIntersection = Union | Intersection, StructuredType = ObjectType | Union | Intersection, - Narrowable = Any | ObjectType | Union | TypeParameter, + + // 'Defaultable' types are types where narrowing reverts to the original type, rather than actually narrow. + // This is never really correct - you can _always_ narrow to an intersection with that type, _but_ we keep + // Void as the only defaultable type, since it's a non-value type construct (representing a lack of a value) + // and, generally speaking, narrowing `void` should fail in some way, as it is nonsensical. (`void` narrowing + // to `void & T`, in a structural sense, is just narrowing to T, which we wouldn't allow under normal rules) + Defaultable = Void, /* @internal */ RequiresWidening = ContainsUndefinedOrNull | ContainsObjectLiteral, /* @internal */ From 73d4aaef46eea2323587edb650678c2fe70aa170 Mon Sep 17 00:00:00 2001 From: Wesley Wigham Date: Tue, 26 Apr 2016 04:17:40 -0400 Subject: [PATCH 12/41] Defaultable -> NotNarrowable to align with use --- src/compiler/checker.ts | 4 ++-- src/compiler/types.ts | 4 ++-- 2 files changed, 4 insertions(+), 4 deletions(-) diff --git a/src/compiler/checker.ts b/src/compiler/checker.ts index ca0d61c8106..2e2f59f6c15 100644 --- a/src/compiler/checker.ts +++ b/src/compiler/checker.ts @@ -7555,7 +7555,7 @@ namespace ts { } function getNarrowedTypeOfReference(type: Type, reference: Node) { - if (type.flags & TypeFlags.Defaultable || !isNarrowableReference(reference)) { + if (type.flags & TypeFlags.NotNarrowable || !isNarrowableReference(reference)) { return type; } const leftmostNode = getLeftmostIdentifierOrThis(reference); @@ -7975,7 +7975,7 @@ namespace ts { const defaultsToDeclaredType = !strictNullChecks || type.flags & TypeFlags.Any || !declaration || declaration.kind === SyntaxKind.Parameter || isInAmbientContext(declaration) || getContainingFunctionOrModule(declaration) !== getContainingFunctionOrModule(node); - if (defaultsToDeclaredType && type.flags & TypeFlags.Defaultable) { + if (defaultsToDeclaredType && type.flags & TypeFlags.NotNarrowable) { return type; } const flowType = getFlowTypeOfReference(node, type, defaultsToDeclaredType ? type : undefinedType); diff --git a/src/compiler/types.ts b/src/compiler/types.ts index 5f181bc982e..47f9d1b752e 100644 --- a/src/compiler/types.ts +++ b/src/compiler/types.ts @@ -2176,12 +2176,12 @@ namespace ts { UnionOrIntersection = Union | Intersection, StructuredType = ObjectType | Union | Intersection, - // 'Defaultable' types are types where narrowing reverts to the original type, rather than actually narrow. + // 'NotNarrowable' types are types where narrowing reverts to the original type, rather than actually narrow. // This is never really correct - you can _always_ narrow to an intersection with that type, _but_ we keep // Void as the only defaultable type, since it's a non-value type construct (representing a lack of a value) // and, generally speaking, narrowing `void` should fail in some way, as it is nonsensical. (`void` narrowing // to `void & T`, in a structural sense, is just narrowing to T, which we wouldn't allow under normal rules) - Defaultable = Void, + NotNarrowable = Void, /* @internal */ RequiresWidening = ContainsUndefinedOrNull | ContainsObjectLiteral, /* @internal */ From 70733da2f2013ef786486d64a6dce7efae504298 Mon Sep 17 00:00:00 2001 From: Wesley Wigham Date: Tue, 26 Apr 2016 04:23:54 -0400 Subject: [PATCH 13/41] Missed a defaultable in comments --- src/compiler/types.ts | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/compiler/types.ts b/src/compiler/types.ts index 47f9d1b752e..044cf3f4e8b 100644 --- a/src/compiler/types.ts +++ b/src/compiler/types.ts @@ -2178,7 +2178,7 @@ namespace ts { // 'NotNarrowable' types are types where narrowing reverts to the original type, rather than actually narrow. // This is never really correct - you can _always_ narrow to an intersection with that type, _but_ we keep - // Void as the only defaultable type, since it's a non-value type construct (representing a lack of a value) + // Void as the only non-narrowable type, since it's a non-value type construct (representing a lack of a value) // and, generally speaking, narrowing `void` should fail in some way, as it is nonsensical. (`void` narrowing // to `void & T`, in a structural sense, is just narrowing to T, which we wouldn't allow under normal rules) NotNarrowable = Void, From a80529b9899ebe105f6e72a176f4bdf6c5eb1586 Mon Sep 17 00:00:00 2001 From: Wesley Wigham Date: Tue, 26 Apr 2016 04:37:00 -0400 Subject: [PATCH 14/41] Add test for narrowing to unions of string literals --- .../typeGuardNarrowsToLiteralTypeUnion.js | 21 +++++++++++ ...typeGuardNarrowsToLiteralTypeUnion.symbols | 32 +++++++++++++++++ .../typeGuardNarrowsToLiteralTypeUnion.types | 35 +++++++++++++++++++ .../typeGuardNarrowsToLiteralTypeUnion.ts | 10 ++++++ 4 files changed, 98 insertions(+) create mode 100644 tests/baselines/reference/typeGuardNarrowsToLiteralTypeUnion.js create mode 100644 tests/baselines/reference/typeGuardNarrowsToLiteralTypeUnion.symbols create mode 100644 tests/baselines/reference/typeGuardNarrowsToLiteralTypeUnion.types create mode 100644 tests/cases/conformance/expressions/typeGuards/typeGuardNarrowsToLiteralTypeUnion.ts diff --git a/tests/baselines/reference/typeGuardNarrowsToLiteralTypeUnion.js b/tests/baselines/reference/typeGuardNarrowsToLiteralTypeUnion.js new file mode 100644 index 00000000000..715b362c8e0 --- /dev/null +++ b/tests/baselines/reference/typeGuardNarrowsToLiteralTypeUnion.js @@ -0,0 +1,21 @@ +//// [typeGuardNarrowsToLiteralTypeUnion.ts] +declare function isFoo(value: string) : value is ("foo" | "bar"); +declare function doThis(value: "foo" | "bar"): void; +declare function doThat(value: string) : void; +let value: string; +if (isFoo(value)) { + doThis(value); +} else { + doThat(value); +} + + + +//// [typeGuardNarrowsToLiteralTypeUnion.js] +var value; +if (isFoo(value)) { + doThis(value); +} +else { + doThat(value); +} diff --git a/tests/baselines/reference/typeGuardNarrowsToLiteralTypeUnion.symbols b/tests/baselines/reference/typeGuardNarrowsToLiteralTypeUnion.symbols new file mode 100644 index 00000000000..356fa06a06c --- /dev/null +++ b/tests/baselines/reference/typeGuardNarrowsToLiteralTypeUnion.symbols @@ -0,0 +1,32 @@ +=== tests/cases/conformance/expressions/typeGuards/typeGuardNarrowsToLiteralTypeUnion.ts === +declare function isFoo(value: string) : value is ("foo" | "bar"); +>isFoo : Symbol(isFoo, Decl(typeGuardNarrowsToLiteralTypeUnion.ts, 0, 0)) +>value : Symbol(value, Decl(typeGuardNarrowsToLiteralTypeUnion.ts, 0, 23)) +>value : Symbol(value, Decl(typeGuardNarrowsToLiteralTypeUnion.ts, 0, 23)) + +declare function doThis(value: "foo" | "bar"): void; +>doThis : Symbol(doThis, Decl(typeGuardNarrowsToLiteralTypeUnion.ts, 0, 65)) +>value : Symbol(value, Decl(typeGuardNarrowsToLiteralTypeUnion.ts, 1, 24)) + +declare function doThat(value: string) : void; +>doThat : Symbol(doThat, Decl(typeGuardNarrowsToLiteralTypeUnion.ts, 1, 52)) +>value : Symbol(value, Decl(typeGuardNarrowsToLiteralTypeUnion.ts, 2, 24)) + +let value: string; +>value : Symbol(value, Decl(typeGuardNarrowsToLiteralTypeUnion.ts, 3, 3)) + +if (isFoo(value)) { +>isFoo : Symbol(isFoo, Decl(typeGuardNarrowsToLiteralTypeUnion.ts, 0, 0)) +>value : Symbol(value, Decl(typeGuardNarrowsToLiteralTypeUnion.ts, 3, 3)) + + doThis(value); +>doThis : Symbol(doThis, Decl(typeGuardNarrowsToLiteralTypeUnion.ts, 0, 65)) +>value : Symbol(value, Decl(typeGuardNarrowsToLiteralTypeUnion.ts, 3, 3)) + +} else { + doThat(value); +>doThat : Symbol(doThat, Decl(typeGuardNarrowsToLiteralTypeUnion.ts, 1, 52)) +>value : Symbol(value, Decl(typeGuardNarrowsToLiteralTypeUnion.ts, 3, 3)) +} + + diff --git a/tests/baselines/reference/typeGuardNarrowsToLiteralTypeUnion.types b/tests/baselines/reference/typeGuardNarrowsToLiteralTypeUnion.types new file mode 100644 index 00000000000..321a8861d55 --- /dev/null +++ b/tests/baselines/reference/typeGuardNarrowsToLiteralTypeUnion.types @@ -0,0 +1,35 @@ +=== tests/cases/conformance/expressions/typeGuards/typeGuardNarrowsToLiteralTypeUnion.ts === +declare function isFoo(value: string) : value is ("foo" | "bar"); +>isFoo : (value: string) => value is "foo" | "bar" +>value : string +>value : any + +declare function doThis(value: "foo" | "bar"): void; +>doThis : (value: "foo" | "bar") => void +>value : "foo" | "bar" + +declare function doThat(value: string) : void; +>doThat : (value: string) => void +>value : string + +let value: string; +>value : string + +if (isFoo(value)) { +>isFoo(value) : boolean +>isFoo : (value: string) => value is "foo" | "bar" +>value : string + + doThis(value); +>doThis(value) : void +>doThis : (value: "foo" | "bar") => void +>value : "foo" | "bar" + +} else { + doThat(value); +>doThat(value) : void +>doThat : (value: string) => void +>value : string +} + + diff --git a/tests/cases/conformance/expressions/typeGuards/typeGuardNarrowsToLiteralTypeUnion.ts b/tests/cases/conformance/expressions/typeGuards/typeGuardNarrowsToLiteralTypeUnion.ts new file mode 100644 index 00000000000..8f4726160f4 --- /dev/null +++ b/tests/cases/conformance/expressions/typeGuards/typeGuardNarrowsToLiteralTypeUnion.ts @@ -0,0 +1,10 @@ +declare function isFoo(value: string) : value is ("foo" | "bar"); +declare function doThis(value: "foo" | "bar"): void; +declare function doThat(value: string) : void; +let value: string; +if (isFoo(value)) { + doThis(value); +} else { + doThat(value); +} + From 81ce532cde1f4ffd88e8c507b19432712723401f Mon Sep 17 00:00:00 2001 From: zhengbli Date: Wed, 25 May 2016 23:05:20 -0700 Subject: [PATCH 15/41] Change how typedef tag is parsed --- src/compiler/binder.ts | 7 +- src/compiler/parser.ts | 215 ++++++++++++++++++----------------------- src/compiler/types.ts | 13 +-- 3 files changed, 103 insertions(+), 132 deletions(-) diff --git a/src/compiler/binder.ts b/src/compiler/binder.ts index 9b421ed34cb..377c5d3f465 100644 --- a/src/compiler/binder.ts +++ b/src/compiler/binder.ts @@ -1702,8 +1702,9 @@ namespace ts { case SyntaxKind.PropertyDeclaration: case SyntaxKind.PropertySignature: case SyntaxKind.JSDocRecordMember: - case SyntaxKind.JSDocPropertyTag: return bindPropertyOrMethodOrAccessor(node, SymbolFlags.Property | ((node).questionToken ? SymbolFlags.Optional : SymbolFlags.None), SymbolFlags.PropertyExcludes); + case SyntaxKind.JSDocPropertyTag: + return bindJSDocProperty(node); case SyntaxKind.PropertyAssignment: case SyntaxKind.ShorthandPropertyAssignment: return bindPropertyOrMethodOrAccessor(node, SymbolFlags.Property, SymbolFlags.PropertyExcludes); @@ -2085,6 +2086,10 @@ namespace ts { : declareSymbolAndAddToSymbolTable(node, symbolFlags, symbolExcludes); } + function bindJSDocProperty(node: JSDocPropertyTag) { + return declareSymbolAndAddToSymbolTable(node, SymbolFlags.Property, SymbolFlags.PropertyExcludes); + } + // reachability checks function shouldReportErrorOnModuleDeclaration(node: ModuleDeclaration): boolean { diff --git a/src/compiler/parser.ts b/src/compiler/parser.ts index 9c26fdeb179..07509d0afcc 100644 --- a/src/compiler/parser.ts +++ b/src/compiler/parser.ts @@ -406,7 +406,7 @@ namespace ts { visitNode(cbNode, (node).name) || visitNode(cbNode, (node).type); case SyntaxKind.JSDocTypeLiteral: - return visitNodes(cbNodes, (node).members); + return visitNodes(cbNodes, (node).jsDocPropertyTags); case SyntaxKind.JSDocPropertyTag: return visitNode(cbNode, (node).typeExpression) || visitNode(cbNode, (node).name); @@ -4113,9 +4113,9 @@ namespace ts { const isAsync = !!(node.flags & NodeFlags.Async); node.name = isGenerator && isAsync ? doInYieldAndAwaitContext(parseOptionalIdentifier) : - isGenerator ? doInYieldContext(parseOptionalIdentifier) : - isAsync ? doInAwaitContext(parseOptionalIdentifier) : - parseOptionalIdentifier(); + isGenerator ? doInYieldContext(parseOptionalIdentifier) : + isAsync ? doInAwaitContext(parseOptionalIdentifier) : + parseOptionalIdentifier(); fillSignature(SyntaxKind.ColonToken, /*yieldContext*/ isGenerator, /*awaitContext*/ isAsync, /*requireCompleteParameterList*/ false, node); node.body = parseFunctionBlock(/*allowYield*/ isGenerator, /*allowAwait*/ isAsync, /*ignoreMissingOpenBrace*/ false); @@ -6066,9 +6066,6 @@ namespace ts { Debug.assert(end <= content.length); let tags: NodeArray; - let currentParentJSDocTag: JSDocParentTag; - let currentParentJSDocTagEnd: number; - let result: JSDocComment; // Check for /** (JSDoc opening part) @@ -6125,10 +6122,6 @@ namespace ts { nextJSDocToken(); } - if (currentParentJSDocTag) { - finishCurrentParentTag(); - } - result = createJSDocComment(); }); @@ -6152,40 +6145,6 @@ namespace ts { } } - function finishCurrentParentTag(): void { - if (!currentParentJSDocTag) { - return; - } - - if (currentParentJSDocTag.kind === SyntaxKind.JSDocTypedefTag) { - const typedefTag = currentParentJSDocTag; - if (typedefTag.jsDocTypeTag) { - if (typedefTag.jsDocTypeTag.typeExpression.type.kind === SyntaxKind.JSDocTypeReference) { - const typeTagtype = typedefTag.jsDocTypeTag.typeExpression.type; - if ((typeTagtype.name.kind !== SyntaxKind.Identifier) || - (typeTagtype.name).text !== "Object") { - typedefTag.type = typedefTag.jsDocTypeTag.typeExpression.type; - } - } - else { - typedefTag.type = typedefTag.jsDocTypeTag.typeExpression.type; - } - } - if (!typedefTag.type && typedefTag.jsDocPropertyTags && typedefTag.jsDocPropertyTags.length > 0) { - const pos = typedefTag.jsDocPropertyTags[0].pos; - const end = typedefTag.jsDocPropertyTags[typedefTag.jsDocPropertyTags.length - 1].end; - const jsdocTypeLiteral = createNode(SyntaxKind.JSDocTypeLiteral, pos); - jsdocTypeLiteral.members = >[]; - addRange(jsdocTypeLiteral.members, typedefTag.jsDocPropertyTags); - typedefTag.type = finishNode(jsdocTypeLiteral, end); - } - } - - addTag(finishNode(currentParentJSDocTag, currentParentJSDocTagEnd)); - currentParentJSDocTag = undefined; - currentParentJSDocTagEnd = undefined; - } - function parseTag(): void { Debug.assert(token === SyntaxKind.AtToken); const atToken = createNode(SyntaxKind.AtToken, scanner.getTokenPos()); @@ -6198,34 +6157,23 @@ namespace ts { } const tag = handleTag(atToken, tagName) || handleUnknownTag(atToken, tagName); - if (!currentParentJSDocTag) { - addTag(tag); - } + addTag(tag); } function handleTag(atToken: Node, tagName: Identifier): JSDocTag { if (tagName) { switch (tagName.text) { case "param": - finishCurrentParentTag(); return handleParamTag(atToken, tagName); case "return": case "returns": - finishCurrentParentTag(); return handleReturnTag(atToken, tagName); case "template": - finishCurrentParentTag(); return handleTemplateTag(atToken, tagName); case "type": - // @typedef tag is allowed to have one @type tag, therefore seeing - // a @type tag may not indicate the end of the current parent tag. return handleTypeTag(atToken, tagName); case "typedef": - finishCurrentParentTag(); return handleTypedefTag(atToken, tagName); - case "property": - case "prop": - return handlePropertyTag(atToken, tagName); } } @@ -6251,25 +6199,6 @@ namespace ts { } } - function addToCurrentParentTag(tag: JSDocTag): void { - if (!currentParentJSDocTag) { - return; - } - switch (tag.kind) { - case SyntaxKind.JSDocPropertyTag: - if (!currentParentJSDocTag.jsDocPropertyTags) { - currentParentJSDocTag.jsDocPropertyTags = >[]; - } - currentParentJSDocTag.jsDocPropertyTags.push(tag); - break; - case SyntaxKind.JSDocTypeTag: - if (!currentParentJSDocTag.jsDocTypeTag) { - currentParentJSDocTag.jsDocTypeTag = tag; - } - break; - } - } - function tryParseTypeExpression(): JSDocTypeExpression { if (token !== SyntaxKind.OpenBraceToken) { return undefined; @@ -6345,32 +6274,14 @@ namespace ts { parseErrorAtPosition(tagName.pos, scanner.getTokenPos() - tagName.pos, Diagnostics._0_tag_already_specified, tagName.text); } - let result = createNode(SyntaxKind.JSDocTypeTag, atToken.pos); + const result = createNode(SyntaxKind.JSDocTypeTag, atToken.pos); result.atToken = atToken; result.tagName = tagName; result.typeExpression = tryParseTypeExpression(); - result = finishNode(result); - - if (currentParentJSDocTag && currentParentJSDocTag.kind === SyntaxKind.JSDocTypedefTag) { - const parentTag = currentParentJSDocTag; - if (!parentTag.typeExpression && !parentTag.jsDocTypeTag) { - parentTag.jsDocTypeTag = result; - currentParentJSDocTagEnd = scanner.getStartPos(); - return result; - } - } - // If this @type tag is not part of the current parent tag, then - // it denotes the end of the current parent tag. - finishCurrentParentTag(); - return result; + return finishNode(result); } function handlePropertyTag(atToken: Node, tagName: Identifier): JSDocPropertyTag { - if (!currentParentJSDocTag) { - parseErrorAtPosition(tagName.pos, scanner.getTokenPos() - tagName.pos, Diagnostics._0_tag_cannot_be_used_independently_as_a_top_level_JSDoc_tag, tagName.text); - return undefined; - } - const typeExpression = tryParseTypeExpression(); skipWhitespace(); const name = parseJSDocIdentifierName(); @@ -6379,17 +6290,12 @@ namespace ts { return undefined; } - let result = createNode(SyntaxKind.JSDocPropertyTag, atToken.pos); + const result = createNode(SyntaxKind.JSDocPropertyTag, atToken.pos); result.atToken = atToken; result.tagName = tagName; result.name = name; result.typeExpression = typeExpression; - result.type = typeExpression.type; - result = finishNode(result); - - addToCurrentParentTag(result); - currentParentJSDocTagEnd = scanner.getStartPos(); - return undefined; + return finishNode(result); } function handleTypedefTag(atToken: Node, tagName: Identifier): JSDocTypedefTag { @@ -6413,32 +6319,99 @@ namespace ts { } } - const result = createNode(SyntaxKind.JSDocTypedefTag, atToken.pos); - result.atToken = atToken; - result.tagName = tagName; - result.name = name; - result.typeExpression = typeExpression; + const typedefTag = createNode(SyntaxKind.JSDocTypedefTag, atToken.pos); + typedefTag.atToken = atToken; + typedefTag.tagName = tagName; + typedefTag.name = name; + typedefTag.typeExpression = typeExpression; - if (typeExpression && typeExpression.type.kind === SyntaxKind.JSDocTypeReference) { - const jsDocTypeReference = typeExpression.type; - if (jsDocTypeReference.name.kind === SyntaxKind.Identifier) { - const name = jsDocTypeReference.name; - if (name.text === "Object") { - currentParentJSDocTag = result; + if (typeExpression) { + if (typeExpression.type.kind === SyntaxKind.JSDocTypeReference) { + const jsDocTypeReference = typeExpression.type; + if (jsDocTypeReference.name.kind === SyntaxKind.Identifier) { + const name = jsDocTypeReference.name; + if (name.text === "Object") { + typedefTag.type = scanChildTags(); + } } } + if (!typedefTag.type) { + typedefTag.type = typeExpression.type; + } } - else if (!typeExpression) { - currentParentJSDocTag = result; + else { + typedefTag.type = scanChildTags(); } - if (!currentParentJSDocTag) { - result.type = result.typeExpression.type; - return finishNode(result); + return finishNode(typedefTag); + + function scanChildTags(): JSDocTypeLiteral { + const jsDocTypeLiteral = createNode(SyntaxKind.JSDocTypeLiteral, scanner.getStartPos()); + let resumePos = scanner.getStartPos(); + let canParseTag = true; + let seenAsterisk = false; + let parentTagTerminated = false; + + while (token !== SyntaxKind.EndOfFileToken && !parentTagTerminated) { + nextJSDocToken(); + switch (token) { + case SyntaxKind.AtToken: + if (canParseTag) { + parentTagTerminated = !tryParseChildTag(jsDocTypeLiteral); + } + seenAsterisk = false; + break; + case SyntaxKind.NewLineTrivia: + resumePos = scanner.getStartPos() - 1; + canParseTag = true; + seenAsterisk = false; + break; + case SyntaxKind.AsteriskToken: + if (seenAsterisk) { + canParseTag = false; + } + seenAsterisk = true; + break; + case SyntaxKind.Identifier: + canParseTag = false; + case SyntaxKind.EndOfFileToken: + break; + } + } + scanner.setTextPos(resumePos); + return finishNode(jsDocTypeLiteral); + } + } + + function tryParseChildTag(parentTag: JSDocTypeLiteral): boolean { + Debug.assert(token === SyntaxKind.AtToken); + const atToken = createNode(SyntaxKind.AtToken, scanner.getStartPos()); + atToken.end = scanner.getTextPos(); + nextJSDocToken(); + + const tagName = parseJSDocIdentifierName(); + if (!tagName) { + return false; } - currentParentJSDocTagEnd = scanner.getStartPos(); - return undefined; + switch (tagName.text) { + case "type": + if (parentTag.jsDocTypeTag) { + // already has a @type tag, terminate the parent tag now. + return false; + } + parentTag.jsDocTypeTag = handleTypeTag(atToken, tagName); + return true; + case "prop": + case "property": + if (!parentTag.jsDocPropertyTags) { + parentTag.jsDocPropertyTags = >[]; + } + const propertyTag = handlePropertyTag(atToken, tagName); + parentTag.jsDocPropertyTags.push(propertyTag); + return true; + } + return false; } function handleTemplateTag(atToken: Node, tagName: Identifier): JSDocTemplateTag { diff --git a/src/compiler/types.ts b/src/compiler/types.ts index b472aa92e62..1b9af02a8d5 100644 --- a/src/compiler/types.ts +++ b/src/compiler/types.ts @@ -1517,29 +1517,22 @@ namespace ts { } // @kind(SyntaxKind.JSDocTypedefTag) - export interface JSDocTypedefTag extends JSDocTag, Declaration, JSDocParentTag { + export interface JSDocTypedefTag extends JSDocTag, Declaration { name: Identifier; typeExpression?: JSDocTypeExpression; type: JSDocType; } - export interface JSDocParentTag extends JSDocTag { - jsDocPropertyTags?: NodeArray; - jsDocTypeTag?: JSDocTypeTag; - } - // @kind(SyntaxKind.JSDocPropertyTag) export interface JSDocPropertyTag extends JSDocTag, TypeElement { name: Identifier; typeExpression: JSDocTypeExpression; - // Add a "type" property here to make the interface compatible - // with the "VariableLikeDeclaration" definition - type: TypeNode; } // @kind(SyntaxKind.JSDocTypeLiteral) export interface JSDocTypeLiteral extends JSDocType { - members: NodeArray; + jsDocPropertyTags?: NodeArray; + jsDocTypeTag?: JSDocTypeTag; } // @kind(SyntaxKind.JSDocParameterTag) From 3b5c72c4bc033f5bf4d38931b2eed0bf0bf7e5e9 Mon Sep 17 00:00:00 2001 From: Anders Hejlsberg Date: Thu, 26 May 2016 15:30:31 -0700 Subject: [PATCH 16/41] Include outer function expressions in control flow analysis --- src/compiler/binder.ts | 24 ++++++++++++++++- src/compiler/checker.ts | 57 +++++++++++++++++++++++++++++++++-------- src/compiler/types.ts | 7 +++++ 3 files changed, 77 insertions(+), 11 deletions(-) diff --git a/src/compiler/binder.ts b/src/compiler/binder.ts index a7cb0d12758..2476709b501 100644 --- a/src/compiler/binder.ts +++ b/src/compiler/binder.ts @@ -472,6 +472,9 @@ namespace ts { hasExplicitReturn = false; currentFlow = { flags: FlowFlags.Start }; + if (kind === SyntaxKind.FunctionExpression || kind === SyntaxKind.ArrowFunction) { + (currentFlow).container = node; + } currentBreakTarget = undefined; currentContinueTarget = undefined; activeLabels = undefined; @@ -589,6 +592,9 @@ namespace ts { case SyntaxKind.VariableDeclaration: bindVariableDeclarationFlow(node); break; + case SyntaxKind.CallExpression: + bindCallExpressionFlow(node); + break; default: forEachChild(node, bind); break; @@ -1098,6 +1104,20 @@ namespace ts { } } + function bindCallExpressionFlow(node: CallExpression) { + forEachChild(node, bind); + // If the target of the call expression is a function expression or arrow function we have + // an immediately invoked function expression (IIFE). Initialize the flowNode property to + // the current control flow (which includes evaluation of the IIFE arguments). + let expr: Expression = node.expression; + while (expr.kind === SyntaxKind.ParenthesizedExpression) { + expr = (expr).expression; + } + if (expr.kind === SyntaxKind.FunctionExpression || expr.kind === SyntaxKind.ArrowFunction) { + node.flowNode = currentFlow; + } + } + function getContainerFlags(node: Node): ContainerFlags { switch (node.kind) { case SyntaxKind.ClassExpression: @@ -2054,7 +2074,9 @@ namespace ts { hasAsyncFunctions = true; } } - + if (currentFlow) { + node.flowNode = currentFlow; + } checkStrictModeFunctionName(node); const bindingName = (node).name ? (node).name.text : "__function"; return bindAnonymousDeclaration(node, SymbolFlags.Function, bindingName); diff --git a/src/compiler/checker.ts b/src/compiler/checker.ts index 5a7c6fa29e5..c14a7e26add 100644 --- a/src/compiler/checker.ts +++ b/src/compiler/checker.ts @@ -7648,7 +7648,7 @@ namespace ts { getInitialTypeOfBindingElement(node); } - function getFlowTypeOfReference(reference: Node, declaredType: Type, assumeInitialized: boolean) { + function getFlowTypeOfReference(reference: Node, declaredType: Type, assumeInitialized: boolean, includeOuterFunctions: boolean) { let key: string; if (!reference.flowNode || assumeInitialized && !(declaredType.flags & TypeFlags.Narrowable)) { return declaredType; @@ -7694,15 +7694,30 @@ namespace ts { getTypeAtFlowBranchLabel(flow) : getTypeAtFlowLoopLabel(flow); } - else if (flow.flags & FlowFlags.Unreachable) { + else if (flow.flags & FlowFlags.Start) { + let container = (flow).container; + if (container) { + // If container is an IIFE continue with the control flow associated with the + // call expression node. + let iife = getImmediatelyInvokedFunctionExpression(container); + if (iife && iife.flowNode) { + flow = iife.flowNode; + continue; + } + // Check if we should continue with the control flow of the containing function. + if (includeOuterFunctions && container.flowNode) { + flow = container.flowNode; + continue; + } + } + // At the top of the flow we have the initial type. + type = initialType; + } + else { // Unreachable code errors are reported in the binding phase. Here we // simply return the declared type to reduce follow-on errors. type = declaredType; } - else { - // At the top of the flow we have the initial type. - type = initialType; - } if (flow.flags & FlowFlags.Shared) { // Record visited node and the associated type in the cache. visitedFlowNodes[visitedFlowCount] = flow; @@ -8071,6 +8086,27 @@ namespace ts { return expression; } + function getControlFlowContainer(node: Identifier, declarationContainer: Node, skipFunctionExpressions: boolean) { + let container = getContainingFunctionOrModule(node); + while (container !== declarationContainer && + (container.kind === SyntaxKind.FunctionExpression || container.kind === SyntaxKind.ArrowFunction) && + (skipFunctionExpressions || getImmediatelyInvokedFunctionExpression(container))) { + container = getContainingFunctionOrModule(container); + } + return container; + } + + function isDeclarationIncludedInFlow(reference: Node, declaration: Declaration, includeOuterFunctions: boolean) { + const declarationContainer = getContainingFunctionOrModule(declaration); + let container = getContainingFunctionOrModule(reference); + while (container !== declarationContainer && + (container.kind === SyntaxKind.FunctionExpression || container.kind === SyntaxKind.ArrowFunction) && + (includeOuterFunctions || getImmediatelyInvokedFunctionExpression(container))) { + container = getContainingFunctionOrModule(container); + } + return container === declarationContainer; + } + function checkIdentifier(node: Identifier): Type { const symbol = getResolvedSymbol(node); @@ -8127,10 +8163,11 @@ namespace ts { return type; } const declaration = localOrExportSymbol.valueDeclaration; + const includeOuterFunctions = isReadonlySymbol(localOrExportSymbol); const assumeInitialized = !strictNullChecks || (type.flags & TypeFlags.Any) !== 0 || !declaration || getRootDeclaration(declaration).kind === SyntaxKind.Parameter || isInAmbientContext(declaration) || - getContainingFunctionOrModule(declaration) !== getContainingFunctionOrModule(node); - const flowType = getFlowTypeOfReference(node, type, assumeInitialized); + !isDeclarationIncludedInFlow(node, declaration, includeOuterFunctions); + const flowType = getFlowTypeOfReference(node, type, assumeInitialized, includeOuterFunctions); if (!assumeInitialized && !(getNullableKind(type) & TypeFlags.Undefined) && getNullableKind(flowType) & TypeFlags.Undefined) { error(node, Diagnostics.Variable_0_is_used_before_being_assigned, symbolToString(symbol)); // Return the declared type to reduce follow-on errors @@ -8379,7 +8416,7 @@ namespace ts { if (isClassLike(container.parent)) { const symbol = getSymbolOfNode(container.parent); const type = container.flags & NodeFlags.Static ? getTypeOfSymbol(symbol) : (getDeclaredTypeOfSymbol(symbol)).thisType; - return getFlowTypeOfReference(node, type, /*assumeInitialized*/ true); + return getFlowTypeOfReference(node, type, /*assumeInitialized*/ true, /*includeOuterFunctions*/ true); } if (isInJavaScriptFile(node)) { @@ -9991,7 +10028,7 @@ namespace ts { return propType; } } - return getFlowTypeOfReference(node, propType, /*assumeInitialized*/ true); + return getFlowTypeOfReference(node, propType, /*assumeInitialized*/ true, /*includeOuterFunctions*/ false); } function isValidPropertyAccess(node: PropertyAccessExpression | QualifiedName, propertyName: string): boolean { diff --git a/src/compiler/types.ts b/src/compiler/types.ts index 7613c489920..762d710d504 100644 --- a/src/compiler/types.ts +++ b/src/compiler/types.ts @@ -1537,6 +1537,13 @@ namespace ts { id?: number; // Node id used by flow type cache in checker } + // FlowStart represents the start of a control flow. For a function expression or arrow + // function, the container property references the function (which in turn has a flowNode + // property for the containing control flow). + export interface FlowStart extends FlowNode { + container?: FunctionExpression | ArrowFunction; + } + // FlowLabel represents a junction with multiple possible preceding control flows. export interface FlowLabel extends FlowNode { antecedents: FlowNode[]; From d20664aa69b54cb05b427fb357c72a384100d131 Mon Sep 17 00:00:00 2001 From: Anders Hejlsberg Date: Thu, 26 May 2016 16:26:30 -0700 Subject: [PATCH 17/41] Fix test --- .../typeGuardsInFunctionAndModuleBlock.js | 4 +-- ...typeGuardsInFunctionAndModuleBlock.symbols | 18 +++++----- .../typeGuardsInFunctionAndModuleBlock.types | 34 +++++++++---------- .../typeGuardsInFunctionAndModuleBlock.ts | 2 +- 4 files changed, 29 insertions(+), 29 deletions(-) diff --git a/tests/baselines/reference/typeGuardsInFunctionAndModuleBlock.js b/tests/baselines/reference/typeGuardsInFunctionAndModuleBlock.js index 91443a9ae78..8ec45de814e 100644 --- a/tests/baselines/reference/typeGuardsInFunctionAndModuleBlock.js +++ b/tests/baselines/reference/typeGuardsInFunctionAndModuleBlock.js @@ -41,7 +41,7 @@ function foo4(x: number | string | boolean) { : x.toString(); // number })(x); // x here is narrowed to number | boolean } -// Type guards affect nested function expressions and nested function declarations +// Type guards do not affect nested function declarations function foo5(x: number | string | boolean) { if (typeof x === "string") { var y = x; // string; @@ -121,7 +121,7 @@ function foo4(x) { : x.toString(); // number })(x); // x here is narrowed to number | boolean } -// Type guards affect nested function expressions and nested function declarations +// Type guards do not affect nested function declarations function foo5(x) { if (typeof x === "string") { var y = x; // string; diff --git a/tests/baselines/reference/typeGuardsInFunctionAndModuleBlock.symbols b/tests/baselines/reference/typeGuardsInFunctionAndModuleBlock.symbols index bf21641624e..56eeb22bf9f 100644 --- a/tests/baselines/reference/typeGuardsInFunctionAndModuleBlock.symbols +++ b/tests/baselines/reference/typeGuardsInFunctionAndModuleBlock.symbols @@ -27,9 +27,9 @@ function foo(x: number | string | boolean) { >toString : Symbol(Object.toString, Decl(lib.d.ts, --, --)) : x.toString(); // number ->x.toString : Symbol(toString, Decl(lib.d.ts, --, --), Decl(lib.d.ts, --, --)) +>x.toString : Symbol(Number.toString, Decl(lib.d.ts, --, --)) >x : Symbol(x, Decl(typeGuardsInFunctionAndModuleBlock.ts, 2, 13)) ->toString : Symbol(toString, Decl(lib.d.ts, --, --), Decl(lib.d.ts, --, --)) +>toString : Symbol(Number.toString, Decl(lib.d.ts, --, --)) } (); } @@ -60,9 +60,9 @@ function foo2(x: number | string | boolean) { >toString : Symbol(Object.toString, Decl(lib.d.ts, --, --)) : x.toString(); // number ->x.toString : Symbol(toString, Decl(lib.d.ts, --, --), Decl(lib.d.ts, --, --)) +>x.toString : Symbol(Number.toString, Decl(lib.d.ts, --, --)) >x : Symbol(x, Decl(typeGuardsInFunctionAndModuleBlock.ts, 12, 14)) ->toString : Symbol(toString, Decl(lib.d.ts, --, --), Decl(lib.d.ts, --, --)) +>toString : Symbol(Number.toString, Decl(lib.d.ts, --, --)) } (x); // x here is narrowed to number | boolean >x : Symbol(x, Decl(typeGuardsInFunctionAndModuleBlock.ts, 12, 14)) @@ -91,9 +91,9 @@ function foo3(x: number | string | boolean) { >toString : Symbol(Object.toString, Decl(lib.d.ts, --, --)) : x.toString(); // number ->x.toString : Symbol(toString, Decl(lib.d.ts, --, --), Decl(lib.d.ts, --, --)) +>x.toString : Symbol(Number.toString, Decl(lib.d.ts, --, --)) >x : Symbol(x, Decl(typeGuardsInFunctionAndModuleBlock.ts, 22, 14)) ->toString : Symbol(toString, Decl(lib.d.ts, --, --), Decl(lib.d.ts, --, --)) +>toString : Symbol(Number.toString, Decl(lib.d.ts, --, --)) })(); } @@ -123,14 +123,14 @@ function foo4(x: number | string | boolean) { >toString : Symbol(Object.toString, Decl(lib.d.ts, --, --)) : x.toString(); // number ->x.toString : Symbol(toString, Decl(lib.d.ts, --, --), Decl(lib.d.ts, --, --)) +>x.toString : Symbol(Number.toString, Decl(lib.d.ts, --, --)) >x : Symbol(x, Decl(typeGuardsInFunctionAndModuleBlock.ts, 32, 14)) ->toString : Symbol(toString, Decl(lib.d.ts, --, --), Decl(lib.d.ts, --, --)) +>toString : Symbol(Number.toString, Decl(lib.d.ts, --, --)) })(x); // x here is narrowed to number | boolean >x : Symbol(x, Decl(typeGuardsInFunctionAndModuleBlock.ts, 32, 14)) } -// Type guards affect nested function expressions and nested function declarations +// Type guards do not affect 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)) diff --git a/tests/baselines/reference/typeGuardsInFunctionAndModuleBlock.types b/tests/baselines/reference/typeGuardsInFunctionAndModuleBlock.types index 67d1816cfc3..cc9553b9767 100644 --- a/tests/baselines/reference/typeGuardsInFunctionAndModuleBlock.types +++ b/tests/baselines/reference/typeGuardsInFunctionAndModuleBlock.types @@ -21,14 +21,14 @@ function foo(x: number | string | boolean) { >f : () => string var b = x; // number | boolean ->b : number | string | boolean ->x : number | string | boolean +>b : number | boolean +>x : number | boolean return typeof x === "boolean" >typeof x === "boolean" ? x.toString() // boolean : x.toString() : string >typeof x === "boolean" : boolean >typeof x : string ->x : number | string | boolean +>x : number | boolean >"boolean" : string ? x.toString() // boolean @@ -40,7 +40,7 @@ function foo(x: number | string | boolean) { : x.toString(); // number >x.toString() : string >x.toString : (radix?: number) => string ->x : number | string +>x : number >toString : (radix?: number) => string } (); @@ -66,14 +66,14 @@ function foo2(x: number | string | boolean) { >a : number | boolean var b = x; // new scope - number | boolean ->b : number | string | boolean ->x : number | string | boolean +>b : number | boolean +>x : number | boolean return typeof x === "boolean" >typeof x === "boolean" ? x.toString() // boolean : x.toString() : string >typeof x === "boolean" : boolean >typeof x : string ->x : number | string | boolean +>x : number | boolean >"boolean" : string ? x.toString() // boolean @@ -85,7 +85,7 @@ function foo2(x: number | string | boolean) { : x.toString(); // number >x.toString() : string >x.toString : (radix?: number) => string ->x : number | string +>x : number >toString : (radix?: number) => string } (x); // x here is narrowed to number | boolean @@ -111,14 +111,14 @@ function foo3(x: number | string | boolean) { >() => { var b = x; // new scope - number | boolean return typeof x === "boolean" ? x.toString() // boolean : x.toString(); // number } : () => string var b = x; // new scope - number | boolean ->b : number | string | boolean ->x : number | string | boolean +>b : number | boolean +>x : number | boolean return typeof x === "boolean" >typeof x === "boolean" ? x.toString() // boolean : x.toString() : string >typeof x === "boolean" : boolean >typeof x : string ->x : number | string | boolean +>x : number | boolean >"boolean" : string ? x.toString() // boolean @@ -130,7 +130,7 @@ function foo3(x: number | string | boolean) { : x.toString(); // number >x.toString() : string >x.toString : (radix?: number) => string ->x : number | string +>x : number >toString : (radix?: number) => string })(); @@ -156,14 +156,14 @@ function foo4(x: number | string | boolean) { >a : number | boolean var b = x; // new scope - number | boolean ->b : number | string | boolean ->x : number | string | boolean +>b : number | boolean +>x : number | boolean return typeof x === "boolean" >typeof x === "boolean" ? x.toString() // boolean : x.toString() : string >typeof x === "boolean" : boolean >typeof x : string ->x : number | string | boolean +>x : number | boolean >"boolean" : string ? x.toString() // boolean @@ -175,13 +175,13 @@ function foo4(x: number | string | boolean) { : x.toString(); // number >x.toString() : string >x.toString : (radix?: number) => string ->x : number | string +>x : number >toString : (radix?: number) => string })(x); // x here is narrowed to number | boolean >x : number | boolean } -// Type guards affect nested function expressions and nested function declarations +// Type guards do not affect nested function declarations function foo5(x: number | string | boolean) { >foo5 : (x: number | string | boolean) => void >x : number | string | boolean diff --git a/tests/cases/conformance/expressions/typeGuards/typeGuardsInFunctionAndModuleBlock.ts b/tests/cases/conformance/expressions/typeGuards/typeGuardsInFunctionAndModuleBlock.ts index 5dbc925d04b..f1ba854394b 100644 --- a/tests/cases/conformance/expressions/typeGuards/typeGuardsInFunctionAndModuleBlock.ts +++ b/tests/cases/conformance/expressions/typeGuards/typeGuardsInFunctionAndModuleBlock.ts @@ -40,7 +40,7 @@ function foo4(x: number | string | boolean) { : x.toString(); // number })(x); // x here is narrowed to number | boolean } -// Type guards affect nested function expressions and nested function declarations +// Type guards do not affect nested function declarations function foo5(x: number | string | boolean) { if (typeof x === "string") { var y = x; // string; From e5e21f9b5e34f1c999289a9ba7cbd9adbde67290 Mon Sep 17 00:00:00 2001 From: Anders Hejlsberg Date: Thu, 26 May 2016 16:26:40 -0700 Subject: [PATCH 18/41] Add new tests --- .../constLocalsInFunctionExpressions.js | 73 +++++++++ .../constLocalsInFunctionExpressions.symbols | 95 +++++++++++ .../constLocalsInFunctionExpressions.types | 121 ++++++++++++++ tests/baselines/reference/controlFlowIIFE.js | 87 ++++++++++ .../reference/controlFlowIIFE.symbols | 108 +++++++++++++ .../baselines/reference/controlFlowIIFE.types | 149 ++++++++++++++++++ .../constLocalsInFunctionExpressions.ts | 38 +++++ .../controlFlow/controlFlowIIFE.ts | 47 ++++++ 8 files changed, 718 insertions(+) create mode 100644 tests/baselines/reference/constLocalsInFunctionExpressions.js create mode 100644 tests/baselines/reference/constLocalsInFunctionExpressions.symbols create mode 100644 tests/baselines/reference/constLocalsInFunctionExpressions.types create mode 100644 tests/baselines/reference/controlFlowIIFE.js create mode 100644 tests/baselines/reference/controlFlowIIFE.symbols create mode 100644 tests/baselines/reference/controlFlowIIFE.types create mode 100644 tests/cases/conformance/controlFlow/constLocalsInFunctionExpressions.ts create mode 100644 tests/cases/conformance/controlFlow/controlFlowIIFE.ts diff --git a/tests/baselines/reference/constLocalsInFunctionExpressions.js b/tests/baselines/reference/constLocalsInFunctionExpressions.js new file mode 100644 index 00000000000..473de3b1a6c --- /dev/null +++ b/tests/baselines/reference/constLocalsInFunctionExpressions.js @@ -0,0 +1,73 @@ +//// [constLocalsInFunctionExpressions.ts] +declare function getStringOrNumber(): string | number; + +function f1() { + const x = getStringOrNumber(); + if (typeof x === "string") { + const f = () => x.length; + } +} + +function f2() { + const x = getStringOrNumber(); + if (typeof x !== "string") { + return; + } + const f = () => x.length; +} + +function f3() { + const x = getStringOrNumber(); + if (typeof x === "string") { + const f = function() { return x.length; }; + } +} + +function f4() { + const x = getStringOrNumber(); + if (typeof x !== "string") { + return; + } + const f = function() { return x.length; }; +} + +function f5() { + const x = getStringOrNumber(); + if (typeof x === "string") { + const f = () => () => x.length; + } +} + +//// [constLocalsInFunctionExpressions.js] +function f1() { + var x = getStringOrNumber(); + if (typeof x === "string") { + var f = function () { return x.length; }; + } +} +function f2() { + var x = getStringOrNumber(); + if (typeof x !== "string") { + return; + } + var f = function () { return x.length; }; +} +function f3() { + var x = getStringOrNumber(); + if (typeof x === "string") { + var f = function () { return x.length; }; + } +} +function f4() { + var x = getStringOrNumber(); + if (typeof x !== "string") { + return; + } + var f = function () { return x.length; }; +} +function f5() { + var x = getStringOrNumber(); + if (typeof x === "string") { + var f = function () { return function () { return x.length; }; }; + } +} diff --git a/tests/baselines/reference/constLocalsInFunctionExpressions.symbols b/tests/baselines/reference/constLocalsInFunctionExpressions.symbols new file mode 100644 index 00000000000..d9465a118aa --- /dev/null +++ b/tests/baselines/reference/constLocalsInFunctionExpressions.symbols @@ -0,0 +1,95 @@ +=== tests/cases/conformance/controlFlow/constLocalsInFunctionExpressions.ts === +declare function getStringOrNumber(): string | number; +>getStringOrNumber : Symbol(getStringOrNumber, Decl(constLocalsInFunctionExpressions.ts, 0, 0)) + +function f1() { +>f1 : Symbol(f1, Decl(constLocalsInFunctionExpressions.ts, 0, 54)) + + const x = getStringOrNumber(); +>x : Symbol(x, Decl(constLocalsInFunctionExpressions.ts, 3, 9)) +>getStringOrNumber : Symbol(getStringOrNumber, Decl(constLocalsInFunctionExpressions.ts, 0, 0)) + + if (typeof x === "string") { +>x : Symbol(x, Decl(constLocalsInFunctionExpressions.ts, 3, 9)) + + const f = () => x.length; +>f : Symbol(f, Decl(constLocalsInFunctionExpressions.ts, 5, 13)) +>x.length : Symbol(String.length, Decl(lib.d.ts, --, --)) +>x : Symbol(x, Decl(constLocalsInFunctionExpressions.ts, 3, 9)) +>length : Symbol(String.length, Decl(lib.d.ts, --, --)) + } +} + +function f2() { +>f2 : Symbol(f2, Decl(constLocalsInFunctionExpressions.ts, 7, 1)) + + const x = getStringOrNumber(); +>x : Symbol(x, Decl(constLocalsInFunctionExpressions.ts, 10, 9)) +>getStringOrNumber : Symbol(getStringOrNumber, Decl(constLocalsInFunctionExpressions.ts, 0, 0)) + + if (typeof x !== "string") { +>x : Symbol(x, Decl(constLocalsInFunctionExpressions.ts, 10, 9)) + + return; + } + const f = () => x.length; +>f : Symbol(f, Decl(constLocalsInFunctionExpressions.ts, 14, 9)) +>x.length : Symbol(String.length, Decl(lib.d.ts, --, --)) +>x : Symbol(x, Decl(constLocalsInFunctionExpressions.ts, 10, 9)) +>length : Symbol(String.length, Decl(lib.d.ts, --, --)) +} + +function f3() { +>f3 : Symbol(f3, Decl(constLocalsInFunctionExpressions.ts, 15, 1)) + + const x = getStringOrNumber(); +>x : Symbol(x, Decl(constLocalsInFunctionExpressions.ts, 18, 9)) +>getStringOrNumber : Symbol(getStringOrNumber, Decl(constLocalsInFunctionExpressions.ts, 0, 0)) + + if (typeof x === "string") { +>x : Symbol(x, Decl(constLocalsInFunctionExpressions.ts, 18, 9)) + + const f = function() { return x.length; }; +>f : Symbol(f, Decl(constLocalsInFunctionExpressions.ts, 20, 13)) +>x.length : Symbol(String.length, Decl(lib.d.ts, --, --)) +>x : Symbol(x, Decl(constLocalsInFunctionExpressions.ts, 18, 9)) +>length : Symbol(String.length, Decl(lib.d.ts, --, --)) + } +} + +function f4() { +>f4 : Symbol(f4, Decl(constLocalsInFunctionExpressions.ts, 22, 1)) + + const x = getStringOrNumber(); +>x : Symbol(x, Decl(constLocalsInFunctionExpressions.ts, 25, 9)) +>getStringOrNumber : Symbol(getStringOrNumber, Decl(constLocalsInFunctionExpressions.ts, 0, 0)) + + if (typeof x !== "string") { +>x : Symbol(x, Decl(constLocalsInFunctionExpressions.ts, 25, 9)) + + return; + } + const f = function() { return x.length; }; +>f : Symbol(f, Decl(constLocalsInFunctionExpressions.ts, 29, 9)) +>x.length : Symbol(String.length, Decl(lib.d.ts, --, --)) +>x : Symbol(x, Decl(constLocalsInFunctionExpressions.ts, 25, 9)) +>length : Symbol(String.length, Decl(lib.d.ts, --, --)) +} + +function f5() { +>f5 : Symbol(f5, Decl(constLocalsInFunctionExpressions.ts, 30, 1)) + + const x = getStringOrNumber(); +>x : Symbol(x, Decl(constLocalsInFunctionExpressions.ts, 33, 9)) +>getStringOrNumber : Symbol(getStringOrNumber, Decl(constLocalsInFunctionExpressions.ts, 0, 0)) + + if (typeof x === "string") { +>x : Symbol(x, Decl(constLocalsInFunctionExpressions.ts, 33, 9)) + + const f = () => () => x.length; +>f : Symbol(f, Decl(constLocalsInFunctionExpressions.ts, 35, 13)) +>x.length : Symbol(String.length, Decl(lib.d.ts, --, --)) +>x : Symbol(x, Decl(constLocalsInFunctionExpressions.ts, 33, 9)) +>length : Symbol(String.length, Decl(lib.d.ts, --, --)) + } +} diff --git a/tests/baselines/reference/constLocalsInFunctionExpressions.types b/tests/baselines/reference/constLocalsInFunctionExpressions.types new file mode 100644 index 00000000000..e9f0086f6b1 --- /dev/null +++ b/tests/baselines/reference/constLocalsInFunctionExpressions.types @@ -0,0 +1,121 @@ +=== tests/cases/conformance/controlFlow/constLocalsInFunctionExpressions.ts === +declare function getStringOrNumber(): string | number; +>getStringOrNumber : () => string | number + +function f1() { +>f1 : () => void + + const x = getStringOrNumber(); +>x : string | number +>getStringOrNumber() : string | number +>getStringOrNumber : () => string | number + + if (typeof x === "string") { +>typeof x === "string" : boolean +>typeof x : string +>x : string | number +>"string" : string + + const f = () => x.length; +>f : () => number +>() => x.length : () => number +>x.length : number +>x : string +>length : number + } +} + +function f2() { +>f2 : () => void + + const x = getStringOrNumber(); +>x : string | number +>getStringOrNumber() : string | number +>getStringOrNumber : () => string | number + + if (typeof x !== "string") { +>typeof x !== "string" : boolean +>typeof x : string +>x : string | number +>"string" : string + + return; + } + const f = () => x.length; +>f : () => number +>() => x.length : () => number +>x.length : number +>x : string +>length : number +} + +function f3() { +>f3 : () => void + + const x = getStringOrNumber(); +>x : string | number +>getStringOrNumber() : string | number +>getStringOrNumber : () => string | number + + if (typeof x === "string") { +>typeof x === "string" : boolean +>typeof x : string +>x : string | number +>"string" : string + + const f = function() { return x.length; }; +>f : () => number +>function() { return x.length; } : () => number +>x.length : number +>x : string +>length : number + } +} + +function f4() { +>f4 : () => void + + const x = getStringOrNumber(); +>x : string | number +>getStringOrNumber() : string | number +>getStringOrNumber : () => string | number + + if (typeof x !== "string") { +>typeof x !== "string" : boolean +>typeof x : string +>x : string | number +>"string" : string + + return; + } + const f = function() { return x.length; }; +>f : () => number +>function() { return x.length; } : () => number +>x.length : number +>x : string +>length : number +} + +function f5() { +>f5 : () => void + + const x = getStringOrNumber(); +>x : string | number +>getStringOrNumber() : string | number +>getStringOrNumber : () => string | number + + if (typeof x === "string") { +>typeof x === "string" : boolean +>typeof x : string +>x : string | number +>"string" : string + + const f = () => () => x.length; +>f : () => () => number +>() => () => x.length : () => () => number +>() => x.length : () => number +>x.length : number +>x : string +>length : number + } +} diff --git a/tests/baselines/reference/controlFlowIIFE.js b/tests/baselines/reference/controlFlowIIFE.js new file mode 100644 index 00000000000..6454d1117db --- /dev/null +++ b/tests/baselines/reference/controlFlowIIFE.js @@ -0,0 +1,87 @@ +//// [controlFlowIIFE.ts] + +declare function getStringOrNumber(): string | number; + +function f1() { + let x = getStringOrNumber(); + if (typeof x === "string") { + let n = function() { + return x.length; + }(); + } +} + +function f2() { + let x = getStringOrNumber(); + if (typeof x === "string") { + let n = (function() { + return x.length; + })(); + } +} + +function f3() { + let x = getStringOrNumber(); + let y: number; + if (typeof x === "string") { + let n = (z => x.length + y + z)(y = 1); + } +} + +// Repros from #8381 + +let maybeNumber: number | undefined; +(function () { + maybeNumber = 1; +})(); +if (maybeNumber !== undefined) { + maybeNumber++; +} + +let test: string | undefined; +if (!test) { + throw new Error('Test is not defined'); +} +(() => { + test.slice(1); // No error +})(); + +//// [controlFlowIIFE.js] +function f1() { + var x = getStringOrNumber(); + if (typeof x === "string") { + var n = function () { + return x.length; + }(); + } +} +function f2() { + var x = getStringOrNumber(); + if (typeof x === "string") { + var n = (function () { + return x.length; + })(); + } +} +function f3() { + var x = getStringOrNumber(); + var y; + if (typeof x === "string") { + var n = (function (z) { return x.length + y + z; })(y = 1); + } +} +// Repros from #8381 +var maybeNumber; +(function () { + maybeNumber = 1; +})(); +if (maybeNumber !== undefined) { + maybeNumber++; +} +var test; +if (!test) { + throw new Error('Test is not defined'); +} +(function () { + test.slice(1); // No error +})(); diff --git a/tests/baselines/reference/controlFlowIIFE.symbols b/tests/baselines/reference/controlFlowIIFE.symbols new file mode 100644 index 00000000000..2a03bb3ff2b --- /dev/null +++ b/tests/baselines/reference/controlFlowIIFE.symbols @@ -0,0 +1,108 @@ +=== tests/cases/conformance/controlFlow/controlFlowIIFE.ts === + +declare function getStringOrNumber(): string | number; +>getStringOrNumber : Symbol(getStringOrNumber, Decl(controlFlowIIFE.ts, 0, 0)) + +function f1() { +>f1 : Symbol(f1, Decl(controlFlowIIFE.ts, 1, 54)) + + let x = getStringOrNumber(); +>x : Symbol(x, Decl(controlFlowIIFE.ts, 4, 7)) +>getStringOrNumber : Symbol(getStringOrNumber, Decl(controlFlowIIFE.ts, 0, 0)) + + if (typeof x === "string") { +>x : Symbol(x, Decl(controlFlowIIFE.ts, 4, 7)) + + let n = function() { +>n : Symbol(n, Decl(controlFlowIIFE.ts, 6, 11)) + + return x.length; +>x.length : Symbol(String.length, Decl(lib.d.ts, --, --)) +>x : Symbol(x, Decl(controlFlowIIFE.ts, 4, 7)) +>length : Symbol(String.length, Decl(lib.d.ts, --, --)) + + }(); + } +} + +function f2() { +>f2 : Symbol(f2, Decl(controlFlowIIFE.ts, 10, 1)) + + let x = getStringOrNumber(); +>x : Symbol(x, Decl(controlFlowIIFE.ts, 13, 7)) +>getStringOrNumber : Symbol(getStringOrNumber, Decl(controlFlowIIFE.ts, 0, 0)) + + if (typeof x === "string") { +>x : Symbol(x, Decl(controlFlowIIFE.ts, 13, 7)) + + let n = (function() { +>n : Symbol(n, Decl(controlFlowIIFE.ts, 15, 11)) + + return x.length; +>x.length : Symbol(String.length, Decl(lib.d.ts, --, --)) +>x : Symbol(x, Decl(controlFlowIIFE.ts, 13, 7)) +>length : Symbol(String.length, Decl(lib.d.ts, --, --)) + + })(); + } +} + +function f3() { +>f3 : Symbol(f3, Decl(controlFlowIIFE.ts, 19, 1)) + + let x = getStringOrNumber(); +>x : Symbol(x, Decl(controlFlowIIFE.ts, 22, 7)) +>getStringOrNumber : Symbol(getStringOrNumber, Decl(controlFlowIIFE.ts, 0, 0)) + + let y: number; +>y : Symbol(y, Decl(controlFlowIIFE.ts, 23, 7)) + + if (typeof x === "string") { +>x : Symbol(x, Decl(controlFlowIIFE.ts, 22, 7)) + + let n = (z => x.length + y + z)(y = 1); +>n : Symbol(n, Decl(controlFlowIIFE.ts, 25, 11)) +>z : Symbol(z, Decl(controlFlowIIFE.ts, 25, 17)) +>x.length : Symbol(String.length, Decl(lib.d.ts, --, --)) +>x : Symbol(x, Decl(controlFlowIIFE.ts, 22, 7)) +>length : Symbol(String.length, Decl(lib.d.ts, --, --)) +>y : Symbol(y, Decl(controlFlowIIFE.ts, 23, 7)) +>z : Symbol(z, Decl(controlFlowIIFE.ts, 25, 17)) +>y : Symbol(y, Decl(controlFlowIIFE.ts, 23, 7)) + } +} + +// Repros from #8381 + +let maybeNumber: number | undefined; +>maybeNumber : Symbol(maybeNumber, Decl(controlFlowIIFE.ts, 31, 3)) + +(function () { + maybeNumber = 1; +>maybeNumber : Symbol(maybeNumber, Decl(controlFlowIIFE.ts, 31, 3)) + +})(); +if (maybeNumber !== undefined) { +>maybeNumber : Symbol(maybeNumber, Decl(controlFlowIIFE.ts, 31, 3)) +>undefined : Symbol(undefined) + + maybeNumber++; +>maybeNumber : Symbol(maybeNumber, Decl(controlFlowIIFE.ts, 31, 3)) +} + +let test: string | undefined; +>test : Symbol(test, Decl(controlFlowIIFE.ts, 39, 3)) + +if (!test) { +>test : Symbol(test, Decl(controlFlowIIFE.ts, 39, 3)) + + throw new Error('Test is not defined'); +>Error : Symbol(Error, Decl(lib.d.ts, --, --), Decl(lib.d.ts, --, --)) +} +(() => { + test.slice(1); // No error +>test.slice : Symbol(String.slice, Decl(lib.d.ts, --, --)) +>test : Symbol(test, Decl(controlFlowIIFE.ts, 39, 3)) +>slice : Symbol(String.slice, Decl(lib.d.ts, --, --)) + +})(); diff --git a/tests/baselines/reference/controlFlowIIFE.types b/tests/baselines/reference/controlFlowIIFE.types new file mode 100644 index 00000000000..0a6ad5da02f --- /dev/null +++ b/tests/baselines/reference/controlFlowIIFE.types @@ -0,0 +1,149 @@ +=== tests/cases/conformance/controlFlow/controlFlowIIFE.ts === + +declare function getStringOrNumber(): string | number; +>getStringOrNumber : () => string | number + +function f1() { +>f1 : () => void + + let x = getStringOrNumber(); +>x : string | number +>getStringOrNumber() : string | number +>getStringOrNumber : () => string | number + + if (typeof x === "string") { +>typeof x === "string" : boolean +>typeof x : string +>x : string | number +>"string" : string + + let n = function() { +>n : number +>function() { return x.length; }() : number +>function() { return x.length; } : () => number + + return x.length; +>x.length : number +>x : string +>length : number + + }(); + } +} + +function f2() { +>f2 : () => void + + let x = getStringOrNumber(); +>x : string | number +>getStringOrNumber() : string | number +>getStringOrNumber : () => string | number + + if (typeof x === "string") { +>typeof x === "string" : boolean +>typeof x : string +>x : string | number +>"string" : string + + let n = (function() { +>n : number +>(function() { return x.length; })() : number +>(function() { return x.length; }) : () => number +>function() { return x.length; } : () => number + + return x.length; +>x.length : number +>x : string +>length : number + + })(); + } +} + +function f3() { +>f3 : () => void + + let x = getStringOrNumber(); +>x : string | number +>getStringOrNumber() : string | number +>getStringOrNumber : () => string | number + + let y: number; +>y : number + + if (typeof x === "string") { +>typeof x === "string" : boolean +>typeof x : string +>x : string | number +>"string" : string + + let n = (z => x.length + y + z)(y = 1); +>n : number +>(z => x.length + y + z)(y = 1) : number +>(z => x.length + y + z) : (z: number) => number +>z => x.length + y + z : (z: number) => number +>z : number +>x.length + y + z : number +>x.length + y : number +>x.length : number +>x : string +>length : number +>y : number +>z : number +>y = 1 : number +>y : number +>1 : number + } +} + +// Repros from #8381 + +let maybeNumber: number | undefined; +>maybeNumber : number | undefined + +(function () { +>(function () { maybeNumber = 1;})() : void +>(function () { maybeNumber = 1;}) : () => void +>function () { maybeNumber = 1;} : () => void + + maybeNumber = 1; +>maybeNumber = 1 : number +>maybeNumber : number | undefined +>1 : number + +})(); +if (maybeNumber !== undefined) { +>maybeNumber !== undefined : boolean +>maybeNumber : number | undefined +>undefined : undefined + + maybeNumber++; +>maybeNumber++ : number +>maybeNumber : number +} + +let test: string | undefined; +>test : string | undefined + +if (!test) { +>!test : boolean +>test : string | undefined + + throw new Error('Test is not defined'); +>new Error('Test is not defined') : Error +>Error : ErrorConstructor +>'Test is not defined' : string +} +(() => { +>(() => { test.slice(1); // No error})() : void +>(() => { test.slice(1); // No error}) : () => void +>() => { test.slice(1); // No error} : () => void + + test.slice(1); // No error +>test.slice(1) : string +>test.slice : (start?: number | undefined, end?: number | undefined) => string +>test : string +>slice : (start?: number | undefined, end?: number | undefined) => string +>1 : number + +})(); diff --git a/tests/cases/conformance/controlFlow/constLocalsInFunctionExpressions.ts b/tests/cases/conformance/controlFlow/constLocalsInFunctionExpressions.ts new file mode 100644 index 00000000000..69b19fcb307 --- /dev/null +++ b/tests/cases/conformance/controlFlow/constLocalsInFunctionExpressions.ts @@ -0,0 +1,38 @@ +declare function getStringOrNumber(): string | number; + +function f1() { + const x = getStringOrNumber(); + if (typeof x === "string") { + const f = () => x.length; + } +} + +function f2() { + const x = getStringOrNumber(); + if (typeof x !== "string") { + return; + } + const f = () => x.length; +} + +function f3() { + const x = getStringOrNumber(); + if (typeof x === "string") { + const f = function() { return x.length; }; + } +} + +function f4() { + const x = getStringOrNumber(); + if (typeof x !== "string") { + return; + } + const f = function() { return x.length; }; +} + +function f5() { + const x = getStringOrNumber(); + if (typeof x === "string") { + const f = () => () => x.length; + } +} \ No newline at end of file diff --git a/tests/cases/conformance/controlFlow/controlFlowIIFE.ts b/tests/cases/conformance/controlFlow/controlFlowIIFE.ts new file mode 100644 index 00000000000..e703fb2fdff --- /dev/null +++ b/tests/cases/conformance/controlFlow/controlFlowIIFE.ts @@ -0,0 +1,47 @@ +// @strictNullChecks: true + +declare function getStringOrNumber(): string | number; + +function f1() { + let x = getStringOrNumber(); + if (typeof x === "string") { + let n = function() { + return x.length; + }(); + } +} + +function f2() { + let x = getStringOrNumber(); + if (typeof x === "string") { + let n = (function() { + return x.length; + })(); + } +} + +function f3() { + let x = getStringOrNumber(); + let y: number; + if (typeof x === "string") { + let n = (z => x.length + y + z)(y = 1); + } +} + +// Repros from #8381 + +let maybeNumber: number | undefined; +(function () { + maybeNumber = 1; +})(); +if (maybeNumber !== undefined) { + maybeNumber++; +} + +let test: string | undefined; +if (!test) { + throw new Error('Test is not defined'); +} +(() => { + test.slice(1); // No error +})(); \ No newline at end of file From 1647d20d90e0cc68a2714abeb7a1e81bb32c2b17 Mon Sep 17 00:00:00 2001 From: Anders Hejlsberg Date: Thu, 26 May 2016 16:50:36 -0700 Subject: [PATCH 19/41] Fix linting errors --- src/compiler/checker.ts | 14 ++------------ 1 file changed, 2 insertions(+), 12 deletions(-) diff --git a/src/compiler/checker.ts b/src/compiler/checker.ts index c14a7e26add..58aaae9f72b 100644 --- a/src/compiler/checker.ts +++ b/src/compiler/checker.ts @@ -7695,11 +7695,11 @@ namespace ts { getTypeAtFlowLoopLabel(flow); } else if (flow.flags & FlowFlags.Start) { - let container = (flow).container; + const container = (flow).container; if (container) { // If container is an IIFE continue with the control flow associated with the // call expression node. - let iife = getImmediatelyInvokedFunctionExpression(container); + const iife = getImmediatelyInvokedFunctionExpression(container); if (iife && iife.flowNode) { flow = iife.flowNode; continue; @@ -8086,16 +8086,6 @@ namespace ts { return expression; } - function getControlFlowContainer(node: Identifier, declarationContainer: Node, skipFunctionExpressions: boolean) { - let container = getContainingFunctionOrModule(node); - while (container !== declarationContainer && - (container.kind === SyntaxKind.FunctionExpression || container.kind === SyntaxKind.ArrowFunction) && - (skipFunctionExpressions || getImmediatelyInvokedFunctionExpression(container))) { - container = getContainingFunctionOrModule(container); - } - return container; - } - function isDeclarationIncludedInFlow(reference: Node, declaration: Declaration, includeOuterFunctions: boolean) { const declarationContainer = getContainingFunctionOrModule(declaration); let container = getContainingFunctionOrModule(reference); From e8ecf0e615ca552b7355740cd91cae6f8ed7b74e Mon Sep 17 00:00:00 2001 From: Anders Hejlsberg Date: Sat, 28 May 2016 06:43:28 -0700 Subject: [PATCH 20/41] Track return statements in IIFE using a flow label --- src/compiler/binder.ts | 83 +++++++++++++++++++++++++-------------- src/compiler/checker.ts | 63 ++++++++++------------------- src/compiler/types.ts | 1 + src/compiler/utilities.ts | 14 +++++++ 4 files changed, 89 insertions(+), 72 deletions(-) diff --git a/src/compiler/binder.ts b/src/compiler/binder.ts index 2476709b501..93417493fa7 100644 --- a/src/compiler/binder.ts +++ b/src/compiler/binder.ts @@ -104,14 +104,15 @@ namespace ts { let seenThisKeyword: boolean; // state used by reachability checks - let hasExplicitReturn: boolean; let currentFlow: FlowNode; let currentBreakTarget: FlowLabel; let currentContinueTarget: FlowLabel; + let currentReturnTarget: FlowLabel; let currentTrueTarget: FlowLabel; let currentFalseTarget: FlowLabel; let preSwitchCaseFlow: FlowNode; let activeLabels: ActiveLabel[]; + let hasExplicitReturn: boolean; // state used for emit helpers let hasClassExtends: boolean; @@ -156,13 +157,14 @@ namespace ts { blockScopeContainer = undefined; lastContainer = undefined; seenThisKeyword = false; - hasExplicitReturn = false; currentFlow = undefined; currentBreakTarget = undefined; currentContinueTarget = undefined; + currentReturnTarget = undefined; currentTrueTarget = undefined; currentFalseTarget = undefined; activeLabels = undefined; + hasExplicitReturn = false; hasClassExtends = false; hasAsyncFunctions = false; hasDecorators = false; @@ -443,20 +445,20 @@ namespace ts { blockScopeContainer.locals = undefined; } - let savedHasExplicitReturn: boolean; - let savedCurrentFlow: FlowNode; - let savedBreakTarget: FlowLabel; - let savedContinueTarget: FlowLabel; - let savedActiveLabels: ActiveLabel[]; + let saveCurrentFlow: FlowNode; + let saveBreakTarget: FlowLabel; + let saveContinueTarget: FlowLabel; + let saveReturnTarget: FlowLabel; + let saveActiveLabels: ActiveLabel[]; + let saveHasExplicitReturn: boolean; + let isIIFE: boolean; const kind = node.kind; let flags = node.flags; - // reset all reachability check related flags on node (for incremental scenarios) - flags &= ~NodeFlags.ReachabilityCheckFlags; - - // reset all emit helper flags on node (for incremental scenarios) - flags &= ~NodeFlags.EmitHelperFlags; + // Reset all reachability check related flags on node (for incremental scenarios) + // Reset all emit helper flags on node (for incremental scenarios) + flags &= ~NodeFlags.ReachabilityAndEmitFlags; if (kind === SyntaxKind.InterfaceDeclaration) { seenThisKeyword = false; @@ -464,23 +466,30 @@ namespace ts { const saveState = kind === SyntaxKind.SourceFile || kind === SyntaxKind.ModuleBlock || isFunctionLikeKind(kind); if (saveState) { - savedHasExplicitReturn = hasExplicitReturn; - savedCurrentFlow = currentFlow; - savedBreakTarget = currentBreakTarget; - savedContinueTarget = currentContinueTarget; - savedActiveLabels = activeLabels; - - hasExplicitReturn = false; - currentFlow = { flags: FlowFlags.Start }; - if (kind === SyntaxKind.FunctionExpression || kind === SyntaxKind.ArrowFunction) { - (currentFlow).container = node; + saveCurrentFlow = currentFlow; + saveBreakTarget = currentBreakTarget; + saveContinueTarget = currentContinueTarget; + saveReturnTarget = currentReturnTarget; + saveActiveLabels = activeLabels; + saveHasExplicitReturn = hasExplicitReturn; + isIIFE = (kind === SyntaxKind.FunctionExpression || kind === SyntaxKind.ArrowFunction) && !!getImmediatelyInvokedFunctionExpression(node); + if (isIIFE) { + currentReturnTarget = createBranchLabel(); + } + else { + currentFlow = { flags: FlowFlags.Start }; + if (kind === SyntaxKind.FunctionExpression || kind === SyntaxKind.ArrowFunction) { + (currentFlow).container = node; + } + currentReturnTarget = undefined; } currentBreakTarget = undefined; currentContinueTarget = undefined; activeLabels = undefined; + hasExplicitReturn = false; } - if (isInJavaScriptFile(node) && node.jsDocComment) { + if (node.flags & NodeFlags.JavaScriptFile && node.jsDocComment) { bind(node.jsDocComment); } @@ -518,11 +527,18 @@ namespace ts { node.flags = flags; if (saveState) { - hasExplicitReturn = savedHasExplicitReturn; - currentFlow = savedCurrentFlow; - currentBreakTarget = savedBreakTarget; - currentContinueTarget = savedContinueTarget; - activeLabels = savedActiveLabels; + if (isIIFE) { + addAntecedent(currentReturnTarget, currentFlow); + currentFlow = finishFlowLabel(currentReturnTarget); + } + else { + currentFlow = saveCurrentFlow; + } + currentBreakTarget = saveBreakTarget; + currentContinueTarget = saveContinueTarget; + currentReturnTarget = saveReturnTarget; + activeLabels = saveActiveLabels; + hasExplicitReturn = saveHasExplicitReturn; } container = saveContainer; @@ -854,6 +870,9 @@ namespace ts { bind(node.expression); if (node.kind === SyntaxKind.ReturnStatement) { hasExplicitReturn = true; + if (currentReturnTarget) { + addAntecedent(currentReturnTarget, currentFlow); + } } currentFlow = unreachableFlow; } @@ -1105,7 +1124,6 @@ namespace ts { } function bindCallExpressionFlow(node: CallExpression) { - forEachChild(node, bind); // If the target of the call expression is a function expression or arrow function we have // an immediately invoked function expression (IIFE). Initialize the flowNode property to // the current control flow (which includes evaluation of the IIFE arguments). @@ -1114,7 +1132,12 @@ namespace ts { expr = (expr).expression; } if (expr.kind === SyntaxKind.FunctionExpression || expr.kind === SyntaxKind.ArrowFunction) { - node.flowNode = currentFlow; + forEach(node.typeArguments, bind); + forEach(node.arguments, bind); + bind(node.expression); + } + else { + forEachChild(node, bind); } } diff --git a/src/compiler/checker.ts b/src/compiler/checker.ts index 58aaae9f72b..037ab78413a 100644 --- a/src/compiler/checker.ts +++ b/src/compiler/checker.ts @@ -7695,20 +7695,11 @@ namespace ts { getTypeAtFlowLoopLabel(flow); } else if (flow.flags & FlowFlags.Start) { + // Check if we should continue with the control flow of the containing function. const container = (flow).container; - if (container) { - // If container is an IIFE continue with the control flow associated with the - // call expression node. - const iife = getImmediatelyInvokedFunctionExpression(container); - if (iife && iife.flowNode) { - flow = iife.flowNode; - continue; - } - // Check if we should continue with the control flow of the containing function. - if (includeOuterFunctions && container.flowNode) { - flow = container.flowNode; - continue; - } + if (container && includeOuterFunctions) { + flow = container.flowNode; + continue; } // At the top of the flow we have the initial type. type = initialType; @@ -8652,23 +8643,25 @@ namespace ts { function getContextuallyTypedParameterType(parameter: ParameterDeclaration): Type { const func = parameter.parent; if (isContextSensitiveFunctionOrObjectLiteralMethod(func)) { - const iife = getImmediatelyInvokedFunctionExpression(func); - if (iife) { - const indexOfParameter = indexOf(func.parameters, parameter); - if (iife.arguments && indexOfParameter < iife.arguments.length) { - if (parameter.dotDotDotToken) { - const restTypes: Type[] = []; - for (let i = indexOfParameter; i < iife.arguments.length; i++) { - restTypes.push(getTypeOfExpression(iife.arguments[i])); + if (func.kind === SyntaxKind.FunctionExpression || func.kind === SyntaxKind.ArrowFunction) { + const iife = getImmediatelyInvokedFunctionExpression(func); + if (iife) { + const indexOfParameter = indexOf(func.parameters, parameter); + if (iife.arguments && indexOfParameter < iife.arguments.length) { + if (parameter.dotDotDotToken) { + const restTypes: Type[] = []; + for (let i = indexOfParameter; i < iife.arguments.length; i++) { + restTypes.push(getTypeOfExpression(iife.arguments[i])); + } + return createArrayType(getUnionType(restTypes)); } - return createArrayType(getUnionType(restTypes)); + const links = getNodeLinks(iife); + const cached = links.resolvedSignature; + links.resolvedSignature = anySignature; + const type = checkExpression(iife.arguments[indexOfParameter]); + links.resolvedSignature = cached; + return type; } - const links = getNodeLinks(iife); - const cached = links.resolvedSignature; - links.resolvedSignature = anySignature; - const type = checkExpression(iife.arguments[indexOfParameter]); - links.resolvedSignature = cached; - return type; } } const contextualSignature = getContextualSignature(func); @@ -8691,20 +8684,6 @@ namespace ts { return undefined; } - function getImmediatelyInvokedFunctionExpression(func: FunctionExpression | MethodDeclaration) { - if (isFunctionExpressionOrArrowFunction(func)) { - let prev: Node = func; - let parent: Node = func.parent; - while (parent.kind === SyntaxKind.ParenthesizedExpression) { - prev = parent; - parent = parent.parent; - } - if (parent.kind === SyntaxKind.CallExpression && (parent as CallExpression).expression === prev) { - return parent as CallExpression; - } - } - } - // In a variable, parameter or property declaration with a type annotation, // the contextual type of an initializer expression is the type of the variable, parameter or property. // Otherwise, in a parameter declaration of a contextually typed function expression, diff --git a/src/compiler/types.ts b/src/compiler/types.ts index 762d710d504..4e55956fa7c 100644 --- a/src/compiler/types.ts +++ b/src/compiler/types.ts @@ -416,6 +416,7 @@ namespace ts { ReachabilityCheckFlags = HasImplicitReturn | HasExplicitReturn, EmitHelperFlags = HasClassExtends | HasDecorators | HasParamDecorators | HasAsyncFunctions, + ReachabilityAndEmitFlags = ReachabilityCheckFlags | EmitHelperFlags, // Parsing context flags ContextFlags = DisallowInContext | YieldContext | DecoratorContext | AwaitContext | JavaScriptFile, diff --git a/src/compiler/utilities.ts b/src/compiler/utilities.ts index 7969389d788..500fa722dda 100644 --- a/src/compiler/utilities.ts +++ b/src/compiler/utilities.ts @@ -987,6 +987,20 @@ namespace ts { } } + export function getImmediatelyInvokedFunctionExpression(func: Node): CallExpression { + if (func.kind === SyntaxKind.FunctionExpression || func.kind === SyntaxKind.ArrowFunction) { + let prev = func; + let parent = func.parent; + while (parent.kind === SyntaxKind.ParenthesizedExpression) { + prev = parent; + parent = parent.parent; + } + if (parent.kind === SyntaxKind.CallExpression && (parent as CallExpression).expression === prev) { + return parent as CallExpression; + } + } + } + /** * Determines whether a node is a property or element access expression for super. */ From 4d730a5c6a6cda260f5e382fa2c4b8cf92cb578c Mon Sep 17 00:00:00 2001 From: Anders Hejlsberg Date: Sat, 28 May 2016 06:45:10 -0700 Subject: [PATCH 21/41] Update test --- tests/baselines/reference/controlFlowIIFE.js | 2 ++ tests/baselines/reference/controlFlowIIFE.symbols | 9 ++++++--- tests/baselines/reference/controlFlowIIFE.types | 6 +++++- tests/cases/conformance/controlFlow/controlFlowIIFE.ts | 1 + 4 files changed, 14 insertions(+), 4 deletions(-) diff --git a/tests/baselines/reference/controlFlowIIFE.js b/tests/baselines/reference/controlFlowIIFE.js index 6454d1117db..5d295b57315 100644 --- a/tests/baselines/reference/controlFlowIIFE.js +++ b/tests/baselines/reference/controlFlowIIFE.js @@ -34,6 +34,7 @@ let maybeNumber: number | undefined; (function () { maybeNumber = 1; })(); +maybeNumber++; if (maybeNumber !== undefined) { maybeNumber++; } @@ -75,6 +76,7 @@ var maybeNumber; (function () { maybeNumber = 1; })(); +maybeNumber++; if (maybeNumber !== undefined) { maybeNumber++; } diff --git a/tests/baselines/reference/controlFlowIIFE.symbols b/tests/baselines/reference/controlFlowIIFE.symbols index 2a03bb3ff2b..b1b2b741158 100644 --- a/tests/baselines/reference/controlFlowIIFE.symbols +++ b/tests/baselines/reference/controlFlowIIFE.symbols @@ -82,6 +82,9 @@ let maybeNumber: number | undefined; >maybeNumber : Symbol(maybeNumber, Decl(controlFlowIIFE.ts, 31, 3)) })(); +maybeNumber++; +>maybeNumber : Symbol(maybeNumber, Decl(controlFlowIIFE.ts, 31, 3)) + if (maybeNumber !== undefined) { >maybeNumber : Symbol(maybeNumber, Decl(controlFlowIIFE.ts, 31, 3)) >undefined : Symbol(undefined) @@ -91,10 +94,10 @@ if (maybeNumber !== undefined) { } let test: string | undefined; ->test : Symbol(test, Decl(controlFlowIIFE.ts, 39, 3)) +>test : Symbol(test, Decl(controlFlowIIFE.ts, 40, 3)) if (!test) { ->test : Symbol(test, Decl(controlFlowIIFE.ts, 39, 3)) +>test : Symbol(test, Decl(controlFlowIIFE.ts, 40, 3)) throw new Error('Test is not defined'); >Error : Symbol(Error, Decl(lib.d.ts, --, --), Decl(lib.d.ts, --, --)) @@ -102,7 +105,7 @@ if (!test) { (() => { test.slice(1); // No error >test.slice : Symbol(String.slice, Decl(lib.d.ts, --, --)) ->test : Symbol(test, Decl(controlFlowIIFE.ts, 39, 3)) +>test : Symbol(test, Decl(controlFlowIIFE.ts, 40, 3)) >slice : Symbol(String.slice, Decl(lib.d.ts, --, --)) })(); diff --git a/tests/baselines/reference/controlFlowIIFE.types b/tests/baselines/reference/controlFlowIIFE.types index 0a6ad5da02f..c041de04bc7 100644 --- a/tests/baselines/reference/controlFlowIIFE.types +++ b/tests/baselines/reference/controlFlowIIFE.types @@ -112,9 +112,13 @@ let maybeNumber: number | undefined; >1 : number })(); +maybeNumber++; +>maybeNumber++ : number +>maybeNumber : number + if (maybeNumber !== undefined) { >maybeNumber !== undefined : boolean ->maybeNumber : number | undefined +>maybeNumber : number >undefined : undefined maybeNumber++; diff --git a/tests/cases/conformance/controlFlow/controlFlowIIFE.ts b/tests/cases/conformance/controlFlow/controlFlowIIFE.ts index e703fb2fdff..c72f038c1ec 100644 --- a/tests/cases/conformance/controlFlow/controlFlowIIFE.ts +++ b/tests/cases/conformance/controlFlow/controlFlowIIFE.ts @@ -34,6 +34,7 @@ let maybeNumber: number | undefined; (function () { maybeNumber = 1; })(); +maybeNumber++; if (maybeNumber !== undefined) { maybeNumber++; } From 66e9f7dc214dc13c936e3cec2a27d3fef56777be Mon Sep 17 00:00:00 2001 From: Anders Hejlsberg Date: Sat, 28 May 2016 16:00:31 -0700 Subject: [PATCH 22/41] Optimize critical code paths in binder --- src/compiler/binder.ts | 213 +++++++++++++++++------------------------ 1 file changed, 87 insertions(+), 126 deletions(-) diff --git a/src/compiler/binder.ts b/src/compiler/binder.ts index 93417493fa7..4f24458f08d 100644 --- a/src/compiler/binder.ts +++ b/src/compiler/binder.ts @@ -77,12 +77,14 @@ namespace ts { // Blocks (when not parented by functions), Catch clauses, For/For-in/For-of statements... IsBlockScopedContainer = 1 << 1, - HasLocals = 1 << 2, + // The current node is the container of a control flow path. The current control flow should + // be saved and restored, and a new control flow initialized within the container. + IsControlFlowContainer = 1 << 2, - // If the current node is a container that also container that also contains locals. Examples: - // - // Functions, Methods, Modules, Source-files. - IsContainerWithLocals = IsContainer | HasLocals + IsFunctionLike = 1 << 3, + IsFunctionExpression = 1 << 4, + HasLocals = 1 << 5, + IsInterface = 1 << 6, } const binder = createBinder(); @@ -103,7 +105,7 @@ namespace ts { let lastContainer: Node; let seenThisKeyword: boolean; - // state used by reachability checks + // state used by control flow analysis let currentFlow: FlowNode; let currentBreakTarget: FlowLabel; let currentContinueTarget: FlowLabel; @@ -115,11 +117,7 @@ namespace ts { let hasExplicitReturn: boolean; // state used for emit helpers - let hasClassExtends: boolean; - let hasAsyncFunctions: boolean; - let hasDecorators: boolean; - let hasParameterDecorators: boolean; - let hasJsxSpreadAttribute: boolean; + let emitFlags: NodeFlags; // If this file is an external module, then it is automatically in strict-mode according to // ES6. If it is not an external module, then we'll determine if it is in strict mode or @@ -165,11 +163,7 @@ namespace ts { currentFalseTarget = undefined; activeLabels = undefined; hasExplicitReturn = false; - hasClassExtends = false; - hasAsyncFunctions = false; - hasDecorators = false; - hasParameterDecorators = false; - hasJsxSpreadAttribute = false; + emitFlags = NodeFlags.None; } return bindSourceFile; @@ -402,17 +396,12 @@ namespace ts { // All container nodes are kept on a linked list in declaration order. This list is used by // the getLocalNameOfContainer function in the type checker to validate that the local name // used for a container is unique. - function bindChildren(node: Node) { + function bindContainer(node: Node, containerFlags: ContainerFlags) { // Before we recurse into a node's children, we first save the existing parent, container // and block-container. Then after we pop out of processing the children, we restore // these saved values. - const saveParent = parent; const saveContainer = container; const savedBlockScopeContainer = blockScopeContainer; - - // This node will now be set as the parent of all of its children as we recurse into them. - parent = node; - // Depending on what kind of node this is, we may have to adjust the current container // and block-container. If the current node is a container, then it is automatically // considered the current block-container as well. Also, for containers that we know @@ -430,55 +419,33 @@ namespace ts { // reusing a node from a previous compilation, that node may have had 'locals' created // for it. We must clear this so we don't accidentally move any stale data forward from // a previous compilation. - const containerFlags = getContainerFlags(node); if (containerFlags & ContainerFlags.IsContainer) { container = blockScopeContainer = node; - if (containerFlags & ContainerFlags.HasLocals) { container.locals = {}; } - addToContainerChain(container); } else if (containerFlags & ContainerFlags.IsBlockScopedContainer) { blockScopeContainer = node; blockScopeContainer.locals = undefined; } - - let saveCurrentFlow: FlowNode; - let saveBreakTarget: FlowLabel; - let saveContinueTarget: FlowLabel; - let saveReturnTarget: FlowLabel; - let saveActiveLabels: ActiveLabel[]; - let saveHasExplicitReturn: boolean; - let isIIFE: boolean; - - const kind = node.kind; - let flags = node.flags; - - // Reset all reachability check related flags on node (for incremental scenarios) - // Reset all emit helper flags on node (for incremental scenarios) - flags &= ~NodeFlags.ReachabilityAndEmitFlags; - - if (kind === SyntaxKind.InterfaceDeclaration) { - seenThisKeyword = false; - } - - const saveState = kind === SyntaxKind.SourceFile || kind === SyntaxKind.ModuleBlock || isFunctionLikeKind(kind); - if (saveState) { - saveCurrentFlow = currentFlow; - saveBreakTarget = currentBreakTarget; - saveContinueTarget = currentContinueTarget; - saveReturnTarget = currentReturnTarget; - saveActiveLabels = activeLabels; - saveHasExplicitReturn = hasExplicitReturn; - isIIFE = (kind === SyntaxKind.FunctionExpression || kind === SyntaxKind.ArrowFunction) && !!getImmediatelyInvokedFunctionExpression(node); + if (containerFlags & ContainerFlags.IsControlFlowContainer) { + const saveCurrentFlow = currentFlow; + const saveBreakTarget = currentBreakTarget; + const saveContinueTarget = currentContinueTarget; + const saveReturnTarget = currentReturnTarget; + const saveActiveLabels = activeLabels; + const saveHasExplicitReturn = hasExplicitReturn; + const isIIFE = containerFlags & ContainerFlags.IsFunctionExpression && !!getImmediatelyInvokedFunctionExpression(node); + // An IIFE is considered part of the containing control flow. Return statements behave + // similarly to break statements that exit to a label just past the statement body. if (isIIFE) { currentReturnTarget = createBranchLabel(); } else { currentFlow = { flags: FlowFlags.Start }; - if (kind === SyntaxKind.FunctionExpression || kind === SyntaxKind.ArrowFunction) { + if (containerFlags & ContainerFlags.IsFunctionExpression) { (currentFlow).container = node; } currentReturnTarget = undefined; @@ -487,46 +454,17 @@ namespace ts { currentContinueTarget = undefined; activeLabels = undefined; hasExplicitReturn = false; - } - - if (node.flags & NodeFlags.JavaScriptFile && node.jsDocComment) { - bind(node.jsDocComment); - } - - bindReachableStatement(node); - - if (!(currentFlow.flags & FlowFlags.Unreachable) && isFunctionLikeKind(kind) && nodeIsPresent((node).body)) { - flags |= NodeFlags.HasImplicitReturn; - if (hasExplicitReturn) { - flags |= NodeFlags.HasExplicitReturn; + bindChildren(node); + // Reset all reachability check related flags on node (for incremental scenarios) + // Reset all emit helper flags on node (for incremental scenarios) + node.flags &= ~NodeFlags.ReachabilityAndEmitFlags; + if (!(currentFlow.flags & FlowFlags.Unreachable) && containerFlags & ContainerFlags.IsFunctionLike && nodeIsPresent((node).body)) { + node.flags |= NodeFlags.HasImplicitReturn; + if (hasExplicitReturn) node.flags |= NodeFlags.HasExplicitReturn; } - } - - if (kind === SyntaxKind.InterfaceDeclaration) { - flags = seenThisKeyword ? flags | NodeFlags.ContainsThis : flags & ~NodeFlags.ContainsThis; - } - - if (kind === SyntaxKind.SourceFile) { - if (hasClassExtends) { - flags |= NodeFlags.HasClassExtends; + if (node.kind === SyntaxKind.SourceFile) { + node.flags |= emitFlags; } - if (hasDecorators) { - flags |= NodeFlags.HasDecorators; - } - if (hasParameterDecorators) { - flags |= NodeFlags.HasParamDecorators; - } - if (hasAsyncFunctions) { - flags |= NodeFlags.HasAsyncFunctions; - } - if (hasJsxSpreadAttribute) { - flags |= NodeFlags.HasJsxSpreadAttribute; - } - } - - node.flags = flags; - - if (saveState) { if (isIIFE) { addAntecedent(currentReturnTarget, currentFlow); currentFlow = finishFlowLabel(currentReturnTarget); @@ -540,22 +478,26 @@ namespace ts { activeLabels = saveActiveLabels; hasExplicitReturn = saveHasExplicitReturn; } - + else if (containerFlags & ContainerFlags.IsInterface) { + seenThisKeyword = false; + bindChildren(node); + node.flags = seenThisKeyword ? node.flags | NodeFlags.ContainsThis : node.flags & ~NodeFlags.ContainsThis; + } + else { + bindChildren(node); + } container = saveContainer; - parent = saveParent; blockScopeContainer = savedBlockScopeContainer; } - /** - * Returns true if node and its subnodes were successfully traversed. - * Returning false means that node was not examined and caller needs to dive into the node himself. - */ - function bindReachableStatement(node: Node): void { + function bindChildren(node: Node): void { + if (node.flags & NodeFlags.JavaScriptFile && node.jsDocComment) { + bind(node.jsDocComment); + } if (checkUnreachable(node)) { forEachChild(node, bind); return; } - switch (node.kind) { case SyntaxKind.WhileStatement: bindWhileStatement(node); @@ -1145,31 +1087,42 @@ namespace ts { switch (node.kind) { case SyntaxKind.ClassExpression: case SyntaxKind.ClassDeclaration: - case SyntaxKind.InterfaceDeclaration: case SyntaxKind.EnumDeclaration: case SyntaxKind.ObjectLiteralExpression: case SyntaxKind.TypeLiteral: case SyntaxKind.JSDocRecordType: return ContainerFlags.IsContainer; + case SyntaxKind.InterfaceDeclaration: + return ContainerFlags.IsContainer | ContainerFlags.IsInterface; + + case SyntaxKind.JSDocFunctionType: + case SyntaxKind.ModuleDeclaration: + case SyntaxKind.TypeAliasDeclaration: + return ContainerFlags.IsContainer | ContainerFlags.HasLocals; + + case SyntaxKind.SourceFile: + return ContainerFlags.IsContainer | ContainerFlags.IsControlFlowContainer | ContainerFlags.HasLocals; + + case SyntaxKind.Constructor: + case SyntaxKind.FunctionDeclaration: + case SyntaxKind.MethodDeclaration: + case SyntaxKind.MethodSignature: + case SyntaxKind.GetAccessor: + case SyntaxKind.SetAccessor: case SyntaxKind.CallSignature: case SyntaxKind.ConstructSignature: case SyntaxKind.IndexSignature: - case SyntaxKind.MethodDeclaration: - case SyntaxKind.MethodSignature: - case SyntaxKind.FunctionDeclaration: - case SyntaxKind.Constructor: - case SyntaxKind.GetAccessor: - case SyntaxKind.SetAccessor: case SyntaxKind.FunctionType: - case SyntaxKind.JSDocFunctionType: case SyntaxKind.ConstructorType: + return ContainerFlags.IsContainer | ContainerFlags.IsControlFlowContainer | ContainerFlags.HasLocals | ContainerFlags.IsFunctionLike; + case SyntaxKind.FunctionExpression: case SyntaxKind.ArrowFunction: - case SyntaxKind.ModuleDeclaration: - case SyntaxKind.SourceFile: - case SyntaxKind.TypeAliasDeclaration: - return ContainerFlags.IsContainerWithLocals; + return ContainerFlags.IsContainer | ContainerFlags.IsControlFlowContainer | ContainerFlags.HasLocals | ContainerFlags.IsFunctionLike | ContainerFlags.IsFunctionExpression; + + case SyntaxKind.ModuleBlock: + return ContainerFlags.IsControlFlowContainer; case SyntaxKind.CatchClause: case SyntaxKind.ForStatement: @@ -1606,8 +1559,8 @@ namespace ts { node.parent = parent; - const savedInStrictMode = inStrictMode; - if (!savedInStrictMode) { + const saveInStrictMode = inStrictMode; + if (!saveInStrictMode) { updateStrictMode(node); } @@ -1627,9 +1580,18 @@ namespace ts { // symbols we do specialized work when we recurse. For example, we'll keep track of // the current 'container' node when it changes. This helps us know which symbol table // a local should go into for example. - bindChildren(node); + const saveParent = parent; + parent = node; + const containerFlags = getContainerFlags(node); + if (containerFlags === ContainerFlags.None) { + bindChildren(node); + } + else { + bindContainer(node, containerFlags); + } + parent = saveParent; - inStrictMode = savedInStrictMode; + inStrictMode = saveInStrictMode; } function updateStrictMode(node: Node) { @@ -1746,7 +1708,7 @@ namespace ts { return bindPropertyOrMethodOrAccessor(node, SymbolFlags.EnumMember, SymbolFlags.EnumMemberExcludes); case SyntaxKind.JsxSpreadAttribute: - hasJsxSpreadAttribute = true; + emitFlags |= NodeFlags.HasJsxSpreadAttribute; return; case SyntaxKind.CallSignature: @@ -1970,10 +1932,10 @@ namespace ts { function bindClassLikeDeclaration(node: ClassLikeDeclaration) { if (!isDeclarationFile(file) && !isInAmbientContext(node)) { if (getClassExtendsHeritageClauseElement(node) !== undefined) { - hasClassExtends = true; + emitFlags |= NodeFlags.HasClassExtends; } if (nodeIsDecorated(node)) { - hasDecorators = true; + emitFlags |= NodeFlags.HasDecorators; } } @@ -2049,8 +2011,7 @@ namespace ts { if (!isDeclarationFile(file) && !isInAmbientContext(node) && nodeIsDecorated(node)) { - hasDecorators = true; - hasParameterDecorators = true; + emitFlags |= (NodeFlags.HasDecorators | NodeFlags.HasParamDecorators); } if (inStrictMode) { @@ -2077,7 +2038,7 @@ namespace ts { function bindFunctionDeclaration(node: FunctionDeclaration) { if (!isDeclarationFile(file) && !isInAmbientContext(node)) { if (isAsyncFunctionLike(node)) { - hasAsyncFunctions = true; + emitFlags |= NodeFlags.HasAsyncFunctions; } } @@ -2094,7 +2055,7 @@ namespace ts { function bindFunctionExpression(node: FunctionExpression) { if (!isDeclarationFile(file) && !isInAmbientContext(node)) { if (isAsyncFunctionLike(node)) { - hasAsyncFunctions = true; + emitFlags |= NodeFlags.HasAsyncFunctions; } } if (currentFlow) { @@ -2108,10 +2069,10 @@ namespace ts { function bindPropertyOrMethodOrAccessor(node: Declaration, symbolFlags: SymbolFlags, symbolExcludes: SymbolFlags) { if (!isDeclarationFile(file) && !isInAmbientContext(node)) { if (isAsyncFunctionLike(node)) { - hasAsyncFunctions = true; + emitFlags |= NodeFlags.HasAsyncFunctions; } if (nodeIsDecorated(node)) { - hasDecorators = true; + emitFlags |= NodeFlags.HasDecorators; } } From 5b4dffc2d72f9c1b99fdf6fa3d2f090fc735066c Mon Sep 17 00:00:00 2001 From: Anders Hejlsberg Date: Sun, 29 May 2016 16:04:34 -0700 Subject: [PATCH 23/41] More critical path optimization in binder --- src/compiler/binder.ts | 86 ++++++++++++++++++------------------------ 1 file changed, 37 insertions(+), 49 deletions(-) diff --git a/src/compiler/binder.ts b/src/compiler/binder.ts index 4f24458f08d..af304e5f0a2 100644 --- a/src/compiler/binder.ts +++ b/src/compiler/binder.ts @@ -1556,15 +1556,9 @@ namespace ts { if (!node) { return; } - node.parent = parent; - const saveInStrictMode = inStrictMode; - if (!saveInStrictMode) { - updateStrictMode(node); - } - - // First we bind declaration nodes to a symbol if possible. We'll both create a symbol + // First we bind declaration nodes to a symbol if possible. We'll both create a symbol // and then potentially add the symbol to an appropriate symbol table. Possible // destination symbol tables are: // @@ -1572,56 +1566,40 @@ namespace ts { // 2) The 'members' table of the current container's symbol. // 3) The 'locals' table of the current container. // - // However, not all symbols will end up in any of these tables. 'Anonymous' symbols + // However, not all symbols will end up in any of these tables. 'Anonymous' symbols // (like TypeLiterals for example) will not be put in any table. bindWorker(node); - - // Then we recurse into the children of the node to bind them as well. For certain - // symbols we do specialized work when we recurse. For example, we'll keep track of - // the current 'container' node when it changes. This helps us know which symbol table - // a local should go into for example. - const saveParent = parent; - parent = node; - const containerFlags = getContainerFlags(node); - if (containerFlags === ContainerFlags.None) { - bindChildren(node); + // Then we recurse into the children of the node to bind them as well. For certain + // symbols we do specialized work when we recurse. For example, we'll keep track of + // the current 'container' node when it changes. This helps us know which symbol table + // a local should go into for example. Since terminal nodes are known not to have + // children, as an optimization we don't process those. + if (node.kind > SyntaxKind.LastToken) { + const saveParent = parent; + parent = node; + const containerFlags = getContainerFlags(node); + if (containerFlags === ContainerFlags.None) { + bindChildren(node); + } + else { + bindContainer(node, containerFlags); + } + parent = saveParent; } - else { - bindContainer(node, containerFlags); - } - parent = saveParent; - inStrictMode = saveInStrictMode; } - function updateStrictMode(node: Node) { - switch (node.kind) { - case SyntaxKind.SourceFile: - case SyntaxKind.ModuleBlock: - updateStrictModeStatementList((node).statements); - return; - case SyntaxKind.Block: - if (isFunctionLike(node.parent)) { - updateStrictModeStatementList((node).statements); - } - return; - case SyntaxKind.ClassDeclaration: - case SyntaxKind.ClassExpression: - // All classes are automatically in strict mode in ES6. - inStrictMode = true; - return; - } - } - function updateStrictModeStatementList(statements: NodeArray) { - for (const statement of statements) { - if (!isPrologueDirective(statement)) { - return; - } + if (!inStrictMode) { + for (const statement of statements) { + if (!isPrologueDirective(statement)) { + return; + } - if (isUseStrictPrologueDirective(statement)) { - inStrictMode = true; - return; + if (isUseStrictPrologueDirective(statement)) { + inStrictMode = true; + return; + } } } } @@ -1753,6 +1731,8 @@ namespace ts { // Members of classes, interfaces, and modules case SyntaxKind.ClassExpression: case SyntaxKind.ClassDeclaration: + // All classes are automatically in strict mode in ES6. + inStrictMode = true; return bindClassLikeDeclaration(node); case SyntaxKind.InterfaceDeclaration: return bindBlockScopedDeclaration(node, SymbolFlags.Interface, SymbolFlags.InterfaceExcludes); @@ -1778,7 +1758,15 @@ namespace ts { case SyntaxKind.ExportAssignment: return bindExportAssignment(node); case SyntaxKind.SourceFile: + updateStrictModeStatementList((node).statements); return bindSourceFileIfExternalModule(); + case SyntaxKind.Block: + if (!isFunctionLike(node.parent)) { + return; + } + // Fall through + case SyntaxKind.ModuleBlock: + return updateStrictModeStatementList((node).statements); } } From 18ee4b0a1ee1e326b5bbcdc0104466ac9c3d13d2 Mon Sep 17 00:00:00 2001 From: zhengbli Date: Mon, 30 May 2016 22:11:43 -0700 Subject: [PATCH 24/41] cr feedback --- src/compiler/binder.ts | 12 ++++++++++++ src/compiler/checker.ts | 16 +++++++++++++--- src/compiler/parser.ts | 29 ++++++----------------------- src/compiler/types.ts | 8 +++++--- src/services/navigationBar.ts | 6 ++++++ src/services/services.ts | 11 +---------- 6 files changed, 43 insertions(+), 39 deletions(-) diff --git a/src/compiler/binder.ts b/src/compiler/binder.ts index 756be48ed94..3d3a86f20fd 100644 --- a/src/compiler/binder.ts +++ b/src/compiler/binder.ts @@ -267,6 +267,18 @@ namespace ts { let functionType = node.parent; let index = indexOf(functionType.parameters, node); return "p" + index; + case SyntaxKind.JSDocTypedefTag: + const parentNode = node.parent && node.parent.parent; + let nameFromParentNode: string; + if (parentNode && parentNode.kind === SyntaxKind.VariableStatement) { + if ((parentNode).declarationList.declarations.length > 0) { + const nameIdentifier = (parentNode).declarationList.declarations[0].name; + if (nameIdentifier.kind === SyntaxKind.Identifier) { + nameFromParentNode = (nameIdentifier).text; + } + } + } + return nameFromParentNode; } } diff --git a/src/compiler/checker.ts b/src/compiler/checker.ts index 42af34c0659..e8b23822593 100644 --- a/src/compiler/checker.ts +++ b/src/compiler/checker.ts @@ -3586,11 +3586,21 @@ namespace ts { return unknownType; } + let type: Type; let declaration: JSDocTypedefTag | TypeAliasDeclaration = getDeclarationOfKind(symbol, SyntaxKind.JSDocTypedefTag); - if (!declaration) { - declaration = getDeclarationOfKind(symbol, SyntaxKind.TypeAliasDeclaration); + if (declaration) { + if (declaration.jsDocTypeLiteral) { + type = getTypeFromTypeNode(declaration.jsDocTypeLiteral); + } + else { + type = getTypeFromTypeNode(declaration.typeExpression.type); + } } - let type = getTypeFromTypeNode(declaration.type); + else { + declaration = getDeclarationOfKind(symbol, SyntaxKind.TypeAliasDeclaration); + type = getTypeFromTypeNode(declaration.type); + } + if (popTypeResolution()) { links.typeParameters = getLocalTypeParametersOfClassOrInterfaceOrTypeAlias(symbol); if (links.typeParameters) { diff --git a/src/compiler/parser.ts b/src/compiler/parser.ts index 07509d0afcc..c61b4b836ea 100644 --- a/src/compiler/parser.ts +++ b/src/compiler/parser.ts @@ -404,7 +404,7 @@ namespace ts { case SyntaxKind.JSDocTypedefTag: return visitNode(cbNode, (node).typeExpression) || visitNode(cbNode, (node).name) || - visitNode(cbNode, (node).type); + visitNode(cbNode, (node).jsDocTypeLiteral); case SyntaxKind.JSDocTypeLiteral: return visitNodes(cbNodes, (node).jsDocPropertyTags); case SyntaxKind.JSDocPropertyTag: @@ -6301,28 +6301,11 @@ namespace ts { function handleTypedefTag(atToken: Node, tagName: Identifier): JSDocTypedefTag { const typeExpression = tryParseTypeExpression(); skipWhitespace(); - let name = parseJSDocIdentifierName(); - if (!name) { - let foundNameFromParentNode = false; - if (parentNode && parentNode.kind === SyntaxKind.VariableStatement) { - if ((parentNode).declarationList.declarations.length > 0) { - const nameFromParentNode = (parentNode).declarationList.declarations[0].name; - if (nameFromParentNode.kind === SyntaxKind.Identifier) { - foundNameFromParentNode = true; - name = nameFromParentNode; - } - } - } - if (!foundNameFromParentNode) { - parseErrorAtPosition(scanner.getStartPos(), 0, Diagnostics.Identifier_expected); - return undefined; - } - } const typedefTag = createNode(SyntaxKind.JSDocTypedefTag, atToken.pos); typedefTag.atToken = atToken; typedefTag.tagName = tagName; - typedefTag.name = name; + typedefTag.name = parseJSDocIdentifierName(); typedefTag.typeExpression = typeExpression; if (typeExpression) { @@ -6331,16 +6314,16 @@ namespace ts { if (jsDocTypeReference.name.kind === SyntaxKind.Identifier) { const name = jsDocTypeReference.name; if (name.text === "Object") { - typedefTag.type = scanChildTags(); + typedefTag.jsDocTypeLiteral = scanChildTags(); } } } - if (!typedefTag.type) { - typedefTag.type = typeExpression.type; + if (!typedefTag.jsDocTypeLiteral) { + typedefTag.jsDocTypeLiteral = typeExpression.type; } } else { - typedefTag.type = scanChildTags(); + typedefTag.jsDocTypeLiteral = scanChildTags(); } return finishNode(typedefTag); diff --git a/src/compiler/types.ts b/src/compiler/types.ts index 1b9af02a8d5..f23b52b35d7 100644 --- a/src/compiler/types.ts +++ b/src/compiler/types.ts @@ -376,7 +376,9 @@ namespace ts { LastBinaryOperator = CaretEqualsToken, FirstNode = QualifiedName, FirstJSDocNode = JSDocTypeExpression, - LastJSDocNode = JSDocTypeLiteral + LastJSDocNode = JSDocTypeLiteral, + FirstJSDocTagNode = JSDocComment, + LastJSDocTagNode = JSDocTypeLiteral } export const enum NodeFlags { @@ -1518,9 +1520,9 @@ namespace ts { // @kind(SyntaxKind.JSDocTypedefTag) export interface JSDocTypedefTag extends JSDocTag, Declaration { - name: Identifier; + name?: Identifier; typeExpression?: JSDocTypeExpression; - type: JSDocType; + jsDocTypeLiteral?: JSDocTypeLiteral; } // @kind(SyntaxKind.JSDocPropertyTag) diff --git a/src/services/navigationBar.ts b/src/services/navigationBar.ts index 0cb431de668..5515356a259 100644 --- a/src/services/navigationBar.ts +++ b/src/services/navigationBar.ts @@ -653,6 +653,12 @@ namespace ts.NavigationBar { topItem.childItems.push(newItem); } + if (node.jsDocComments && node.jsDocComments.length > 0) { + for (const jsDocComment of node.jsDocComments) { + visitNode(jsDocComment); + } + } + // Add a level if traversing into a container if (newItem && (isFunctionLike(node) || isClassLike(node))) { const lastTop = topItem; diff --git a/src/services/services.ts b/src/services/services.ts index 8fef753f2e8..90740b257c6 100644 --- a/src/services/services.ts +++ b/src/services/services.ts @@ -276,16 +276,7 @@ namespace ts { scanner.setText((sourceFile || this.getSourceFile()).text); children = []; let pos = this.pos; - const useJSDocScanner = - this.kind === SyntaxKind.JSDocComment || - this.kind === SyntaxKind.JSDocParameterTag || - this.kind === SyntaxKind.JSDocTag || - this.kind === SyntaxKind.JSDocParameterTag || - this.kind === SyntaxKind.JSDocReturnTag || - this.kind === SyntaxKind.JSDocTypeTag || - this.kind === SyntaxKind.JSDocTemplateTag || - this.kind === SyntaxKind.JSDocTypedefTag || - this.kind === SyntaxKind.JSDocPropertyTag; + const useJSDocScanner = this.kind >= SyntaxKind.FirstJSDocTagNode && this.kind <= SyntaxKind.LastJSDocTagNode; const processNode = (node: Node) => { if (pos < node.pos) { pos = this.addSyntheticNodes(children, pos, node.pos, useJSDocScanner); From 59b188d4cae7c9d0c4d39a6ec839506c1170a519 Mon Sep 17 00:00:00 2001 From: zhengbli Date: Tue, 31 May 2016 02:05:26 -0700 Subject: [PATCH 25/41] Add navigationTo test for jsdoc typedef --- src/compiler/parser.ts | 7 ++++++ src/harness/harness.ts | 5 +++- src/services/navigationBar.ts | 23 ++++++++++++++++++- .../server/jsdocTypedefTagNavigateTo.ts | 16 +++++++++++++ tests/cases/unittests/jsDocParsing.ts | 3 ++- 5 files changed, 51 insertions(+), 3 deletions(-) create mode 100644 tests/cases/fourslash/server/jsdocTypedefTagNavigateTo.ts diff --git a/src/compiler/parser.ts b/src/compiler/parser.ts index c61b4b836ea..20f3d927e71 100644 --- a/src/compiler/parser.ts +++ b/src/compiler/parser.ts @@ -672,6 +672,13 @@ namespace ts { const saveParent = parent; parent = n; forEachChild(n, visitNode); + if (n.jsDocComments) { + for (const jsDocComment of n.jsDocComments) { + jsDocComment.parent = n; + parent = jsDocComment; + forEachChild(jsDocComment, visitNode); + } + } parent = saveParent; } } diff --git a/src/harness/harness.ts b/src/harness/harness.ts index e1c90cee80c..5fe5b84ac91 100644 --- a/src/harness/harness.ts +++ b/src/harness/harness.ts @@ -223,7 +223,10 @@ namespace Utils { // For some markers in SyntaxKind, we should print its original syntax name instead of // the marker name in tests. - if (k === (ts).SyntaxKind.FirstJSDocNode || k === (ts).SyntaxKind.LastJSDocNode) { + if (k === (ts).SyntaxKind.FirstJSDocNode || + k === (ts).SyntaxKind.LastJSDocNode || + k === (ts).SyntaxKind.FirstJSDocTagNode || + k === (ts).SyntaxKind.LastJSDocTagNode) { for (const kindName in (ts).SyntaxKind) { if ((ts).SyntaxKind[kindName] === k) { return kindName; diff --git a/src/services/navigationBar.ts b/src/services/navigationBar.ts index 5515356a259..3adc5563d71 100644 --- a/src/services/navigationBar.ts +++ b/src/services/navigationBar.ts @@ -738,6 +738,27 @@ namespace ts.NavigationBar { } const declName = declarationNameToString(decl.name); return getNavBarItem(declName, ScriptElementKind.constElement, [getNodeSpan(node)]); + case SyntaxKind.JSDocTypedefTag: + if ((node).name) { + return getNavBarItem( + (node).name.text, + ScriptElementKind.typeElement, + [getNodeSpan(node)]); + } + else { + const parentNode = node.parent && node.parent.parent; + if (parentNode && parentNode.kind === SyntaxKind.VariableStatement) { + if ((parentNode).declarationList.declarations.length > 0) { + const nameIdentifier = (parentNode).declarationList.declarations[0].name; + if (nameIdentifier.kind === SyntaxKind.Identifier) { + return getNavBarItem( + (nameIdentifier).text, + ScriptElementKind.typeElement, + [getNodeSpan(node)]); + } + } + } + } default: return undefined; } @@ -808,7 +829,7 @@ namespace ts.NavigationBar { } function getNodeSpan(node: Node) { - return node.kind === SyntaxKind.SourceFile + return node.kind === SyntaxKind.SourceFile ? createTextSpanFromBounds(node.getFullStart(), node.getEnd()) : createTextSpanFromBounds(node.getStart(), node.getEnd()); } diff --git a/tests/cases/fourslash/server/jsdocTypedefTagNavigateTo.ts b/tests/cases/fourslash/server/jsdocTypedefTagNavigateTo.ts new file mode 100644 index 00000000000..1d1c1ac27ca --- /dev/null +++ b/tests/cases/fourslash/server/jsdocTypedefTagNavigateTo.ts @@ -0,0 +1,16 @@ +/// + +// @allowNonTsExtensions: true +// @Filename: jsDocTypedef_form2.js +//// +//// /** @typedef {(string | number)} NumberLike */ +//// /** @typedef {(string | number | string[])} */ +//// var NumberLike2; +//// +//// /** @type {/*1*/NumberLike} */ +//// var numberLike; + +verify.navigationBarContains("NumberLike", "type"); +verify.navigationBarContains("NumberLike2", "type"); +verify.navigationBarContains("NumberLike2", "var"); +verify.navigationBarContains("numberLike", "var"); \ No newline at end of file diff --git a/tests/cases/unittests/jsDocParsing.ts b/tests/cases/unittests/jsDocParsing.ts index 8afad08909c..fb75b4d4940 100644 --- a/tests/cases/unittests/jsDocParsing.ts +++ b/tests/cases/unittests/jsDocParsing.ts @@ -1004,7 +1004,8 @@ namespace ts { if (result !== expected) { // Turn on a human-readable diff if (typeof require !== "undefined") { - require("chai").config.showDiff = true; + const chai = require("chai"); + chai.config.showDiff = true; chai.expect(JSON.parse(result)).equal(JSON.parse(expected)); } else { From e93f9df95506a8cf3daa99b18a05331960c9f726 Mon Sep 17 00:00:00 2001 From: Zhengbo Li Date: Tue, 31 May 2016 10:48:25 -0700 Subject: [PATCH 26/41] Fix broken test --- .../server/jsdocTypedefTagNavigateTo.ts | 22 +++++++++++++++---- 1 file changed, 18 insertions(+), 4 deletions(-) diff --git a/tests/cases/fourslash/server/jsdocTypedefTagNavigateTo.ts b/tests/cases/fourslash/server/jsdocTypedefTagNavigateTo.ts index 1d1c1ac27ca..77cd75aa44c 100644 --- a/tests/cases/fourslash/server/jsdocTypedefTagNavigateTo.ts +++ b/tests/cases/fourslash/server/jsdocTypedefTagNavigateTo.ts @@ -10,7 +10,21 @@ //// /** @type {/*1*/NumberLike} */ //// var numberLike; -verify.navigationBarContains("NumberLike", "type"); -verify.navigationBarContains("NumberLike2", "type"); -verify.navigationBarContains("NumberLike2", "var"); -verify.navigationBarContains("numberLike", "var"); \ No newline at end of file +verify.navigationBar([ + { + "text": "NumberLike", + "kind": "type" + }, + { + "text": "NumberLike2", + "kind": "type" + }, + { + "text": "NumberLike2", + "kind": "var" + }, + { + "text": "numberLike", + "kind": "var" + } +]); \ No newline at end of file From 3433a7800a01e045486e48bc06fffc551295086f Mon Sep 17 00:00:00 2001 From: zhengbli Date: Tue, 31 May 2016 12:35:12 -0700 Subject: [PATCH 27/41] Fix formatOnEnter for double newlines --- src/harness/fourslash.ts | 10 ++++++++++ src/services/formatting/formatting.ts | 6 ++++++ tests/cases/fourslash/formatOnEnter.ts | 17 +++++++++++++++++ tests/cases/fourslash/fourslash.ts | 1 + 4 files changed, 34 insertions(+) create mode 100644 tests/cases/fourslash/formatOnEnter.ts diff --git a/src/harness/fourslash.ts b/src/harness/fourslash.ts index 4a756ab3b1e..bd556c659c7 100644 --- a/src/harness/fourslash.ts +++ b/src/harness/fourslash.ts @@ -1486,6 +1486,12 @@ namespace FourSlash { this.fixCaretPosition(); } + public formatOnType(pos: number, key: string) { + const edits = this.languageService.getFormattingEditsAfterKeystroke(this.activeFile.fileName, pos, key, this.formatCodeOptions); + this.currentCaretPosition += this.applyEdits(this.activeFile.fileName, edits, /*isFormattingEdit*/ true); + this.fixCaretPosition(); + } + private updateMarkersForEdit(fileName: string, minChar: number, limChar: number, text: string) { for (let i = 0; i < this.testData.markers.length; i++) { const marker = this.testData.markers[i]; @@ -3223,6 +3229,10 @@ namespace FourSlashInterface { this.state.formatSelection(this.state.getMarkerByName(startMarker).position, this.state.getMarkerByName(endMarker).position); } + public onType(posMarker: string, key: string) { + this.state.formatOnType(this.state.getMarkerByName(posMarker).position, key); + } + public setOption(name: string, value: number): void; public setOption(name: string, value: string): void; public setOption(name: string, value: boolean): void; diff --git a/src/services/formatting/formatting.ts b/src/services/formatting/formatting.ts index 2f5f10752f4..7127eb98006 100644 --- a/src/services/formatting/formatting.ts +++ b/src/services/formatting/formatting.ts @@ -81,6 +81,12 @@ namespace ts.formatting { while (isWhiteSpace(sourceFile.text.charCodeAt(endOfFormatSpan)) && !isLineBreak(sourceFile.text.charCodeAt(endOfFormatSpan))) { endOfFormatSpan--; } + // if the character at the end of the span is a line break, we shouldn't include it, because it indicates we don't want to + // touch the current line at all. Also, on some OSes the line break consists of two characters (\r\n), we should test if the + // previous character before the end of format span is line break character as well. + while (isLineBreak(sourceFile.text.charCodeAt(endOfFormatSpan))) { + endOfFormatSpan--; + } const span = { // get start position for the previous line pos: getStartPositionOfLine(line - 1, sourceFile), diff --git a/tests/cases/fourslash/formatOnEnter.ts b/tests/cases/fourslash/formatOnEnter.ts new file mode 100644 index 00000000000..f505cf6cec7 --- /dev/null +++ b/tests/cases/fourslash/formatOnEnter.ts @@ -0,0 +1,17 @@ +/// + +/////*3*/function listAPIFiles (path : string): string[] { +//// /*1*/ +//// /*2*/ +////} + +goTo.marker("1"); +format.onType("1", "\n"); +verify.currentLineContentIs(" "); + +goTo.marker("2"); +format.onType("2", "\n"); +verify.currentLineContentIs(" "); + +goTo.marker("3"); +verify.currentLineContentIs("function listAPIFiles(path: string): string[] {"); \ No newline at end of file diff --git a/tests/cases/fourslash/fourslash.ts b/tests/cases/fourslash/fourslash.ts index 8a0d2d36364..71e1c3cc5d7 100644 --- a/tests/cases/fourslash/fourslash.ts +++ b/tests/cases/fourslash/fourslash.ts @@ -246,6 +246,7 @@ declare namespace FourSlashInterface { copyFormatOptions(): FormatCodeOptions; setFormatOptions(options: FormatCodeOptions): any; selection(startMarker: string, endMarker: string): void; + onType(posMarker: string, key: string): void; setOption(name: string, value: number): any; setOption(name: string, value: string): any; setOption(name: string, value: boolean): any; From 58fdd011df74f226f1909a2155896e43fc5c2042 Mon Sep 17 00:00:00 2001 From: zhengbli Date: Tue, 31 May 2016 14:08:48 -0700 Subject: [PATCH 28/41] avoid eating all preceding empty lines --- src/services/formatting/formatting.ts | 2 +- tests/cases/fourslash/{ => server}/formatOnEnter.ts | 0 2 files changed, 1 insertion(+), 1 deletion(-) rename tests/cases/fourslash/{ => server}/formatOnEnter.ts (100%) diff --git a/src/services/formatting/formatting.ts b/src/services/formatting/formatting.ts index 7127eb98006..ef8fddcfb3a 100644 --- a/src/services/formatting/formatting.ts +++ b/src/services/formatting/formatting.ts @@ -84,7 +84,7 @@ namespace ts.formatting { // if the character at the end of the span is a line break, we shouldn't include it, because it indicates we don't want to // touch the current line at all. Also, on some OSes the line break consists of two characters (\r\n), we should test if the // previous character before the end of format span is line break character as well. - while (isLineBreak(sourceFile.text.charCodeAt(endOfFormatSpan))) { + if (isLineBreak(sourceFile.text.charCodeAt(endOfFormatSpan))) { endOfFormatSpan--; } const span = { diff --git a/tests/cases/fourslash/formatOnEnter.ts b/tests/cases/fourslash/server/formatOnEnter.ts similarity index 100% rename from tests/cases/fourslash/formatOnEnter.ts rename to tests/cases/fourslash/server/formatOnEnter.ts From 41446fe4c27820e5668e6a37ba7a2934d6dfa7ad Mon Sep 17 00:00:00 2001 From: Anders Hejlsberg Date: Tue, 31 May 2016 14:33:00 -0700 Subject: [PATCH 29/41] Address CR feedback --- src/compiler/checker.ts | 32 +++++++++++++++----------------- 1 file changed, 15 insertions(+), 17 deletions(-) diff --git a/src/compiler/checker.ts b/src/compiler/checker.ts index 037ab78413a..caaabdc96cd 100644 --- a/src/compiler/checker.ts +++ b/src/compiler/checker.ts @@ -8643,25 +8643,23 @@ namespace ts { function getContextuallyTypedParameterType(parameter: ParameterDeclaration): Type { const func = parameter.parent; if (isContextSensitiveFunctionOrObjectLiteralMethod(func)) { - if (func.kind === SyntaxKind.FunctionExpression || func.kind === SyntaxKind.ArrowFunction) { - const iife = getImmediatelyInvokedFunctionExpression(func); - if (iife) { - const indexOfParameter = indexOf(func.parameters, parameter); - if (iife.arguments && indexOfParameter < iife.arguments.length) { - if (parameter.dotDotDotToken) { - const restTypes: Type[] = []; - for (let i = indexOfParameter; i < iife.arguments.length; i++) { - restTypes.push(getTypeOfExpression(iife.arguments[i])); - } - return createArrayType(getUnionType(restTypes)); + const iife = getImmediatelyInvokedFunctionExpression(func); + if (iife) { + const indexOfParameter = indexOf(func.parameters, parameter); + if (iife.arguments && indexOfParameter < iife.arguments.length) { + if (parameter.dotDotDotToken) { + const restTypes: Type[] = []; + for (let i = indexOfParameter; i < iife.arguments.length; i++) { + restTypes.push(getTypeOfExpression(iife.arguments[i])); } - const links = getNodeLinks(iife); - const cached = links.resolvedSignature; - links.resolvedSignature = anySignature; - const type = checkExpression(iife.arguments[indexOfParameter]); - links.resolvedSignature = cached; - return type; + return createArrayType(getUnionType(restTypes)); } + const links = getNodeLinks(iife); + const cached = links.resolvedSignature; + links.resolvedSignature = anySignature; + const type = checkExpression(iife.arguments[indexOfParameter]); + links.resolvedSignature = cached; + return type; } } const contextualSignature = getContextualSignature(func); From eb0f035c78219ed920052094351326c5b35e1fe3 Mon Sep 17 00:00:00 2001 From: zhengbli Date: Tue, 31 May 2016 16:08:12 -0700 Subject: [PATCH 30/41] Remove unused parameter --- src/compiler/parser.ts | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/src/compiler/parser.ts b/src/compiler/parser.ts index 20f3d927e71..6f042627c6b 100644 --- a/src/compiler/parser.ts +++ b/src/compiler/parser.ts @@ -2725,7 +2725,7 @@ namespace ts { // 1) async[no LineTerminator here]AsyncArrowBindingIdentifier[?Yield][no LineTerminator here]=>AsyncConciseBody[?In] // 2) CoverCallExpressionAndAsyncArrowHead[?Yield, ?Await][no LineTerminator here]=>AsyncConciseBody[?In] // Production (1) of AsyncArrowFunctionExpression is parsed in "tryParseAsyncSimpleArrowFunctionExpression". - // And production (2) is parsed in "tryParseParenthesizedArrowFunctionExpression". + // And production (2) is parsed in "tryParseParenthesizedArrowFunctionExpression". // // If we do successfully parse arrow-function, we must *not* recurse for productions 1, 2 or 3. An ArrowFunction is // not a LeftHandSideExpression, nor does it start a ConditionalExpression. So we are done @@ -6050,7 +6050,7 @@ namespace ts { const saveParseDiagnosticsLength = parseDiagnostics.length; const saveParseErrorBeforeNextFinishedNode = parseErrorBeforeNextFinishedNode; - const comment = parseJSDocCommentWorker(start, length, parent); + const comment = parseJSDocCommentWorker(start, length); if (comment) { comment.parent = parent; } @@ -6062,7 +6062,7 @@ namespace ts { return comment; } - export function parseJSDocCommentWorker(start: number, length: number, parentNode?: Node): JSDocComment { + export function parseJSDocCommentWorker(start: number, length: number): JSDocComment { const content = sourceText; start = start || 0; const end = length === undefined ? content.length : start + length; From 9a620bf616e1f6fac6d035ce43ad690455fa84a6 Mon Sep 17 00:00:00 2001 From: Nathan Shively-Sanders Date: Wed, 1 Jun 2016 11:39:22 -0700 Subject: [PATCH 31/41] Actually merge from master --- src/compiler/checker.ts | 11 ----------- 1 file changed, 11 deletions(-) diff --git a/src/compiler/checker.ts b/src/compiler/checker.ts index 52e70941408..ef11dc904ea 100644 --- a/src/compiler/checker.ts +++ b/src/compiler/checker.ts @@ -8156,23 +8156,12 @@ namespace ts { return type; } const declaration = localOrExportSymbol.valueDeclaration; -<<<<<<< HEAD - const defaultsToDeclaredType = !strictNullChecks || type.flags & TypeFlags.Any || !declaration || - declaration.kind === SyntaxKind.Parameter || isInAmbientContext(declaration) || - getContainingFunctionOrModule(declaration) !== getContainingFunctionOrModule(node); - if (defaultsToDeclaredType && type.flags & TypeFlags.NotNarrowable) { - return type; - } - const flowType = getFlowTypeOfReference(node, type, defaultsToDeclaredType ? type : undefinedType); - if (strictNullChecks && !(type.flags & TypeFlags.Any) && !(getNullableKind(type) & TypeFlags.Undefined) && getNullableKind(flowType) & TypeFlags.Undefined) { -======= const includeOuterFunctions = isReadonlySymbol(localOrExportSymbol); const assumeInitialized = !strictNullChecks || (type.flags & TypeFlags.Any) !== 0 || !declaration || getRootDeclaration(declaration).kind === SyntaxKind.Parameter || isInAmbientContext(declaration) || !isDeclarationIncludedInFlow(node, declaration, includeOuterFunctions); const flowType = getFlowTypeOfReference(node, type, assumeInitialized, includeOuterFunctions); if (!assumeInitialized && !(getNullableKind(type) & TypeFlags.Undefined) && getNullableKind(flowType) & TypeFlags.Undefined) { ->>>>>>> master error(node, Diagnostics.Variable_0_is_used_before_being_assigned, symbolToString(symbol)); // Return the declared type to reduce follow-on errors return type; From 0e96c5eaf134c5317febf417044a638b4ce6d70e Mon Sep 17 00:00:00 2001 From: zhengbli Date: Wed, 1 Jun 2016 22:57:25 -0700 Subject: [PATCH 32/41] Run fixupParentReferences when parsing isolated jsDocComment --- src/compiler/parser.ts | 21 +++++++++++++++++- ...tacticClassificationForJSDocTemplateTag.ts | 22 +++++++++++++++++++ 2 files changed, 42 insertions(+), 1 deletion(-) create mode 100644 tests/cases/fourslash/syntacticClassificationForJSDocTemplateTag.ts diff --git a/src/compiler/parser.ts b/src/compiler/parser.ts index 6f042627c6b..13f16f3848a 100644 --- a/src/compiler/parser.ts +++ b/src/compiler/parser.ts @@ -440,7 +440,26 @@ namespace ts { /* @internal */ export function parseIsolatedJSDocComment(content: string, start?: number, length?: number) { - return Parser.JSDocParser.parseIsolatedJSDocComment(content, start, length); + const result = Parser.JSDocParser.parseIsolatedJSDocComment(content, start, length); + if (result.jsDocComment) { + // because the jsDocComment was parsed out of the source file, it might + // not be covered by the fixupParentReferences. + let parentNode: Node = result.jsDocComment; + forEachChild(result.jsDocComment, visitNode); + + function visitNode(n: Node): void { + if (n.parent !== parentNode) { + n.parent = parentNode; + + const saveParent = parentNode; + parentNode = n; + forEachChild(n, visitNode); + parentNode = saveParent; + } + } + } + + return result; } /* @internal */ diff --git a/tests/cases/fourslash/syntacticClassificationForJSDocTemplateTag.ts b/tests/cases/fourslash/syntacticClassificationForJSDocTemplateTag.ts new file mode 100644 index 00000000000..c3368207d2c --- /dev/null +++ b/tests/cases/fourslash/syntacticClassificationForJSDocTemplateTag.ts @@ -0,0 +1,22 @@ +/// + +/////** @template T */ +////function ident: T { +////} + +var c = classification; +verify.syntacticClassificationsAre( + c.comment("/** "), + c.punctuation("@"), + c.docCommentTagName("template"), + c.typeParameterName("T"), + c.comment(" */"), + c.keyword("function"), + c.identifier("ident"), + c.punctuation("<"), + c.typeParameterName("T"), + c.punctuation(">"), + c.punctuation(":"), + c.identifier("T"), + c.punctuation("{"), + c.punctuation("}")); From 92177bee913eb7d27b256d421f590290963e4c11 Mon Sep 17 00:00:00 2001 From: Vladimir Matveev Date: Thu, 2 Jun 2016 00:03:10 -0700 Subject: [PATCH 33/41] initial revision of unit test support for project system in tsserver --- Jakefile.js | 3 +- src/server/editorServices.ts | 2 +- .../cases/unittests/tsserverProjectSystem.ts | 294 ++++++++++++++++++ 3 files changed, 297 insertions(+), 2 deletions(-) create mode 100644 tests/cases/unittests/tsserverProjectSystem.ts diff --git a/Jakefile.js b/Jakefile.js index 2f0ce4aa399..c4f56c559f3 100644 --- a/Jakefile.js +++ b/Jakefile.js @@ -153,7 +153,8 @@ var harnessSources = harnessCoreSources.concat([ "tsconfigParsing.ts", "commandLineParsing.ts", "convertCompilerOptionsFromJson.ts", - "convertTypingOptionsFromJson.ts" + "convertTypingOptionsFromJson.ts", + "tsserverProjectSystem.ts" ].map(function (f) { return path.join(unittestsDirectory, f); })).concat([ diff --git a/src/server/editorServices.ts b/src/server/editorServices.ts index 5502f74408e..8eae43b503d 100644 --- a/src/server/editorServices.ts +++ b/src/server/editorServices.ts @@ -1138,7 +1138,7 @@ namespace ts.server { else { this.log("No config files found."); } - return {}; + return configFileName ? { configFileName } : {}; } /** diff --git a/tests/cases/unittests/tsserverProjectSystem.ts b/tests/cases/unittests/tsserverProjectSystem.ts new file mode 100644 index 00000000000..c69821ced5f --- /dev/null +++ b/tests/cases/unittests/tsserverProjectSystem.ts @@ -0,0 +1,294 @@ +/// + +namespace ts { + function notImplemented(): any { + throw new Error("Not yet implemented"); + } + + const nullLogger: server.Logger = { + close: () => void 0, + isVerbose: () => void 0, + loggingEnabled: () => false, + perftrc: () => void 0, + info: () => void 0, + startGroup: () => void 0, + endGroup: () => void 0, + msg: () => void 0 + }; + + const { content: libFileContent } = Harness.getDefaultLibraryFile(Harness.IO); + + function getExecutingFilePathFromLibFile(libFile: FileOrFolder): string { + return combinePaths(getDirectoryPath(libFile.path), "tsc.js"); + } + + interface FileOrFolder { + path: string; + content?: string; + } + + interface FSEntry { + path: Path; + fullPath: string; + } + + interface File extends FSEntry { + content: string; + } + + interface Folder extends FSEntry { + entries: FSEntry[]; + } + + function isFolder(s: FSEntry): s is Folder { + return isArray((s).entries); + } + + function isFile(s: FSEntry): s is File { + return typeof (s).content === "string"; + } + + function addFolder(fullPath: string, toPath: (s: string) => Path, fs: FileMap): Folder { + const path = toPath(fullPath); + if (fs.contains(path)) { + Debug.assert(isFolder(fs.get(path))); + return (fs.get(path)); + } + + const entry: Folder = { path, entries: [], fullPath }; + fs.set(path, entry); + + const baseFullPath = getDirectoryPath(fullPath); + if (fullPath !== baseFullPath) { + addFolder(baseFullPath, toPath, fs).entries.push(entry); + } + + return entry; + } + + function sizeOfMap(map: Map): number { + let n = 0; + for (const name in map) { + if (hasProperty(map, name)) { + n++; + } + } + return n; + } + + function checkMapKeys(caption: string, map: Map, expectedKeys: string[]) { + assert.equal(sizeOfMap(map), expectedKeys.length, `${caption}: incorrect size of map`); + for (const name of expectedKeys) { + assert.isTrue(hasProperty(map, name), `${caption} is expected to contain ${name}, actual keys: ${getKeys(map)}`); + } + } + + function checkFileNames(caption: string, actualFileNames: string[], expectedFileNames: string[]) { + assert.equal(actualFileNames.length, expectedFileNames.length, `${caption}: incorrect actual number of files, expected ${JSON.stringify(expectedFileNames)}, got ${actualFileNames}`); + for (const f of expectedFileNames) { + assert.isTrue(contains(actualFileNames, f), `${caption}: expected to find ${f} in ${JSON.stringify(actualFileNames)}`); + } + } + + function readDirectory(folder: FSEntry, ext: string, excludes: Path[], result: string[]): void { + if (!folder || !isFolder(folder) || contains(excludes, folder.path)) { + return; + } + for (const entry of folder.entries) { + if (contains(excludes, entry.path)) { + continue; + } + if (isFolder(entry)) { + readDirectory(entry, ext, excludes, result); + } + else if (fileExtensionIs(entry.path, ext)) { + result.push(entry.fullPath); + } + } + } + + class TestServerHost implements server.ServerHost { + args: string[] = []; + newLine: "\n"; + + private fs: ts.FileMap; + private getCanonicalFileName: (s: string) => string; + private toPath: (f: string) => Path; + readonly watchedDirectories: Map<{ cb: DirectoryWatcherCallback, recursive: boolean }[]> = {}; + readonly watchedFiles: Map = {}; + + constructor(public useCaseSensitiveFileNames: boolean, private executingFilePath: string, private currentDirectory: string, fileOrFolderList: FileOrFolder[]) { + this.getCanonicalFileName = createGetCanonicalFileName(useCaseSensitiveFileNames); + this.toPath = s => toPath(s, currentDirectory, this.getCanonicalFileName); + + this.reloadFS(fileOrFolderList); + } + + reloadFS(filesOrFolders: FileOrFolder[]) { + this.fs = createFileMap(); + for (const fileOrFolder of filesOrFolders) { + const path = this.toPath(fileOrFolder.path); + const fullPath = getNormalizedAbsolutePath(fileOrFolder.path, this.currentDirectory); + if (typeof fileOrFolder.content === "string") { + const entry = { path, content: fileOrFolder.content, fullPath }; + this.fs.set(path, entry); + addFolder(getDirectoryPath(fullPath), this.toPath, this.fs).entries.push(entry); + } + else { + addFolder(fullPath, this.toPath, this.fs); + } + } + } + + fileExists(s: string) { + const path = this.toPath(s); + return this.fs.contains(path) && isFile(this.fs.get(path)); + }; + + directoryExists(s: string) { + const path = this.toPath(s); + return this.fs.contains(path) && isFolder(this.fs.get(path)); + } + + getDirectories(s: string) { + const path = this.toPath(s); + if (!this.fs.contains(path)) { + return []; + } + else { + const entry = this.fs.get(path); + return isFolder(entry) ? map(entry.entries, x => getBaseFileName(x.fullPath)) : []; + } + } + + readDirectory(path: string, ext: string, excludes: string[]): string[] { + const result: string[] = []; + readDirectory(this.fs.get(this.toPath(path)), ext, map(excludes, e => toPath(e, path, this.getCanonicalFileName)), result); + return result; + } + + watchDirectory(directoryName: string, callback: DirectoryWatcherCallback, recursive: boolean): DirectoryWatcher { + const path = this.toPath(directoryName); + const callbacks = lookUp(this.watchedDirectories, path) || (this.watchedDirectories[path] = []); + callbacks.push({ cb: callback, recursive }); + return { + referenceCount: 0, + directoryName, + close: () => { + for (let i = 0; i < callbacks.length; i++) { + if (callbacks[i].cb === callback) { + callbacks.splice(i, 1); + break; + } + } + if (!callbacks.length) { + delete this.watchedDirectories[path]; + } + } + }; + } + + watchFile(fileName: string, callback: FileWatcherCallback) { + const path = this.toPath(fileName); + const callbacks = lookUp(this.watchedFiles, path) || (this.watchedFiles[path] = []); + callbacks.push(callback); + return { + close: () => { + const i = callbacks.indexOf(callback); + callbacks.splice(i, 1); + if (!callbacks.length) { + delete this.watchedFiles[path]; + } + } + }; + } + + // TOOD: record and invoke callbacks to simulate timer events + readonly setTimeout = (callback: (...args: any[]) => void, ms: number, ...args: any[]): any => void 0; + readonly clearTimeout = (timeoutId: any): void => void 0; + readonly readFile = (s: string) => (this.fs.get(this.toPath(s))).content; + readonly resolvePath = (s: string) => s; + readonly getExecutingFilePath = () => this.executingFilePath; + readonly getCurrentDirectory = () => this.currentDirectory; + readonly writeFile = (path: string, content: string) => notImplemented(); + readonly write = (s: string) => notImplemented(); + readonly createDirectory = (s: string) => notImplemented(); + readonly exit = () => notImplemented(); + } + + describe("tsserver project system:", () => { + it("create inferred project", () => { + const appFile: FileOrFolder = { + path: "/a/b/c/app.ts", + content: ` + import {f} from "./module" + console.log(f) + ` + }; + const libFile: FileOrFolder = { + path: "/a/lib/lib.d.ts", + content: libFileContent + }; + const moduleFile: FileOrFolder = { + path: "/a/b/c/module.d.ts", + content: `export let x: number` + }; + const host = new TestServerHost(/*useCaseSensitiveFileNames*/ false, getExecutingFilePathFromLibFile(libFile), "/", [appFile, moduleFile, libFile]); + const projectService = new server.ProjectService(host, nullLogger); + const { configFileName } = projectService.openClientFile(appFile.path); + + assert(!configFileName, `should not find config, got: '${configFileName}`); + assert.equal(projectService.inferredProjects.length, 1, "expected one inferred project"); + assert.equal(projectService.configuredProjects.length, 0, "expected no configured project"); + + const project = projectService.inferredProjects[0]; + + checkFileNames("inferred project", project.getFileNames(), [appFile.path, libFile.path, moduleFile.path]); + checkMapKeys("watchedDirectories", host.watchedDirectories, ["/a/b/c", "/a/b", "/a"]); + }); + + it("create configured project without file list", () => { + const configFile: FileOrFolder = { + path: "/a/b/tsconfig.json", + content: ` + { + "compilerOptions": {}, + "exclude": [ + "e" + ] + }` + }; + const libFile: FileOrFolder = { + path: "/a/lib/lib.d.ts", + content: libFileContent + }; + const file1: FileOrFolder = { + path: "/a/b/c/f1.ts", + content: "let x = 1" + }; + const file2: FileOrFolder = { + path: "/a/b/d/f2.ts", + content: "let y = 1" + }; + const file3: FileOrFolder = { + path: "/a/b/e/f3.ts", + content: "let z = 1" + }; + const host = new TestServerHost(/*useCaseSensitiveFileNames*/ false, getExecutingFilePathFromLibFile(libFile), "/", [ configFile, libFile, file1, file2, file3 ]); + const projectService = new server.ProjectService(host, nullLogger); + const { configFileName, configFileErrors } = projectService.openClientFile(file1.path); + + assert(configFileName, "should find config file"); + assert.isTrue(!configFileErrors, `expect no errors in config file, got ${JSON.stringify(configFileErrors)}`); + assert.equal(projectService.inferredProjects.length, 0, "expected no inferred project"); + assert.equal(projectService.configuredProjects.length, 1, "expected one configured project"); + + const project = projectService.configuredProjects[0]; + checkFileNames("configuredProjects project, actualFileNames", project.getFileNames(), [file1.path, libFile.path, file2.path]); + checkFileNames("configuredProjects project, rootFileNames", project.getRootFiles(), [file1.path, file2.path]); + + checkMapKeys("watchedFiles", host.watchedFiles, [configFile.path, file2.path, libFile.path]); // watching all files except one that was open + checkMapKeys("watchedDirectories", host.watchedDirectories, [getDirectoryPath(configFile.path)]); + }); + }); +} \ No newline at end of file From 251723826962cc3fa3cc2d766c17dc197099cce3 Mon Sep 17 00:00:00 2001 From: Anders Hejlsberg Date: Thu, 2 Jun 2016 06:32:14 -0700 Subject: [PATCH 34/41] Add non-widening forms of null and undefined --- src/compiler/checker.ts | 37 ++++++++++++++++++++----------------- src/compiler/types.ts | 6 +++--- 2 files changed, 23 insertions(+), 20 deletions(-) diff --git a/src/compiler/checker.ts b/src/compiler/checker.ts index 06ef2463402..6166234dcf4 100644 --- a/src/compiler/checker.ts +++ b/src/compiler/checker.ts @@ -110,16 +110,17 @@ namespace ts { const unknownSymbol = createSymbol(SymbolFlags.Property | SymbolFlags.Transient, "unknown"); const resolvingSymbol = createSymbol(SymbolFlags.Transient, "__resolving__"); - const nullableWideningFlags = strictNullChecks ? 0 : TypeFlags.ContainsUndefinedOrNull; const anyType = createIntrinsicType(TypeFlags.Any, "any"); const stringType = createIntrinsicType(TypeFlags.String, "string"); const numberType = createIntrinsicType(TypeFlags.Number, "number"); const booleanType = createIntrinsicType(TypeFlags.Boolean, "boolean"); const esSymbolType = createIntrinsicType(TypeFlags.ESSymbol, "symbol"); const voidType = createIntrinsicType(TypeFlags.Void, "void"); - const undefinedType = createIntrinsicType(TypeFlags.Undefined | nullableWideningFlags, "undefined"); - const nullType = createIntrinsicType(TypeFlags.Null | nullableWideningFlags, "null"); - const emptyArrayElementType = createIntrinsicType(TypeFlags.Undefined | TypeFlags.ContainsUndefinedOrNull, "undefined"); + const undefinedType = createIntrinsicType(TypeFlags.Undefined, "undefined"); + const undefinedWideningType = strictNullChecks ? undefinedType : createIntrinsicType(TypeFlags.Undefined | TypeFlags.ContainsWideningType, "undefined"); + const nullType = createIntrinsicType(TypeFlags.Null, "null"); + const nullWideningType = strictNullChecks ? nullType : createIntrinsicType(TypeFlags.Null | TypeFlags.ContainsWideningType, "null"); + const emptyArrayElementType = createIntrinsicType(TypeFlags.Undefined | TypeFlags.ContainsWideningType, "undefined"); const unknownType = createIntrinsicType(TypeFlags.Any, "unknown"); const neverType = createIntrinsicType(TypeFlags.Never, "never"); @@ -3405,7 +3406,7 @@ namespace ts { error(type.symbol.valueDeclaration, Diagnostics._0_is_referenced_directly_or_indirectly_in_its_own_base_expression, symbolToString(type.symbol)); return type.resolvedBaseConstructorType = unknownType; } - if (baseConstructorType !== unknownType && baseConstructorType !== nullType && !isConstructorType(baseConstructorType)) { + if (baseConstructorType !== unknownType && baseConstructorType !== nullWideningType && !isConstructorType(baseConstructorType)) { error(baseTypeNode.expression, Diagnostics.Type_0_is_not_a_constructor_function_type, typeToString(baseConstructorType)); return type.resolvedBaseConstructorType = unknownType; } @@ -5011,6 +5012,7 @@ namespace ts { containsAny?: boolean; containsUndefined?: boolean; containsNull?: boolean; + containsNonWideningType?: boolean; } function addTypeToSet(typeSet: TypeSet, type: Type, typeSetKind: TypeFlags) { @@ -5021,6 +5023,7 @@ namespace ts { if (type.flags & TypeFlags.Any) typeSet.containsAny = true; if (type.flags & TypeFlags.Undefined) typeSet.containsUndefined = true; if (type.flags & TypeFlags.Null) typeSet.containsNull = true; + if (!(type.flags & TypeFlags.ContainsWideningType)) typeSet.containsNonWideningType = true; } else if (type !== neverType && !contains(typeSet, type)) { typeSet.push(type); @@ -5081,8 +5084,8 @@ namespace ts { removeSubtypes(typeSet); } if (typeSet.length === 0) { - return typeSet.containsNull ? nullType : - typeSet.containsUndefined ? undefinedType : + return typeSet.containsNull ? typeSet.containsNonWideningType ? nullType : nullWideningType : + typeSet.containsUndefined ? typeSet.containsNonWideningType ? undefinedType : undefinedWideningType : neverType; } else if (typeSet.length === 1) { @@ -5882,7 +5885,7 @@ namespace ts { if (!(target.flags & TypeFlags.Never)) { if (target.flags & TypeFlags.Any || source.flags & TypeFlags.Never) return Ternary.True; if (source.flags & TypeFlags.Undefined) { - if (!strictNullChecks || target.flags & (TypeFlags.Undefined | TypeFlags.Void) || source === emptyArrayElementType) return Ternary.True; + if (!strictNullChecks || target.flags & (TypeFlags.Undefined | TypeFlags.Void)) return Ternary.True; } if (source.flags & TypeFlags.Null) { if (!strictNullChecks || target.flags & TypeFlags.Null) return Ternary.True; @@ -6972,7 +6975,7 @@ namespace ts { if (type.flags & TypeFlags.ObjectLiteral) { for (const p of getPropertiesOfObjectType(type)) { const t = getTypeOfSymbol(p); - if (t.flags & TypeFlags.ContainsUndefinedOrNull) { + if (t.flags & TypeFlags.ContainsWideningType) { if (!reportWideningErrorsInType(t)) { error(p.valueDeclaration, Diagnostics.Object_literal_s_property_0_implicitly_has_an_1_type, p.name, typeToString(getWidenedType(t))); } @@ -7019,7 +7022,7 @@ namespace ts { } function reportErrorsFromWidening(declaration: Declaration, type: Type) { - if (produceDiagnostics && compilerOptions.noImplicitAny && type.flags & TypeFlags.ContainsUndefinedOrNull) { + if (produceDiagnostics && compilerOptions.noImplicitAny && type.flags & TypeFlags.ContainsWideningType) { // Report implicit any error within type if possible, otherwise report error on declaration if (!reportWideningErrorsInType(type)) { reportImplicitAnyError(declaration, type); @@ -8311,7 +8314,7 @@ namespace ts { const classInstanceType = getDeclaredTypeOfSymbol(classSymbol); const baseConstructorType = getBaseConstructorTypeOfClass(classInstanceType); - return baseConstructorType === nullType; + return baseConstructorType === nullWideningType; } function checkThisExpression(node: Node): Type { @@ -9202,7 +9205,7 @@ namespace ts { } } } - return createArrayType(elementTypes.length ? getUnionType(elementTypes) : emptyArrayElementType); + return createArrayType(elementTypes.length ? getUnionType(elementTypes) : strictNullChecks ? neverType : undefinedWideningType); } function isNumericName(name: DeclarationName): boolean { @@ -12002,7 +12005,7 @@ namespace ts { function checkVoidExpression(node: VoidExpression): Type { checkExpression(node.expression); - return undefinedType; + return undefinedWideningType; } function checkAwaitExpression(node: AwaitExpression): Type { @@ -12409,7 +12412,7 @@ namespace ts { case SyntaxKind.InKeyword: return checkInExpression(left, right, leftType, rightType); case SyntaxKind.AmpersandAmpersandToken: - return addNullableKind(rightType, getNullableKind(leftType)); + return strictNullChecks ? addNullableKind(rightType, getNullableKind(leftType)) : rightType; case SyntaxKind.BarBarToken: return getUnionType([getNonNullableType(leftType), rightType]); case SyntaxKind.EqualsToken: @@ -12676,7 +12679,7 @@ namespace ts { case SyntaxKind.SuperKeyword: return checkSuperExpression(node); case SyntaxKind.NullKeyword: - return nullType; + return nullWideningType; case SyntaxKind.TrueKeyword: case SyntaxKind.FalseKeyword: return booleanType; @@ -12734,7 +12737,7 @@ namespace ts { case SyntaxKind.SpreadElementExpression: return checkSpreadElementExpression(node, contextualMapper); case SyntaxKind.OmittedExpression: - return undefinedType; + return undefinedWideningType; case SyntaxKind.YieldExpression: return checkYieldExpression(node); case SyntaxKind.JsxExpression: @@ -17648,7 +17651,7 @@ namespace ts { // Setup global builtins addToSymbolTable(globals, builtinGlobals, Diagnostics.Declaration_name_conflicts_with_built_in_global_identifier_0); - getSymbolLinks(undefinedSymbol).type = undefinedType; + getSymbolLinks(undefinedSymbol).type = undefinedWideningType; getSymbolLinks(argumentsSymbol).type = getGlobalType("IArguments"); getSymbolLinks(unknownSymbol).type = unknownType; diff --git a/src/compiler/types.ts b/src/compiler/types.ts index c7e14a10031..4c0e7a036db 100644 --- a/src/compiler/types.ts +++ b/src/compiler/types.ts @@ -2197,7 +2197,7 @@ namespace ts { /* @internal */ FreshObjectLiteral = 0x00100000, // Fresh object literal type /* @internal */ - ContainsUndefinedOrNull = 0x00200000, // Type is or contains undefined or null type + ContainsWideningType = 0x00200000, // Type is or contains undefined or null widening type /* @internal */ ContainsObjectLiteral = 0x00400000, // Type is or contains object literal type /* @internal */ @@ -2220,9 +2220,9 @@ namespace ts { StructuredType = ObjectType | Union | Intersection, Narrowable = Any | ObjectType | Union | TypeParameter, /* @internal */ - RequiresWidening = ContainsUndefinedOrNull | ContainsObjectLiteral, + RequiresWidening = ContainsWideningType | ContainsObjectLiteral, /* @internal */ - PropagatingFlags = ContainsUndefinedOrNull | ContainsObjectLiteral | ContainsAnyFunctionType + PropagatingFlags = ContainsWideningType | ContainsObjectLiteral | ContainsAnyFunctionType } export type DestructuringPattern = BindingPattern | ObjectLiteralExpression | ArrayLiteralExpression; From 5f3f2d302fbbb5fd95c81534944553221914a342 Mon Sep 17 00:00:00 2001 From: Anders Hejlsberg Date: Thu, 2 Jun 2016 06:47:37 -0700 Subject: [PATCH 35/41] Create separate control flows for property declarations with initializers --- src/compiler/binder.ts | 2 ++ 1 file changed, 2 insertions(+) diff --git a/src/compiler/binder.ts b/src/compiler/binder.ts index 5fdfc7f02be..8c48d984b78 100644 --- a/src/compiler/binder.ts +++ b/src/compiler/binder.ts @@ -1142,6 +1142,8 @@ namespace ts { case SyntaxKind.ModuleBlock: return ContainerFlags.IsControlFlowContainer; + case SyntaxKind.PropertyDeclaration: + return (node).initializer ? ContainerFlags.IsControlFlowContainer : 0; case SyntaxKind.CatchClause: case SyntaxKind.ForStatement: From 706683d51b90ef8b67b52db0e21182c0eaba816d Mon Sep 17 00:00:00 2001 From: Anders Hejlsberg Date: Thu, 2 Jun 2016 06:54:27 -0700 Subject: [PATCH 36/41] Add regression test --- .../controlFlowPropertyDeclarations.js | 291 +++++++++++++ .../controlFlowPropertyDeclarations.symbols | 288 +++++++++++++ .../controlFlowPropertyDeclarations.types | 383 ++++++++++++++++++ .../controlFlowPropertyDeclarations.ts | 148 +++++++ 4 files changed, 1110 insertions(+) create mode 100644 tests/baselines/reference/controlFlowPropertyDeclarations.js create mode 100644 tests/baselines/reference/controlFlowPropertyDeclarations.symbols create mode 100644 tests/baselines/reference/controlFlowPropertyDeclarations.types create mode 100644 tests/cases/compiler/controlFlowPropertyDeclarations.ts diff --git a/tests/baselines/reference/controlFlowPropertyDeclarations.js b/tests/baselines/reference/controlFlowPropertyDeclarations.js new file mode 100644 index 00000000000..748f63ea858 --- /dev/null +++ b/tests/baselines/reference/controlFlowPropertyDeclarations.js @@ -0,0 +1,291 @@ +//// [controlFlowPropertyDeclarations.ts] +// Repro from ##8913 + +declare var require:any; + +var HTMLDOMPropertyConfig = require('react/lib/HTMLDOMPropertyConfig'); + +// Populate property map with ReactJS's attribute and property mappings +// TODO handle/use .Properties value eg: MUST_USE_PROPERTY is not HTML attr +for (var propname in HTMLDOMPropertyConfig.Properties) { + if (!HTMLDOMPropertyConfig.Properties.hasOwnProperty(propname)) { + continue; + } + + var mapFrom = HTMLDOMPropertyConfig.DOMAttributeNames[propname] || propname.toLowerCase(); +} + +/** + * Repeats a string a certain number of times. + * Also: the future is bright and consists of native string repetition: + * https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/String/repeat + * + * @param {string} string String to repeat + * @param {number} times Number of times to repeat string. Integer. + * @see http://jsperf.com/string-repeater/2 + */ +function repeatString(string, times) { + if (times === 1) { + return string; + } + if (times < 0) { throw new Error(); } + var repeated = ''; + while (times) { + if (times & 1) { + repeated += string; + } + if (times >>= 1) { + string += string; + } + } + return repeated; +} + +/** + * Determine if the string ends with the specified substring. + * + * @param {string} haystack String to search in + * @param {string} needle String to search for + * @return {boolean} + */ +function endsWith(haystack, needle) { + return haystack.slice(-needle.length) === needle; +} + +/** + * Trim the specified substring off the string. If the string does not end + * with the specified substring, this is a no-op. + * + * @param {string} haystack String to search in + * @param {string} needle String to search for + * @return {string} + */ +function trimEnd(haystack, needle) { + return endsWith(haystack, needle) + ? haystack.slice(0, -needle.length) + : haystack; +} + +/** + * Convert a hyphenated string to camelCase. + */ +function hyphenToCamelCase(string) { + return string.replace(/-(.)/g, function(match, chr) { + return chr.toUpperCase(); + }); +} + +/** + * Determines if the specified string consists entirely of whitespace. + */ +function isEmpty(string) { + return !/[^\s]/.test(string); +} + +/** + * Determines if the CSS value can be converted from a + * 'px' suffixed string to a numeric value + * + * @param {string} value CSS property value + * @return {boolean} + */ +function isConvertiblePixelValue(value) { + return /^\d+px$/.test(value); +} + +export class HTMLtoJSX { + private output: string; + private level: number; + private _inPreTag: boolean; + + + /** + * Handles processing of the specified text node + * + * @param {TextNode} node + */ + _visitText = (node) => { + var parentTag = node.parentNode && node.parentNode.tagName.toLowerCase(); + if (parentTag === 'textarea' || parentTag === 'style') { + // Ignore text content of textareas and styles, as it will have already been moved + // to a "defaultValue" attribute and "dangerouslySetInnerHTML" attribute respectively. + return; + } + + var text = '' + + if (this._inPreTag) { + // If this text is contained within a
, we need to ensure the JSX
+      // whitespace coalescing rules don't eat the whitespace. This means
+      // wrapping newlines and sequences of two or more spaces in variables.
+      text = text
+        .replace(/\r/g, '')
+        .replace(/( {2,}|\n|\t|\{|\})/g, function(whitespace) {
+          return '{' + JSON.stringify(whitespace) + '}';
+        });
+    } else {
+      // If there's a newline in the text, adjust the indent level
+      if (text.indexOf('\n') > -1) {
+      }
+    }
+    this.output += text;
+  }
+
+
+
+};
+
+/**
+ * Handles parsing of inline styles
+ */
+export class StyleParser {
+  styles = {};
+  toJSXString = () => {
+    for (var key in this.styles) {
+      if (!this.styles.hasOwnProperty(key)) {
+      }
+    }
+  }
+}
+
+//// [controlFlowPropertyDeclarations.js]
+// Repro from ##8913
+"use strict";
+var HTMLDOMPropertyConfig = require('react/lib/HTMLDOMPropertyConfig');
+// Populate property map with ReactJS's attribute and property mappings
+// TODO handle/use .Properties value eg: MUST_USE_PROPERTY is not HTML attr
+for (var propname in HTMLDOMPropertyConfig.Properties) {
+    if (!HTMLDOMPropertyConfig.Properties.hasOwnProperty(propname)) {
+        continue;
+    }
+    var mapFrom = HTMLDOMPropertyConfig.DOMAttributeNames[propname] || propname.toLowerCase();
+}
+/**
+ * Repeats a string a certain number of times.
+ * Also: the future is bright and consists of native string repetition:
+ * https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/String/repeat
+ *
+ * @param {string} string  String to repeat
+ * @param {number} times   Number of times to repeat string. Integer.
+ * @see http://jsperf.com/string-repeater/2
+ */
+function repeatString(string, times) {
+    if (times === 1) {
+        return string;
+    }
+    if (times < 0) {
+        throw new Error();
+    }
+    var repeated = '';
+    while (times) {
+        if (times & 1) {
+            repeated += string;
+        }
+        if (times >>= 1) {
+            string += string;
+        }
+    }
+    return repeated;
+}
+/**
+ * Determine if the string ends with the specified substring.
+ *
+ * @param {string} haystack String to search in
+ * @param {string} needle   String to search for
+ * @return {boolean}
+ */
+function endsWith(haystack, needle) {
+    return haystack.slice(-needle.length) === needle;
+}
+/**
+ * Trim the specified substring off the string. If the string does not end
+ * with the specified substring, this is a no-op.
+ *
+ * @param {string} haystack String to search in
+ * @param {string} needle   String to search for
+ * @return {string}
+ */
+function trimEnd(haystack, needle) {
+    return endsWith(haystack, needle)
+        ? haystack.slice(0, -needle.length)
+        : haystack;
+}
+/**
+ * Convert a hyphenated string to camelCase.
+ */
+function hyphenToCamelCase(string) {
+    return string.replace(/-(.)/g, function (match, chr) {
+        return chr.toUpperCase();
+    });
+}
+/**
+ * Determines if the specified string consists entirely of whitespace.
+ */
+function isEmpty(string) {
+    return !/[^\s]/.test(string);
+}
+/**
+ * Determines if the CSS value can be converted from a
+ * 'px' suffixed string to a numeric value
+ *
+ * @param {string} value CSS property value
+ * @return {boolean}
+ */
+function isConvertiblePixelValue(value) {
+    return /^\d+px$/.test(value);
+}
+var HTMLtoJSX = (function () {
+    function HTMLtoJSX() {
+        var _this = this;
+        /**
+         * Handles processing of the specified text node
+         *
+         * @param {TextNode} node
+         */
+        this._visitText = function (node) {
+            var parentTag = node.parentNode && node.parentNode.tagName.toLowerCase();
+            if (parentTag === 'textarea' || parentTag === 'style') {
+                // Ignore text content of textareas and styles, as it will have already been moved
+                // to a "defaultValue" attribute and "dangerouslySetInnerHTML" attribute respectively.
+                return;
+            }
+            var text = '';
+            if (_this._inPreTag) {
+                // If this text is contained within a 
, we need to ensure the JSX
+                // whitespace coalescing rules don't eat the whitespace. This means
+                // wrapping newlines and sequences of two or more spaces in variables.
+                text = text
+                    .replace(/\r/g, '')
+                    .replace(/( {2,}|\n|\t|\{|\})/g, function (whitespace) {
+                    return '{' + JSON.stringify(whitespace) + '}';
+                });
+            }
+            else {
+                // If there's a newline in the text, adjust the indent level
+                if (text.indexOf('\n') > -1) {
+                }
+            }
+            _this.output += text;
+        };
+    }
+    return HTMLtoJSX;
+}());
+exports.HTMLtoJSX = HTMLtoJSX;
+;
+/**
+ * Handles parsing of inline styles
+ */
+var StyleParser = (function () {
+    function StyleParser() {
+        var _this = this;
+        this.styles = {};
+        this.toJSXString = function () {
+            for (var key in _this.styles) {
+                if (!_this.styles.hasOwnProperty(key)) {
+                }
+            }
+        };
+    }
+    return StyleParser;
+}());
+exports.StyleParser = StyleParser;
diff --git a/tests/baselines/reference/controlFlowPropertyDeclarations.symbols b/tests/baselines/reference/controlFlowPropertyDeclarations.symbols
new file mode 100644
index 00000000000..8e87d71ae33
--- /dev/null
+++ b/tests/baselines/reference/controlFlowPropertyDeclarations.symbols
@@ -0,0 +1,288 @@
+=== tests/cases/compiler/controlFlowPropertyDeclarations.ts ===
+// Repro from ##8913
+
+declare var require:any;
+>require : Symbol(require, Decl(controlFlowPropertyDeclarations.ts, 2, 11))
+
+var HTMLDOMPropertyConfig = require('react/lib/HTMLDOMPropertyConfig');
+>HTMLDOMPropertyConfig : Symbol(HTMLDOMPropertyConfig, Decl(controlFlowPropertyDeclarations.ts, 4, 3))
+>require : Symbol(require, Decl(controlFlowPropertyDeclarations.ts, 2, 11))
+
+// Populate property map with ReactJS's attribute and property mappings
+// TODO handle/use .Properties value eg: MUST_USE_PROPERTY is not HTML attr
+for (var propname in HTMLDOMPropertyConfig.Properties) {
+>propname : Symbol(propname, Decl(controlFlowPropertyDeclarations.ts, 8, 8))
+>HTMLDOMPropertyConfig : Symbol(HTMLDOMPropertyConfig, Decl(controlFlowPropertyDeclarations.ts, 4, 3))
+
+  if (!HTMLDOMPropertyConfig.Properties.hasOwnProperty(propname)) {
+>HTMLDOMPropertyConfig : Symbol(HTMLDOMPropertyConfig, Decl(controlFlowPropertyDeclarations.ts, 4, 3))
+>propname : Symbol(propname, Decl(controlFlowPropertyDeclarations.ts, 8, 8))
+
+    continue;
+  }
+
+  var mapFrom = HTMLDOMPropertyConfig.DOMAttributeNames[propname] || propname.toLowerCase();
+>mapFrom : Symbol(mapFrom, Decl(controlFlowPropertyDeclarations.ts, 13, 5))
+>HTMLDOMPropertyConfig : Symbol(HTMLDOMPropertyConfig, Decl(controlFlowPropertyDeclarations.ts, 4, 3))
+>propname : Symbol(propname, Decl(controlFlowPropertyDeclarations.ts, 8, 8))
+>propname.toLowerCase : Symbol(String.toLowerCase, Decl(lib.d.ts, --, --))
+>propname : Symbol(propname, Decl(controlFlowPropertyDeclarations.ts, 8, 8))
+>toLowerCase : Symbol(String.toLowerCase, Decl(lib.d.ts, --, --))
+}
+
+/**
+ * Repeats a string a certain number of times.
+ * Also: the future is bright and consists of native string repetition:
+ * https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/String/repeat
+ *
+ * @param {string} string  String to repeat
+ * @param {number} times   Number of times to repeat string. Integer.
+ * @see http://jsperf.com/string-repeater/2
+ */
+function repeatString(string, times) {
+>repeatString : Symbol(repeatString, Decl(controlFlowPropertyDeclarations.ts, 14, 1))
+>string : Symbol(string, Decl(controlFlowPropertyDeclarations.ts, 25, 22))
+>times : Symbol(times, Decl(controlFlowPropertyDeclarations.ts, 25, 29))
+
+  if (times === 1) {
+>times : Symbol(times, Decl(controlFlowPropertyDeclarations.ts, 25, 29))
+
+    return string;
+>string : Symbol(string, Decl(controlFlowPropertyDeclarations.ts, 25, 22))
+  }
+  if (times < 0) { throw new Error(); }
+>times : Symbol(times, Decl(controlFlowPropertyDeclarations.ts, 25, 29))
+>Error : Symbol(Error, Decl(lib.d.ts, --, --), Decl(lib.d.ts, --, --))
+
+  var repeated = '';
+>repeated : Symbol(repeated, Decl(controlFlowPropertyDeclarations.ts, 30, 5))
+
+  while (times) {
+>times : Symbol(times, Decl(controlFlowPropertyDeclarations.ts, 25, 29))
+
+    if (times & 1) {
+>times : Symbol(times, Decl(controlFlowPropertyDeclarations.ts, 25, 29))
+
+      repeated += string;
+>repeated : Symbol(repeated, Decl(controlFlowPropertyDeclarations.ts, 30, 5))
+>string : Symbol(string, Decl(controlFlowPropertyDeclarations.ts, 25, 22))
+    }
+    if (times >>= 1) {
+>times : Symbol(times, Decl(controlFlowPropertyDeclarations.ts, 25, 29))
+
+      string += string;
+>string : Symbol(string, Decl(controlFlowPropertyDeclarations.ts, 25, 22))
+>string : Symbol(string, Decl(controlFlowPropertyDeclarations.ts, 25, 22))
+    }
+  }
+  return repeated;
+>repeated : Symbol(repeated, Decl(controlFlowPropertyDeclarations.ts, 30, 5))
+}
+
+/**
+ * Determine if the string ends with the specified substring.
+ *
+ * @param {string} haystack String to search in
+ * @param {string} needle   String to search for
+ * @return {boolean}
+ */
+function endsWith(haystack, needle) {
+>endsWith : Symbol(endsWith, Decl(controlFlowPropertyDeclarations.ts, 40, 1))
+>haystack : Symbol(haystack, Decl(controlFlowPropertyDeclarations.ts, 49, 18))
+>needle : Symbol(needle, Decl(controlFlowPropertyDeclarations.ts, 49, 27))
+
+  return haystack.slice(-needle.length) === needle;
+>haystack : Symbol(haystack, Decl(controlFlowPropertyDeclarations.ts, 49, 18))
+>needle : Symbol(needle, Decl(controlFlowPropertyDeclarations.ts, 49, 27))
+>needle : Symbol(needle, Decl(controlFlowPropertyDeclarations.ts, 49, 27))
+}
+
+/**
+ * Trim the specified substring off the string. If the string does not end
+ * with the specified substring, this is a no-op.
+ *
+ * @param {string} haystack String to search in
+ * @param {string} needle   String to search for
+ * @return {string}
+ */
+function trimEnd(haystack, needle) {
+>trimEnd : Symbol(trimEnd, Decl(controlFlowPropertyDeclarations.ts, 51, 1))
+>haystack : Symbol(haystack, Decl(controlFlowPropertyDeclarations.ts, 61, 17))
+>needle : Symbol(needle, Decl(controlFlowPropertyDeclarations.ts, 61, 26))
+
+  return endsWith(haystack, needle)
+>endsWith : Symbol(endsWith, Decl(controlFlowPropertyDeclarations.ts, 40, 1))
+>haystack : Symbol(haystack, Decl(controlFlowPropertyDeclarations.ts, 61, 17))
+>needle : Symbol(needle, Decl(controlFlowPropertyDeclarations.ts, 61, 26))
+
+    ? haystack.slice(0, -needle.length)
+>haystack : Symbol(haystack, Decl(controlFlowPropertyDeclarations.ts, 61, 17))
+>needle : Symbol(needle, Decl(controlFlowPropertyDeclarations.ts, 61, 26))
+
+    : haystack;
+>haystack : Symbol(haystack, Decl(controlFlowPropertyDeclarations.ts, 61, 17))
+}
+
+/**
+ * Convert a hyphenated string to camelCase.
+ */
+function hyphenToCamelCase(string) {
+>hyphenToCamelCase : Symbol(hyphenToCamelCase, Decl(controlFlowPropertyDeclarations.ts, 65, 1))
+>string : Symbol(string, Decl(controlFlowPropertyDeclarations.ts, 70, 27))
+
+  return string.replace(/-(.)/g, function(match, chr) {
+>string : Symbol(string, Decl(controlFlowPropertyDeclarations.ts, 70, 27))
+>match : Symbol(match, Decl(controlFlowPropertyDeclarations.ts, 71, 42))
+>chr : Symbol(chr, Decl(controlFlowPropertyDeclarations.ts, 71, 48))
+
+    return chr.toUpperCase();
+>chr : Symbol(chr, Decl(controlFlowPropertyDeclarations.ts, 71, 48))
+
+  });
+}
+
+/**
+ * Determines if the specified string consists entirely of whitespace.
+ */
+function isEmpty(string) {
+>isEmpty : Symbol(isEmpty, Decl(controlFlowPropertyDeclarations.ts, 74, 1))
+>string : Symbol(string, Decl(controlFlowPropertyDeclarations.ts, 79, 17))
+
+   return !/[^\s]/.test(string);
+>/[^\s]/.test : Symbol(RegExp.test, Decl(lib.d.ts, --, --))
+>test : Symbol(RegExp.test, Decl(lib.d.ts, --, --))
+>string : Symbol(string, Decl(controlFlowPropertyDeclarations.ts, 79, 17))
+}
+
+/**
+ * Determines if the CSS value can be converted from a
+ * 'px' suffixed string to a numeric value
+ *
+ * @param {string} value CSS property value
+ * @return {boolean}
+ */
+function isConvertiblePixelValue(value) {
+>isConvertiblePixelValue : Symbol(isConvertiblePixelValue, Decl(controlFlowPropertyDeclarations.ts, 81, 1))
+>value : Symbol(value, Decl(controlFlowPropertyDeclarations.ts, 90, 33))
+
+  return /^\d+px$/.test(value);
+>/^\d+px$/.test : Symbol(RegExp.test, Decl(lib.d.ts, --, --))
+>test : Symbol(RegExp.test, Decl(lib.d.ts, --, --))
+>value : Symbol(value, Decl(controlFlowPropertyDeclarations.ts, 90, 33))
+}
+
+export class HTMLtoJSX {
+>HTMLtoJSX : Symbol(HTMLtoJSX, Decl(controlFlowPropertyDeclarations.ts, 92, 1))
+
+    private output: string;
+>output : Symbol(HTMLtoJSX.output, Decl(controlFlowPropertyDeclarations.ts, 94, 24))
+
+    private level: number;
+>level : Symbol(HTMLtoJSX.level, Decl(controlFlowPropertyDeclarations.ts, 95, 27))
+
+    private _inPreTag: boolean;
+>_inPreTag : Symbol(HTMLtoJSX._inPreTag, Decl(controlFlowPropertyDeclarations.ts, 96, 26))
+
+
+  /**
+   * Handles processing of the specified text node
+   *
+   * @param {TextNode} node
+   */
+  _visitText = (node) => {
+>_visitText : Symbol(HTMLtoJSX._visitText, Decl(controlFlowPropertyDeclarations.ts, 97, 31))
+>node : Symbol(node, Decl(controlFlowPropertyDeclarations.ts, 105, 16))
+
+    var parentTag = node.parentNode && node.parentNode.tagName.toLowerCase();
+>parentTag : Symbol(parentTag, Decl(controlFlowPropertyDeclarations.ts, 106, 7))
+>node : Symbol(node, Decl(controlFlowPropertyDeclarations.ts, 105, 16))
+>node : Symbol(node, Decl(controlFlowPropertyDeclarations.ts, 105, 16))
+
+    if (parentTag === 'textarea' || parentTag === 'style') {
+>parentTag : Symbol(parentTag, Decl(controlFlowPropertyDeclarations.ts, 106, 7))
+>parentTag : Symbol(parentTag, Decl(controlFlowPropertyDeclarations.ts, 106, 7))
+
+      // Ignore text content of textareas and styles, as it will have already been moved
+      // to a "defaultValue" attribute and "dangerouslySetInnerHTML" attribute respectively.
+      return;
+    }
+
+    var text = ''
+>text : Symbol(text, Decl(controlFlowPropertyDeclarations.ts, 113, 7))
+
+    if (this._inPreTag) {
+>this._inPreTag : Symbol(HTMLtoJSX._inPreTag, Decl(controlFlowPropertyDeclarations.ts, 96, 26))
+>this : Symbol(HTMLtoJSX, Decl(controlFlowPropertyDeclarations.ts, 92, 1))
+>_inPreTag : Symbol(HTMLtoJSX._inPreTag, Decl(controlFlowPropertyDeclarations.ts, 96, 26))
+
+      // If this text is contained within a 
, we need to ensure the JSX
+      // whitespace coalescing rules don't eat the whitespace. This means
+      // wrapping newlines and sequences of two or more spaces in variables.
+      text = text
+>text : Symbol(text, Decl(controlFlowPropertyDeclarations.ts, 113, 7))
+>text        .replace(/\r/g, '')        .replace : Symbol(String.replace, Decl(lib.d.ts, --, --), Decl(lib.d.ts, --, --), Decl(lib.d.ts, --, --), Decl(lib.d.ts, --, --))
+>text        .replace : Symbol(String.replace, Decl(lib.d.ts, --, --), Decl(lib.d.ts, --, --), Decl(lib.d.ts, --, --), Decl(lib.d.ts, --, --))
+>text : Symbol(text, Decl(controlFlowPropertyDeclarations.ts, 113, 7))
+
+        .replace(/\r/g, '')
+>replace : Symbol(String.replace, Decl(lib.d.ts, --, --), Decl(lib.d.ts, --, --), Decl(lib.d.ts, --, --), Decl(lib.d.ts, --, --))
+
+        .replace(/( {2,}|\n|\t|\{|\})/g, function(whitespace) {
+>replace : Symbol(String.replace, Decl(lib.d.ts, --, --), Decl(lib.d.ts, --, --), Decl(lib.d.ts, --, --), Decl(lib.d.ts, --, --))
+>whitespace : Symbol(whitespace, Decl(controlFlowPropertyDeclarations.ts, 121, 50))
+
+          return '{' + JSON.stringify(whitespace) + '}';
+>JSON.stringify : Symbol(JSON.stringify, Decl(lib.d.ts, --, --), Decl(lib.d.ts, --, --))
+>JSON : Symbol(JSON, Decl(lib.d.ts, --, --), Decl(lib.d.ts, --, --))
+>stringify : Symbol(JSON.stringify, Decl(lib.d.ts, --, --), Decl(lib.d.ts, --, --))
+>whitespace : Symbol(whitespace, Decl(controlFlowPropertyDeclarations.ts, 121, 50))
+
+        });
+    } else {
+      // If there's a newline in the text, adjust the indent level
+      if (text.indexOf('\n') > -1) {
+>text.indexOf : Symbol(String.indexOf, Decl(lib.d.ts, --, --))
+>text : Symbol(text, Decl(controlFlowPropertyDeclarations.ts, 113, 7))
+>indexOf : Symbol(String.indexOf, Decl(lib.d.ts, --, --))
+      }
+    }
+    this.output += text;
+>this.output : Symbol(HTMLtoJSX.output, Decl(controlFlowPropertyDeclarations.ts, 94, 24))
+>this : Symbol(HTMLtoJSX, Decl(controlFlowPropertyDeclarations.ts, 92, 1))
+>output : Symbol(HTMLtoJSX.output, Decl(controlFlowPropertyDeclarations.ts, 94, 24))
+>text : Symbol(text, Decl(controlFlowPropertyDeclarations.ts, 113, 7))
+  }
+
+
+
+};
+
+/**
+ * Handles parsing of inline styles
+ */
+export class StyleParser {
+>StyleParser : Symbol(StyleParser, Decl(controlFlowPropertyDeclarations.ts, 134, 2))
+
+  styles = {};
+>styles : Symbol(StyleParser.styles, Decl(controlFlowPropertyDeclarations.ts, 139, 26))
+
+  toJSXString = () => {
+>toJSXString : Symbol(StyleParser.toJSXString, Decl(controlFlowPropertyDeclarations.ts, 140, 14))
+
+    for (var key in this.styles) {
+>key : Symbol(key, Decl(controlFlowPropertyDeclarations.ts, 142, 12))
+>this.styles : Symbol(StyleParser.styles, Decl(controlFlowPropertyDeclarations.ts, 139, 26))
+>this : Symbol(StyleParser, Decl(controlFlowPropertyDeclarations.ts, 134, 2))
+>styles : Symbol(StyleParser.styles, Decl(controlFlowPropertyDeclarations.ts, 139, 26))
+
+      if (!this.styles.hasOwnProperty(key)) {
+>this.styles.hasOwnProperty : Symbol(Object.hasOwnProperty, Decl(lib.d.ts, --, --))
+>this.styles : Symbol(StyleParser.styles, Decl(controlFlowPropertyDeclarations.ts, 139, 26))
+>this : Symbol(StyleParser, Decl(controlFlowPropertyDeclarations.ts, 134, 2))
+>styles : Symbol(StyleParser.styles, Decl(controlFlowPropertyDeclarations.ts, 139, 26))
+>hasOwnProperty : Symbol(Object.hasOwnProperty, Decl(lib.d.ts, --, --))
+>key : Symbol(key, Decl(controlFlowPropertyDeclarations.ts, 142, 12))
+      }
+    }
+  }
+}
diff --git a/tests/baselines/reference/controlFlowPropertyDeclarations.types b/tests/baselines/reference/controlFlowPropertyDeclarations.types
new file mode 100644
index 00000000000..81c86e8625d
--- /dev/null
+++ b/tests/baselines/reference/controlFlowPropertyDeclarations.types
@@ -0,0 +1,383 @@
+=== tests/cases/compiler/controlFlowPropertyDeclarations.ts ===
+// Repro from ##8913
+
+declare var require:any;
+>require : any
+
+var HTMLDOMPropertyConfig = require('react/lib/HTMLDOMPropertyConfig');
+>HTMLDOMPropertyConfig : any
+>require('react/lib/HTMLDOMPropertyConfig') : any
+>require : any
+>'react/lib/HTMLDOMPropertyConfig' : string
+
+// Populate property map with ReactJS's attribute and property mappings
+// TODO handle/use .Properties value eg: MUST_USE_PROPERTY is not HTML attr
+for (var propname in HTMLDOMPropertyConfig.Properties) {
+>propname : string
+>HTMLDOMPropertyConfig.Properties : any
+>HTMLDOMPropertyConfig : any
+>Properties : any
+
+  if (!HTMLDOMPropertyConfig.Properties.hasOwnProperty(propname)) {
+>!HTMLDOMPropertyConfig.Properties.hasOwnProperty(propname) : boolean
+>HTMLDOMPropertyConfig.Properties.hasOwnProperty(propname) : any
+>HTMLDOMPropertyConfig.Properties.hasOwnProperty : any
+>HTMLDOMPropertyConfig.Properties : any
+>HTMLDOMPropertyConfig : any
+>Properties : any
+>hasOwnProperty : any
+>propname : string
+
+    continue;
+  }
+
+  var mapFrom = HTMLDOMPropertyConfig.DOMAttributeNames[propname] || propname.toLowerCase();
+>mapFrom : any
+>HTMLDOMPropertyConfig.DOMAttributeNames[propname] || propname.toLowerCase() : any
+>HTMLDOMPropertyConfig.DOMAttributeNames[propname] : any
+>HTMLDOMPropertyConfig.DOMAttributeNames : any
+>HTMLDOMPropertyConfig : any
+>DOMAttributeNames : any
+>propname : string
+>propname.toLowerCase() : string
+>propname.toLowerCase : () => string
+>propname : string
+>toLowerCase : () => string
+}
+
+/**
+ * Repeats a string a certain number of times.
+ * Also: the future is bright and consists of native string repetition:
+ * https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/String/repeat
+ *
+ * @param {string} string  String to repeat
+ * @param {number} times   Number of times to repeat string. Integer.
+ * @see http://jsperf.com/string-repeater/2
+ */
+function repeatString(string, times) {
+>repeatString : (string: any, times: any) => any
+>string : any
+>times : any
+
+  if (times === 1) {
+>times === 1 : boolean
+>times : any
+>1 : number
+
+    return string;
+>string : any
+  }
+  if (times < 0) { throw new Error(); }
+>times < 0 : boolean
+>times : any
+>0 : number
+>new Error() : Error
+>Error : ErrorConstructor
+
+  var repeated = '';
+>repeated : string
+>'' : string
+
+  while (times) {
+>times : any
+
+    if (times & 1) {
+>times & 1 : number
+>times : any
+>1 : number
+
+      repeated += string;
+>repeated += string : string
+>repeated : string
+>string : any
+    }
+    if (times >>= 1) {
+>times >>= 1 : number
+>times : any
+>1 : number
+
+      string += string;
+>string += string : any
+>string : any
+>string : any
+    }
+  }
+  return repeated;
+>repeated : string
+}
+
+/**
+ * Determine if the string ends with the specified substring.
+ *
+ * @param {string} haystack String to search in
+ * @param {string} needle   String to search for
+ * @return {boolean}
+ */
+function endsWith(haystack, needle) {
+>endsWith : (haystack: any, needle: any) => boolean
+>haystack : any
+>needle : any
+
+  return haystack.slice(-needle.length) === needle;
+>haystack.slice(-needle.length) === needle : boolean
+>haystack.slice(-needle.length) : any
+>haystack.slice : any
+>haystack : any
+>slice : any
+>-needle.length : number
+>needle.length : any
+>needle : any
+>length : any
+>needle : any
+}
+
+/**
+ * Trim the specified substring off the string. If the string does not end
+ * with the specified substring, this is a no-op.
+ *
+ * @param {string} haystack String to search in
+ * @param {string} needle   String to search for
+ * @return {string}
+ */
+function trimEnd(haystack, needle) {
+>trimEnd : (haystack: any, needle: any) => any
+>haystack : any
+>needle : any
+
+  return endsWith(haystack, needle)
+>endsWith(haystack, needle)    ? haystack.slice(0, -needle.length)    : haystack : any
+>endsWith(haystack, needle) : boolean
+>endsWith : (haystack: any, needle: any) => boolean
+>haystack : any
+>needle : any
+
+    ? haystack.slice(0, -needle.length)
+>haystack.slice(0, -needle.length) : any
+>haystack.slice : any
+>haystack : any
+>slice : any
+>0 : number
+>-needle.length : number
+>needle.length : any
+>needle : any
+>length : any
+
+    : haystack;
+>haystack : any
+}
+
+/**
+ * Convert a hyphenated string to camelCase.
+ */
+function hyphenToCamelCase(string) {
+>hyphenToCamelCase : (string: any) => any
+>string : any
+
+  return string.replace(/-(.)/g, function(match, chr) {
+>string.replace(/-(.)/g, function(match, chr) {    return chr.toUpperCase();  }) : any
+>string.replace : any
+>string : any
+>replace : any
+>/-(.)/g : RegExp
+>function(match, chr) {    return chr.toUpperCase();  } : (match: any, chr: any) => any
+>match : any
+>chr : any
+
+    return chr.toUpperCase();
+>chr.toUpperCase() : any
+>chr.toUpperCase : any
+>chr : any
+>toUpperCase : any
+
+  });
+}
+
+/**
+ * Determines if the specified string consists entirely of whitespace.
+ */
+function isEmpty(string) {
+>isEmpty : (string: any) => boolean
+>string : any
+
+   return !/[^\s]/.test(string);
+>!/[^\s]/.test(string) : boolean
+>/[^\s]/.test(string) : boolean
+>/[^\s]/.test : (string: string) => boolean
+>/[^\s]/ : RegExp
+>test : (string: string) => boolean
+>string : any
+}
+
+/**
+ * Determines if the CSS value can be converted from a
+ * 'px' suffixed string to a numeric value
+ *
+ * @param {string} value CSS property value
+ * @return {boolean}
+ */
+function isConvertiblePixelValue(value) {
+>isConvertiblePixelValue : (value: any) => boolean
+>value : any
+
+  return /^\d+px$/.test(value);
+>/^\d+px$/.test(value) : boolean
+>/^\d+px$/.test : (string: string) => boolean
+>/^\d+px$/ : RegExp
+>test : (string: string) => boolean
+>value : any
+}
+
+export class HTMLtoJSX {
+>HTMLtoJSX : HTMLtoJSX
+
+    private output: string;
+>output : string
+
+    private level: number;
+>level : number
+
+    private _inPreTag: boolean;
+>_inPreTag : boolean
+
+
+  /**
+   * Handles processing of the specified text node
+   *
+   * @param {TextNode} node
+   */
+  _visitText = (node) => {
+>_visitText : (node: any) => void
+>(node) => {    var parentTag = node.parentNode && node.parentNode.tagName.toLowerCase();    if (parentTag === 'textarea' || parentTag === 'style') {      // Ignore text content of textareas and styles, as it will have already been moved      // to a "defaultValue" attribute and "dangerouslySetInnerHTML" attribute respectively.      return;    }    var text = ''    if (this._inPreTag) {      // If this text is contained within a 
, we need to ensure the JSX      // whitespace coalescing rules don't eat the whitespace. This means      // wrapping newlines and sequences of two or more spaces in variables.      text = text        .replace(/\r/g, '')        .replace(/( {2,}|\n|\t|\{|\})/g, function(whitespace) {          return '{' + JSON.stringify(whitespace) + '}';        });    } else {      // If there's a newline in the text, adjust the indent level      if (text.indexOf('\n') > -1) {      }    }    this.output += text;  } : (node: any) => void
+>node : any
+
+    var parentTag = node.parentNode && node.parentNode.tagName.toLowerCase();
+>parentTag : any
+>node.parentNode && node.parentNode.tagName.toLowerCase() : any
+>node.parentNode : any
+>node : any
+>parentNode : any
+>node.parentNode.tagName.toLowerCase() : any
+>node.parentNode.tagName.toLowerCase : any
+>node.parentNode.tagName : any
+>node.parentNode : any
+>node : any
+>parentNode : any
+>tagName : any
+>toLowerCase : any
+
+    if (parentTag === 'textarea' || parentTag === 'style') {
+>parentTag === 'textarea' || parentTag === 'style' : boolean
+>parentTag === 'textarea' : boolean
+>parentTag : any
+>'textarea' : string
+>parentTag === 'style' : boolean
+>parentTag : any
+>'style' : string
+
+      // Ignore text content of textareas and styles, as it will have already been moved
+      // to a "defaultValue" attribute and "dangerouslySetInnerHTML" attribute respectively.
+      return;
+    }
+
+    var text = ''
+>text : string
+>'' : string
+
+    if (this._inPreTag) {
+>this._inPreTag : boolean
+>this : this
+>_inPreTag : boolean
+
+      // If this text is contained within a 
, we need to ensure the JSX
+      // whitespace coalescing rules don't eat the whitespace. This means
+      // wrapping newlines and sequences of two or more spaces in variables.
+      text = text
+>text = text        .replace(/\r/g, '')        .replace(/( {2,}|\n|\t|\{|\})/g, function(whitespace) {          return '{' + JSON.stringify(whitespace) + '}';        }) : string
+>text : string
+>text        .replace(/\r/g, '')        .replace(/( {2,}|\n|\t|\{|\})/g, function(whitespace) {          return '{' + JSON.stringify(whitespace) + '}';        }) : string
+>text        .replace(/\r/g, '')        .replace : { (searchValue: string, replaceValue: string): string; (searchValue: string, replacer: (substring: string, ...args: any[]) => string): string; (searchValue: RegExp, replaceValue: string): string; (searchValue: RegExp, replacer: (substring: string, ...args: any[]) => string): string; }
+>text        .replace(/\r/g, '') : string
+>text        .replace : { (searchValue: string, replaceValue: string): string; (searchValue: string, replacer: (substring: string, ...args: any[]) => string): string; (searchValue: RegExp, replaceValue: string): string; (searchValue: RegExp, replacer: (substring: string, ...args: any[]) => string): string; }
+>text : string
+
+        .replace(/\r/g, '')
+>replace : { (searchValue: string, replaceValue: string): string; (searchValue: string, replacer: (substring: string, ...args: any[]) => string): string; (searchValue: RegExp, replaceValue: string): string; (searchValue: RegExp, replacer: (substring: string, ...args: any[]) => string): string; }
+>/\r/g : RegExp
+>'' : string
+
+        .replace(/( {2,}|\n|\t|\{|\})/g, function(whitespace) {
+>replace : { (searchValue: string, replaceValue: string): string; (searchValue: string, replacer: (substring: string, ...args: any[]) => string): string; (searchValue: RegExp, replaceValue: string): string; (searchValue: RegExp, replacer: (substring: string, ...args: any[]) => string): string; }
+>/( {2,}|\n|\t|\{|\})/g : RegExp
+>function(whitespace) {          return '{' + JSON.stringify(whitespace) + '}';        } : (whitespace: string) => string
+>whitespace : string
+
+          return '{' + JSON.stringify(whitespace) + '}';
+>'{' + JSON.stringify(whitespace) + '}' : string
+>'{' + JSON.stringify(whitespace) : string
+>'{' : string
+>JSON.stringify(whitespace) : string
+>JSON.stringify : { (value: any, replacer?: (key: string, value: any) => any, space?: string | number): string; (value: any, replacer?: (number | string)[], space?: string | number): string; }
+>JSON : JSON
+>stringify : { (value: any, replacer?: (key: string, value: any) => any, space?: string | number): string; (value: any, replacer?: (number | string)[], space?: string | number): string; }
+>whitespace : string
+>'}' : string
+
+        });
+    } else {
+      // If there's a newline in the text, adjust the indent level
+      if (text.indexOf('\n') > -1) {
+>text.indexOf('\n') > -1 : boolean
+>text.indexOf('\n') : number
+>text.indexOf : (searchString: string, position?: number) => number
+>text : string
+>indexOf : (searchString: string, position?: number) => number
+>'\n' : string
+>-1 : number
+>1 : number
+      }
+    }
+    this.output += text;
+>this.output += text : string
+>this.output : string
+>this : this
+>output : string
+>text : string
+  }
+
+
+
+};
+
+/**
+ * Handles parsing of inline styles
+ */
+export class StyleParser {
+>StyleParser : StyleParser
+
+  styles = {};
+>styles : {}
+>{} : {}
+
+  toJSXString = () => {
+>toJSXString : () => void
+>() => {    for (var key in this.styles) {      if (!this.styles.hasOwnProperty(key)) {      }    }  } : () => void
+
+    for (var key in this.styles) {
+>key : string
+>this.styles : {}
+>this : this
+>styles : {}
+
+      if (!this.styles.hasOwnProperty(key)) {
+>!this.styles.hasOwnProperty(key) : boolean
+>this.styles.hasOwnProperty(key) : boolean
+>this.styles.hasOwnProperty : (v: string) => boolean
+>this.styles : {}
+>this : this
+>styles : {}
+>hasOwnProperty : (v: string) => boolean
+>key : string
+      }
+    }
+  }
+}
diff --git a/tests/cases/compiler/controlFlowPropertyDeclarations.ts b/tests/cases/compiler/controlFlowPropertyDeclarations.ts
new file mode 100644
index 00000000000..5a5e9fb96bf
--- /dev/null
+++ b/tests/cases/compiler/controlFlowPropertyDeclarations.ts
@@ -0,0 +1,148 @@
+// Repro from ##8913
+
+declare var require:any;
+
+var HTMLDOMPropertyConfig = require('react/lib/HTMLDOMPropertyConfig');
+
+// Populate property map with ReactJS's attribute and property mappings
+// TODO handle/use .Properties value eg: MUST_USE_PROPERTY is not HTML attr
+for (var propname in HTMLDOMPropertyConfig.Properties) {
+  if (!HTMLDOMPropertyConfig.Properties.hasOwnProperty(propname)) {
+    continue;
+  }
+
+  var mapFrom = HTMLDOMPropertyConfig.DOMAttributeNames[propname] || propname.toLowerCase();
+}
+
+/**
+ * Repeats a string a certain number of times.
+ * Also: the future is bright and consists of native string repetition:
+ * https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/String/repeat
+ *
+ * @param {string} string  String to repeat
+ * @param {number} times   Number of times to repeat string. Integer.
+ * @see http://jsperf.com/string-repeater/2
+ */
+function repeatString(string, times) {
+  if (times === 1) {
+    return string;
+  }
+  if (times < 0) { throw new Error(); }
+  var repeated = '';
+  while (times) {
+    if (times & 1) {
+      repeated += string;
+    }
+    if (times >>= 1) {
+      string += string;
+    }
+  }
+  return repeated;
+}
+
+/**
+ * Determine if the string ends with the specified substring.
+ *
+ * @param {string} haystack String to search in
+ * @param {string} needle   String to search for
+ * @return {boolean}
+ */
+function endsWith(haystack, needle) {
+  return haystack.slice(-needle.length) === needle;
+}
+
+/**
+ * Trim the specified substring off the string. If the string does not end
+ * with the specified substring, this is a no-op.
+ *
+ * @param {string} haystack String to search in
+ * @param {string} needle   String to search for
+ * @return {string}
+ */
+function trimEnd(haystack, needle) {
+  return endsWith(haystack, needle)
+    ? haystack.slice(0, -needle.length)
+    : haystack;
+}
+
+/**
+ * Convert a hyphenated string to camelCase.
+ */
+function hyphenToCamelCase(string) {
+  return string.replace(/-(.)/g, function(match, chr) {
+    return chr.toUpperCase();
+  });
+}
+
+/**
+ * Determines if the specified string consists entirely of whitespace.
+ */
+function isEmpty(string) {
+   return !/[^\s]/.test(string);
+}
+
+/**
+ * Determines if the CSS value can be converted from a
+ * 'px' suffixed string to a numeric value
+ *
+ * @param {string} value CSS property value
+ * @return {boolean}
+ */
+function isConvertiblePixelValue(value) {
+  return /^\d+px$/.test(value);
+}
+
+export class HTMLtoJSX {
+    private output: string;
+    private level: number;
+    private _inPreTag: boolean;
+
+
+  /**
+   * Handles processing of the specified text node
+   *
+   * @param {TextNode} node
+   */
+  _visitText = (node) => {
+    var parentTag = node.parentNode && node.parentNode.tagName.toLowerCase();
+    if (parentTag === 'textarea' || parentTag === 'style') {
+      // Ignore text content of textareas and styles, as it will have already been moved
+      // to a "defaultValue" attribute and "dangerouslySetInnerHTML" attribute respectively.
+      return;
+    }
+
+    var text = ''
+
+    if (this._inPreTag) {
+      // If this text is contained within a 
, we need to ensure the JSX
+      // whitespace coalescing rules don't eat the whitespace. This means
+      // wrapping newlines and sequences of two or more spaces in variables.
+      text = text
+        .replace(/\r/g, '')
+        .replace(/( {2,}|\n|\t|\{|\})/g, function(whitespace) {
+          return '{' + JSON.stringify(whitespace) + '}';
+        });
+    } else {
+      // If there's a newline in the text, adjust the indent level
+      if (text.indexOf('\n') > -1) {
+      }
+    }
+    this.output += text;
+  }
+
+
+
+};
+
+/**
+ * Handles parsing of inline styles
+ */
+export class StyleParser {
+  styles = {};
+  toJSXString = () => {
+    for (var key in this.styles) {
+      if (!this.styles.hasOwnProperty(key)) {
+      }
+    }
+  }
+}
\ No newline at end of file

From 20bab14224bf9f7a7228f416a61807b1f7f73e4a Mon Sep 17 00:00:00 2001
From: Anders Hejlsberg 
Date: Thu, 2 Jun 2016 09:39:47 -0700
Subject: [PATCH 37/41] Add tests

---
 .../reference/arrayLiteralWidened.js          | 15 ++++
 .../reference/arrayLiteralWidened.symbols     | 38 +++++++--
 .../reference/arrayLiteralWidened.types       | 29 +++++++
 .../reference/initializersWidened.js          | 42 +++++++++-
 .../reference/initializersWidened.symbols     | 55 ++++++++++++-
 .../reference/initializersWidened.types       | 77 ++++++++++++++++++-
 .../logicalAndOperatorWithEveryType.types     |  2 +-
 .../reference/objectLiteralWidened.js         | 40 +++++++++-
 .../reference/objectLiteralWidened.symbols    | 53 +++++++++++--
 .../reference/objectLiteralWidened.types      | 54 ++++++++++++-
 .../reference/strictNullChecksNoWidening.js   | 31 ++++++++
 .../strictNullChecksNoWidening.symbols        | 48 ++++++++++++
 .../strictNullChecksNoWidening.types          | 67 ++++++++++++++++
 .../widenedTypes/arrayLiteralWidened.ts       |  9 +++
 .../widenedTypes/initializersWidened.ts       | 24 +++++-
 .../widenedTypes/objectLiteralWidened.ts      | 22 +++++-
 .../strictNullChecksNoWidening.ts             | 17 ++++
 17 files changed, 584 insertions(+), 39 deletions(-)
 create mode 100644 tests/baselines/reference/strictNullChecksNoWidening.js
 create mode 100644 tests/baselines/reference/strictNullChecksNoWidening.symbols
 create mode 100644 tests/baselines/reference/strictNullChecksNoWidening.types
 create mode 100644 tests/cases/conformance/types/typeRelationships/widenedTypes/strictNullChecksNoWidening.ts

diff --git a/tests/baselines/reference/arrayLiteralWidened.js b/tests/baselines/reference/arrayLiteralWidened.js
index a250c47849d..5b7346b8738 100644
--- a/tests/baselines/reference/arrayLiteralWidened.js
+++ b/tests/baselines/reference/arrayLiteralWidened.js
@@ -2,6 +2,7 @@
 // array literals are widened upon assignment according to their element type
 
 var a = []; // any[]
+var a = [,,];
 
 var a = [null, null];
 var a = [undefined, undefined];
@@ -12,11 +13,20 @@ var b = [[undefined, undefined]];
 
 var c = [[[]]]; // any[][][]
 var c = [[[null]],[undefined]]
+
+// no widening when one or more elements are non-widening
+
+var x: undefined = undefined;
+
+var d = [x];
+var d = [, x];
+var d = [undefined, x];
 
 
 //// [arrayLiteralWidened.js]
 // array literals are widened upon assignment according to their element type
 var a = []; // any[]
+var a = [, ,];
 var a = [null, null];
 var a = [undefined, undefined];
 var b = [[], [null, null]]; // any[][]
@@ -24,3 +34,8 @@ var b = [[], []];
 var b = [[undefined, undefined]];
 var c = [[[]]]; // any[][][]
 var c = [[[null]], [undefined]];
+// no widening when one or more elements are non-widening
+var x = undefined;
+var d = [x];
+var d = [, x];
+var d = [undefined, x];
diff --git a/tests/baselines/reference/arrayLiteralWidened.symbols b/tests/baselines/reference/arrayLiteralWidened.symbols
index ba058aeded1..48137812a5d 100644
--- a/tests/baselines/reference/arrayLiteralWidened.symbols
+++ b/tests/baselines/reference/arrayLiteralWidened.symbols
@@ -2,31 +2,53 @@
 // array literals are widened upon assignment according to their element type
 
 var a = []; // any[]
->a : Symbol(a, Decl(arrayLiteralWidened.ts, 2, 3), Decl(arrayLiteralWidened.ts, 4, 3), Decl(arrayLiteralWidened.ts, 5, 3))
+>a : Symbol(a, Decl(arrayLiteralWidened.ts, 2, 3), Decl(arrayLiteralWidened.ts, 3, 3), Decl(arrayLiteralWidened.ts, 5, 3), Decl(arrayLiteralWidened.ts, 6, 3))
+
+var a = [,,];
+>a : Symbol(a, Decl(arrayLiteralWidened.ts, 2, 3), Decl(arrayLiteralWidened.ts, 3, 3), Decl(arrayLiteralWidened.ts, 5, 3), Decl(arrayLiteralWidened.ts, 6, 3))
 
 var a = [null, null];
->a : Symbol(a, Decl(arrayLiteralWidened.ts, 2, 3), Decl(arrayLiteralWidened.ts, 4, 3), Decl(arrayLiteralWidened.ts, 5, 3))
+>a : Symbol(a, Decl(arrayLiteralWidened.ts, 2, 3), Decl(arrayLiteralWidened.ts, 3, 3), Decl(arrayLiteralWidened.ts, 5, 3), Decl(arrayLiteralWidened.ts, 6, 3))
 
 var a = [undefined, undefined];
->a : Symbol(a, Decl(arrayLiteralWidened.ts, 2, 3), Decl(arrayLiteralWidened.ts, 4, 3), Decl(arrayLiteralWidened.ts, 5, 3))
+>a : Symbol(a, Decl(arrayLiteralWidened.ts, 2, 3), Decl(arrayLiteralWidened.ts, 3, 3), Decl(arrayLiteralWidened.ts, 5, 3), Decl(arrayLiteralWidened.ts, 6, 3))
 >undefined : Symbol(undefined)
 >undefined : Symbol(undefined)
 
 var b = [[], [null, null]]; // any[][]
->b : Symbol(b, Decl(arrayLiteralWidened.ts, 7, 3), Decl(arrayLiteralWidened.ts, 8, 3), Decl(arrayLiteralWidened.ts, 9, 3))
+>b : Symbol(b, Decl(arrayLiteralWidened.ts, 8, 3), Decl(arrayLiteralWidened.ts, 9, 3), Decl(arrayLiteralWidened.ts, 10, 3))
 
 var b = [[], []];
->b : Symbol(b, Decl(arrayLiteralWidened.ts, 7, 3), Decl(arrayLiteralWidened.ts, 8, 3), Decl(arrayLiteralWidened.ts, 9, 3))
+>b : Symbol(b, Decl(arrayLiteralWidened.ts, 8, 3), Decl(arrayLiteralWidened.ts, 9, 3), Decl(arrayLiteralWidened.ts, 10, 3))
 
 var b = [[undefined, undefined]];
->b : Symbol(b, Decl(arrayLiteralWidened.ts, 7, 3), Decl(arrayLiteralWidened.ts, 8, 3), Decl(arrayLiteralWidened.ts, 9, 3))
+>b : Symbol(b, Decl(arrayLiteralWidened.ts, 8, 3), Decl(arrayLiteralWidened.ts, 9, 3), Decl(arrayLiteralWidened.ts, 10, 3))
 >undefined : Symbol(undefined)
 >undefined : Symbol(undefined)
 
 var c = [[[]]]; // any[][][]
->c : Symbol(c, Decl(arrayLiteralWidened.ts, 11, 3), Decl(arrayLiteralWidened.ts, 12, 3))
+>c : Symbol(c, Decl(arrayLiteralWidened.ts, 12, 3), Decl(arrayLiteralWidened.ts, 13, 3))
 
 var c = [[[null]],[undefined]]
->c : Symbol(c, Decl(arrayLiteralWidened.ts, 11, 3), Decl(arrayLiteralWidened.ts, 12, 3))
+>c : Symbol(c, Decl(arrayLiteralWidened.ts, 12, 3), Decl(arrayLiteralWidened.ts, 13, 3))
 >undefined : Symbol(undefined)
 
+// no widening when one or more elements are non-widening
+
+var x: undefined = undefined;
+>x : Symbol(x, Decl(arrayLiteralWidened.ts, 17, 3))
+>undefined : Symbol(undefined)
+
+var d = [x];
+>d : Symbol(d, Decl(arrayLiteralWidened.ts, 19, 3), Decl(arrayLiteralWidened.ts, 20, 3), Decl(arrayLiteralWidened.ts, 21, 3))
+>x : Symbol(x, Decl(arrayLiteralWidened.ts, 17, 3))
+
+var d = [, x];
+>d : Symbol(d, Decl(arrayLiteralWidened.ts, 19, 3), Decl(arrayLiteralWidened.ts, 20, 3), Decl(arrayLiteralWidened.ts, 21, 3))
+>x : Symbol(x, Decl(arrayLiteralWidened.ts, 17, 3))
+
+var d = [undefined, x];
+>d : Symbol(d, Decl(arrayLiteralWidened.ts, 19, 3), Decl(arrayLiteralWidened.ts, 20, 3), Decl(arrayLiteralWidened.ts, 21, 3))
+>undefined : Symbol(undefined)
+>x : Symbol(x, Decl(arrayLiteralWidened.ts, 17, 3))
+
diff --git a/tests/baselines/reference/arrayLiteralWidened.types b/tests/baselines/reference/arrayLiteralWidened.types
index 83db7046eed..6237cd79a7e 100644
--- a/tests/baselines/reference/arrayLiteralWidened.types
+++ b/tests/baselines/reference/arrayLiteralWidened.types
@@ -5,6 +5,12 @@ var a = []; // any[]
 >a : any[]
 >[] : undefined[]
 
+var a = [,,];
+>a : any[]
+>[,,] : undefined[]
+> : undefined
+> : undefined
+
 var a = [null, null];
 >a : any[]
 >[null, null] : null[]
@@ -53,3 +59,26 @@ var c = [[[null]],[undefined]]
 >[undefined] : undefined[]
 >undefined : undefined
 
+// no widening when one or more elements are non-widening
+
+var x: undefined = undefined;
+>x : undefined
+>undefined : undefined
+
+var d = [x];
+>d : undefined[]
+>[x] : undefined[]
+>x : undefined
+
+var d = [, x];
+>d : undefined[]
+>[, x] : undefined[]
+> : undefined
+>x : undefined
+
+var d = [undefined, x];
+>d : undefined[]
+>[undefined, x] : undefined[]
+>undefined : undefined
+>x : undefined
+
diff --git a/tests/baselines/reference/initializersWidened.js b/tests/baselines/reference/initializersWidened.js
index fce0750a600..3954735ee6e 100644
--- a/tests/baselines/reference/initializersWidened.js
+++ b/tests/baselines/reference/initializersWidened.js
@@ -1,10 +1,44 @@
 //// [initializersWidened.ts]
 // these are widened to any at the point of assignment
 
-var x = null;
-var y = undefined;
+var x1 = null;
+var y1 = undefined;
+var z1 = void 0;
+
+// these are not widened
+
+var x2: null;
+var y2: undefined;
+
+var x3: null = null;
+var y3: undefined = undefined;
+var z3: undefined = void 0;
+
+// widen only when all constituents of union are widening
+
+var x4 = null || null;
+var y4 = undefined || undefined;
+var z4 = void 0 || void 0;
+
+var x5 = null || x2;
+var y5 = undefined || y2;
+var z5 = void 0 || y2;
 
 //// [initializersWidened.js]
 // these are widened to any at the point of assignment
-var x = null;
-var y = undefined;
+var x1 = null;
+var y1 = undefined;
+var z1 = void 0;
+// these are not widened
+var x2;
+var y2;
+var x3 = null;
+var y3 = undefined;
+var z3 = void 0;
+// widen only when all constituents of union are widening
+var x4 = null || null;
+var y4 = undefined || undefined;
+var z4 = void 0 || void 0;
+var x5 = null || x2;
+var y5 = undefined || y2;
+var z5 = void 0 || y2;
diff --git a/tests/baselines/reference/initializersWidened.symbols b/tests/baselines/reference/initializersWidened.symbols
index 625c066a058..252a248bec8 100644
--- a/tests/baselines/reference/initializersWidened.symbols
+++ b/tests/baselines/reference/initializersWidened.symbols
@@ -1,10 +1,57 @@
 === tests/cases/conformance/types/typeRelationships/widenedTypes/initializersWidened.ts ===
 // these are widened to any at the point of assignment
 
-var x = null;
->x : Symbol(x, Decl(initializersWidened.ts, 2, 3))
+var x1 = null;
+>x1 : Symbol(x1, Decl(initializersWidened.ts, 2, 3))
 
-var y = undefined;
->y : Symbol(y, Decl(initializersWidened.ts, 3, 3))
+var y1 = undefined;
+>y1 : Symbol(y1, Decl(initializersWidened.ts, 3, 3))
 >undefined : Symbol(undefined)
 
+var z1 = void 0;
+>z1 : Symbol(z1, Decl(initializersWidened.ts, 4, 3))
+
+// these are not widened
+
+var x2: null;
+>x2 : Symbol(x2, Decl(initializersWidened.ts, 8, 3))
+
+var y2: undefined;
+>y2 : Symbol(y2, Decl(initializersWidened.ts, 9, 3))
+
+var x3: null = null;
+>x3 : Symbol(x3, Decl(initializersWidened.ts, 11, 3))
+
+var y3: undefined = undefined;
+>y3 : Symbol(y3, Decl(initializersWidened.ts, 12, 3))
+>undefined : Symbol(undefined)
+
+var z3: undefined = void 0;
+>z3 : Symbol(z3, Decl(initializersWidened.ts, 13, 3))
+
+// widen only when all constituents of union are widening
+
+var x4 = null || null;
+>x4 : Symbol(x4, Decl(initializersWidened.ts, 17, 3))
+
+var y4 = undefined || undefined;
+>y4 : Symbol(y4, Decl(initializersWidened.ts, 18, 3))
+>undefined : Symbol(undefined)
+>undefined : Symbol(undefined)
+
+var z4 = void 0 || void 0;
+>z4 : Symbol(z4, Decl(initializersWidened.ts, 19, 3))
+
+var x5 = null || x2;
+>x5 : Symbol(x5, Decl(initializersWidened.ts, 21, 3))
+>x2 : Symbol(x2, Decl(initializersWidened.ts, 8, 3))
+
+var y5 = undefined || y2;
+>y5 : Symbol(y5, Decl(initializersWidened.ts, 22, 3))
+>undefined : Symbol(undefined)
+>y2 : Symbol(y2, Decl(initializersWidened.ts, 9, 3))
+
+var z5 = void 0 || y2;
+>z5 : Symbol(z5, Decl(initializersWidened.ts, 23, 3))
+>y2 : Symbol(y2, Decl(initializersWidened.ts, 9, 3))
+
diff --git a/tests/baselines/reference/initializersWidened.types b/tests/baselines/reference/initializersWidened.types
index 15705589246..766f859029a 100644
--- a/tests/baselines/reference/initializersWidened.types
+++ b/tests/baselines/reference/initializersWidened.types
@@ -1,11 +1,80 @@
 === tests/cases/conformance/types/typeRelationships/widenedTypes/initializersWidened.ts ===
 // these are widened to any at the point of assignment
 
-var x = null;
->x : any
+var x1 = null;
+>x1 : any
 >null : null
 
-var y = undefined;
->y : any
+var y1 = undefined;
+>y1 : any
 >undefined : undefined
 
+var z1 = void 0;
+>z1 : any
+>void 0 : undefined
+>0 : number
+
+// these are not widened
+
+var x2: null;
+>x2 : null
+>null : null
+
+var y2: undefined;
+>y2 : undefined
+
+var x3: null = null;
+>x3 : null
+>null : null
+>null : null
+
+var y3: undefined = undefined;
+>y3 : undefined
+>undefined : undefined
+
+var z3: undefined = void 0;
+>z3 : undefined
+>void 0 : undefined
+>0 : number
+
+// widen only when all constituents of union are widening
+
+var x4 = null || null;
+>x4 : any
+>null || null : null
+>null : null
+>null : null
+
+var y4 = undefined || undefined;
+>y4 : any
+>undefined || undefined : undefined
+>undefined : undefined
+>undefined : undefined
+
+var z4 = void 0 || void 0;
+>z4 : any
+>void 0 || void 0 : undefined
+>void 0 : undefined
+>0 : number
+>void 0 : undefined
+>0 : number
+
+var x5 = null || x2;
+>x5 : null
+>null || x2 : null
+>null : null
+>x2 : null
+
+var y5 = undefined || y2;
+>y5 : undefined
+>undefined || y2 : undefined
+>undefined : undefined
+>y2 : undefined
+
+var z5 = void 0 || y2;
+>z5 : undefined
+>void 0 || y2 : undefined
+>void 0 : undefined
+>0 : number
+>y2 : undefined
+
diff --git a/tests/baselines/reference/logicalAndOperatorWithEveryType.types b/tests/baselines/reference/logicalAndOperatorWithEveryType.types
index b2628eed921..bd913e94da5 100644
--- a/tests/baselines/reference/logicalAndOperatorWithEveryType.types
+++ b/tests/baselines/reference/logicalAndOperatorWithEveryType.types
@@ -623,7 +623,7 @@ var rj8 = a8 && undefined;
 
 var rj9 = null && undefined;
 >rj9 : any
->null && undefined : null
+>null && undefined : undefined
 >null : null
 >undefined : undefined
 
diff --git a/tests/baselines/reference/objectLiteralWidened.js b/tests/baselines/reference/objectLiteralWidened.js
index 4de228cb14a..98d79133540 100644
--- a/tests/baselines/reference/objectLiteralWidened.js
+++ b/tests/baselines/reference/objectLiteralWidened.js
@@ -1,29 +1,61 @@
 //// [objectLiteralWidened.ts]
 // object literal properties are widened to any
 
-var x = {
+var x1 = {
     foo: null,
     bar: undefined
 }
 
-var y = {
+var y1 = {
     foo: null,
     bar: {
         baz: null,
         boo: undefined
     }
+}
+
+// these are not widened
+
+var u: undefined = undefined;
+var n: null = null;
+
+var x2 = {
+    foo: n,
+    bar: u
+}
+
+var y2 = {
+    foo: n,
+    bar: {
+        baz: n,
+        boo: u
+    }
 }
 
 //// [objectLiteralWidened.js]
 // object literal properties are widened to any
-var x = {
+var x1 = {
     foo: null,
     bar: undefined
 };
-var y = {
+var y1 = {
     foo: null,
     bar: {
         baz: null,
         boo: undefined
     }
 };
+// these are not widened
+var u = undefined;
+var n = null;
+var x2 = {
+    foo: n,
+    bar: u
+};
+var y2 = {
+    foo: n,
+    bar: {
+        baz: n,
+        boo: u
+    }
+};
diff --git a/tests/baselines/reference/objectLiteralWidened.symbols b/tests/baselines/reference/objectLiteralWidened.symbols
index 0bf077cd9d8..4b2a1b4a001 100644
--- a/tests/baselines/reference/objectLiteralWidened.symbols
+++ b/tests/baselines/reference/objectLiteralWidened.symbols
@@ -1,22 +1,22 @@
 === tests/cases/conformance/types/typeRelationships/widenedTypes/objectLiteralWidened.ts ===
 // object literal properties are widened to any
 
-var x = {
->x : Symbol(x, Decl(objectLiteralWidened.ts, 2, 3))
+var x1 = {
+>x1 : Symbol(x1, Decl(objectLiteralWidened.ts, 2, 3))
 
     foo: null,
->foo : Symbol(foo, Decl(objectLiteralWidened.ts, 2, 9))
+>foo : Symbol(foo, Decl(objectLiteralWidened.ts, 2, 10))
 
     bar: undefined
 >bar : Symbol(bar, Decl(objectLiteralWidened.ts, 3, 14))
 >undefined : Symbol(undefined)
 }
 
-var y = {
->y : Symbol(y, Decl(objectLiteralWidened.ts, 7, 3))
+var y1 = {
+>y1 : Symbol(y1, Decl(objectLiteralWidened.ts, 7, 3))
 
     foo: null,
->foo : Symbol(foo, Decl(objectLiteralWidened.ts, 7, 9))
+>foo : Symbol(foo, Decl(objectLiteralWidened.ts, 7, 10))
 
     bar: {
 >bar : Symbol(bar, Decl(objectLiteralWidened.ts, 8, 14))
@@ -29,3 +29,44 @@ var y = {
 >undefined : Symbol(undefined)
     }
 }
+
+// these are not widened
+
+var u: undefined = undefined;
+>u : Symbol(u, Decl(objectLiteralWidened.ts, 17, 3))
+>undefined : Symbol(undefined)
+
+var n: null = null;
+>n : Symbol(n, Decl(objectLiteralWidened.ts, 18, 3))
+
+var x2 = {
+>x2 : Symbol(x2, Decl(objectLiteralWidened.ts, 20, 3))
+
+    foo: n,
+>foo : Symbol(foo, Decl(objectLiteralWidened.ts, 20, 10))
+>n : Symbol(n, Decl(objectLiteralWidened.ts, 18, 3))
+
+    bar: u
+>bar : Symbol(bar, Decl(objectLiteralWidened.ts, 21, 11))
+>u : Symbol(u, Decl(objectLiteralWidened.ts, 17, 3))
+}
+
+var y2 = {
+>y2 : Symbol(y2, Decl(objectLiteralWidened.ts, 25, 3))
+
+    foo: n,
+>foo : Symbol(foo, Decl(objectLiteralWidened.ts, 25, 10))
+>n : Symbol(n, Decl(objectLiteralWidened.ts, 18, 3))
+
+    bar: {
+>bar : Symbol(bar, Decl(objectLiteralWidened.ts, 26, 11))
+
+        baz: n,
+>baz : Symbol(baz, Decl(objectLiteralWidened.ts, 27, 10))
+>n : Symbol(n, Decl(objectLiteralWidened.ts, 18, 3))
+
+        boo: u
+>boo : Symbol(boo, Decl(objectLiteralWidened.ts, 28, 15))
+>u : Symbol(u, Decl(objectLiteralWidened.ts, 17, 3))
+    }
+}
diff --git a/tests/baselines/reference/objectLiteralWidened.types b/tests/baselines/reference/objectLiteralWidened.types
index 9f47e47795d..66309202617 100644
--- a/tests/baselines/reference/objectLiteralWidened.types
+++ b/tests/baselines/reference/objectLiteralWidened.types
@@ -1,8 +1,8 @@
 === tests/cases/conformance/types/typeRelationships/widenedTypes/objectLiteralWidened.ts ===
 // object literal properties are widened to any
 
-var x = {
->x : { foo: any; bar: any; }
+var x1 = {
+>x1 : { foo: any; bar: any; }
 >{    foo: null,    bar: undefined} : { foo: null; bar: undefined; }
 
     foo: null,
@@ -14,8 +14,8 @@ var x = {
 >undefined : undefined
 }
 
-var y = {
->y : { foo: any; bar: { baz: any; boo: any; }; }
+var y1 = {
+>y1 : { foo: any; bar: { baz: any; boo: any; }; }
 >{    foo: null,    bar: {        baz: null,        boo: undefined    }} : { foo: null; bar: { baz: null; boo: undefined; }; }
 
     foo: null,
@@ -35,3 +35,49 @@ var y = {
 >undefined : undefined
     }
 }
+
+// these are not widened
+
+var u: undefined = undefined;
+>u : undefined
+>undefined : undefined
+
+var n: null = null;
+>n : null
+>null : null
+>null : null
+
+var x2 = {
+>x2 : { foo: null; bar: undefined; }
+>{    foo: n,    bar: u} : { foo: null; bar: undefined; }
+
+    foo: n,
+>foo : null
+>n : null
+
+    bar: u
+>bar : undefined
+>u : undefined
+}
+
+var y2 = {
+>y2 : { foo: null; bar: { baz: null; boo: undefined; }; }
+>{    foo: n,    bar: {        baz: n,        boo: u    }} : { foo: null; bar: { baz: null; boo: undefined; }; }
+
+    foo: n,
+>foo : null
+>n : null
+
+    bar: {
+>bar : { baz: null; boo: undefined; }
+>{        baz: n,        boo: u    } : { baz: null; boo: undefined; }
+
+        baz: n,
+>baz : null
+>n : null
+
+        boo: u
+>boo : undefined
+>u : undefined
+    }
+}
diff --git a/tests/baselines/reference/strictNullChecksNoWidening.js b/tests/baselines/reference/strictNullChecksNoWidening.js
new file mode 100644
index 00000000000..ba26a04fc13
--- /dev/null
+++ b/tests/baselines/reference/strictNullChecksNoWidening.js
@@ -0,0 +1,31 @@
+//// [strictNullChecksNoWidening.ts]
+
+var a1 = null;
+var a2 = undefined;
+var a3 = void 0;
+
+var b1 = [];
+var b2 = [,];
+var b3 = [undefined];
+var b4 = [[], []];
+var b5 = [[], [,]];
+
+declare function f(x: T): T;
+
+var c1 = f(null);
+var c2 = f(undefined);
+var c3 = f([]);
+
+
+//// [strictNullChecksNoWidening.js]
+var a1 = null;
+var a2 = undefined;
+var a3 = void 0;
+var b1 = [];
+var b2 = [,];
+var b3 = [undefined];
+var b4 = [[], []];
+var b5 = [[], [,]];
+var c1 = f(null);
+var c2 = f(undefined);
+var c3 = f([]);
diff --git a/tests/baselines/reference/strictNullChecksNoWidening.symbols b/tests/baselines/reference/strictNullChecksNoWidening.symbols
new file mode 100644
index 00000000000..a23a0d2e926
--- /dev/null
+++ b/tests/baselines/reference/strictNullChecksNoWidening.symbols
@@ -0,0 +1,48 @@
+=== tests/cases/conformance/types/typeRelationships/widenedTypes/strictNullChecksNoWidening.ts ===
+
+var a1 = null;
+>a1 : Symbol(a1, Decl(strictNullChecksNoWidening.ts, 1, 3))
+
+var a2 = undefined;
+>a2 : Symbol(a2, Decl(strictNullChecksNoWidening.ts, 2, 3))
+>undefined : Symbol(undefined)
+
+var a3 = void 0;
+>a3 : Symbol(a3, Decl(strictNullChecksNoWidening.ts, 3, 3))
+
+var b1 = [];
+>b1 : Symbol(b1, Decl(strictNullChecksNoWidening.ts, 5, 3))
+
+var b2 = [,];
+>b2 : Symbol(b2, Decl(strictNullChecksNoWidening.ts, 6, 3))
+
+var b3 = [undefined];
+>b3 : Symbol(b3, Decl(strictNullChecksNoWidening.ts, 7, 3))
+>undefined : Symbol(undefined)
+
+var b4 = [[], []];
+>b4 : Symbol(b4, Decl(strictNullChecksNoWidening.ts, 8, 3))
+
+var b5 = [[], [,]];
+>b5 : Symbol(b5, Decl(strictNullChecksNoWidening.ts, 9, 3))
+
+declare function f(x: T): T;
+>f : Symbol(f, Decl(strictNullChecksNoWidening.ts, 9, 19))
+>T : Symbol(T, Decl(strictNullChecksNoWidening.ts, 11, 19))
+>x : Symbol(x, Decl(strictNullChecksNoWidening.ts, 11, 22))
+>T : Symbol(T, Decl(strictNullChecksNoWidening.ts, 11, 19))
+>T : Symbol(T, Decl(strictNullChecksNoWidening.ts, 11, 19))
+
+var c1 = f(null);
+>c1 : Symbol(c1, Decl(strictNullChecksNoWidening.ts, 13, 3))
+>f : Symbol(f, Decl(strictNullChecksNoWidening.ts, 9, 19))
+
+var c2 = f(undefined);
+>c2 : Symbol(c2, Decl(strictNullChecksNoWidening.ts, 14, 3))
+>f : Symbol(f, Decl(strictNullChecksNoWidening.ts, 9, 19))
+>undefined : Symbol(undefined)
+
+var c3 = f([]);
+>c3 : Symbol(c3, Decl(strictNullChecksNoWidening.ts, 15, 3))
+>f : Symbol(f, Decl(strictNullChecksNoWidening.ts, 9, 19))
+
diff --git a/tests/baselines/reference/strictNullChecksNoWidening.types b/tests/baselines/reference/strictNullChecksNoWidening.types
new file mode 100644
index 00000000000..6dd2ee3fb4c
--- /dev/null
+++ b/tests/baselines/reference/strictNullChecksNoWidening.types
@@ -0,0 +1,67 @@
+=== tests/cases/conformance/types/typeRelationships/widenedTypes/strictNullChecksNoWidening.ts ===
+
+var a1 = null;
+>a1 : null
+>null : null
+
+var a2 = undefined;
+>a2 : undefined
+>undefined : undefined
+
+var a3 = void 0;
+>a3 : undefined
+>void 0 : undefined
+>0 : number
+
+var b1 = [];
+>b1 : never[]
+>[] : never[]
+
+var b2 = [,];
+>b2 : undefined[]
+>[,] : undefined[]
+> : undefined
+
+var b3 = [undefined];
+>b3 : undefined[]
+>[undefined] : undefined[]
+>undefined : undefined
+
+var b4 = [[], []];
+>b4 : never[][]
+>[[], []] : never[][]
+>[] : never[]
+>[] : never[]
+
+var b5 = [[], [,]];
+>b5 : undefined[][]
+>[[], [,]] : undefined[][]
+>[] : never[]
+>[,] : undefined[]
+> : undefined
+
+declare function f(x: T): T;
+>f : (x: T) => T
+>T : T
+>x : T
+>T : T
+>T : T
+
+var c1 = f(null);
+>c1 : null
+>f(null) : null
+>f : (x: T) => T
+>null : null
+
+var c2 = f(undefined);
+>c2 : undefined
+>f(undefined) : undefined
+>f : (x: T) => T
+>undefined : undefined
+
+var c3 = f([]);
+>c3 : never[]
+>f([]) : never[]
+>f : (x: T) => T
+>[] : never[]
+
diff --git a/tests/cases/conformance/types/typeRelationships/widenedTypes/arrayLiteralWidened.ts b/tests/cases/conformance/types/typeRelationships/widenedTypes/arrayLiteralWidened.ts
index 8af0a5842cc..05428422129 100644
--- a/tests/cases/conformance/types/typeRelationships/widenedTypes/arrayLiteralWidened.ts
+++ b/tests/cases/conformance/types/typeRelationships/widenedTypes/arrayLiteralWidened.ts
@@ -1,6 +1,7 @@
 // array literals are widened upon assignment according to their element type
 
 var a = []; // any[]
+var a = [,,];
 
 var a = [null, null];
 var a = [undefined, undefined];
@@ -11,3 +12,11 @@ var b = [[undefined, undefined]];
 
 var c = [[[]]]; // any[][][]
 var c = [[[null]],[undefined]]
+
+// no widening when one or more elements are non-widening
+
+var x: undefined = undefined;
+
+var d = [x];
+var d = [, x];
+var d = [undefined, x];
diff --git a/tests/cases/conformance/types/typeRelationships/widenedTypes/initializersWidened.ts b/tests/cases/conformance/types/typeRelationships/widenedTypes/initializersWidened.ts
index e79cdc9e168..2eeb96194b7 100644
--- a/tests/cases/conformance/types/typeRelationships/widenedTypes/initializersWidened.ts
+++ b/tests/cases/conformance/types/typeRelationships/widenedTypes/initializersWidened.ts
@@ -1,4 +1,24 @@
 // these are widened to any at the point of assignment
 
-var x = null;
-var y = undefined;
\ No newline at end of file
+var x1 = null;
+var y1 = undefined;
+var z1 = void 0;
+
+// these are not widened
+
+var x2: null;
+var y2: undefined;
+
+var x3: null = null;
+var y3: undefined = undefined;
+var z3: undefined = void 0;
+
+// widen only when all constituents of union are widening
+
+var x4 = null || null;
+var y4 = undefined || undefined;
+var z4 = void 0 || void 0;
+
+var x5 = null || x2;
+var y5 = undefined || y2;
+var z5 = void 0 || y2;
\ No newline at end of file
diff --git a/tests/cases/conformance/types/typeRelationships/widenedTypes/objectLiteralWidened.ts b/tests/cases/conformance/types/typeRelationships/widenedTypes/objectLiteralWidened.ts
index cde44f91168..8b51e526882 100644
--- a/tests/cases/conformance/types/typeRelationships/widenedTypes/objectLiteralWidened.ts
+++ b/tests/cases/conformance/types/typeRelationships/widenedTypes/objectLiteralWidened.ts
@@ -1,14 +1,32 @@
 // object literal properties are widened to any
 
-var x = {
+var x1 = {
     foo: null,
     bar: undefined
 }
 
-var y = {
+var y1 = {
     foo: null,
     bar: {
         baz: null,
         boo: undefined
     }
+}
+
+// these are not widened
+
+var u: undefined = undefined;
+var n: null = null;
+
+var x2 = {
+    foo: n,
+    bar: u
+}
+
+var y2 = {
+    foo: n,
+    bar: {
+        baz: n,
+        boo: u
+    }
 }
\ No newline at end of file
diff --git a/tests/cases/conformance/types/typeRelationships/widenedTypes/strictNullChecksNoWidening.ts b/tests/cases/conformance/types/typeRelationships/widenedTypes/strictNullChecksNoWidening.ts
new file mode 100644
index 00000000000..8f5b4709abf
--- /dev/null
+++ b/tests/cases/conformance/types/typeRelationships/widenedTypes/strictNullChecksNoWidening.ts
@@ -0,0 +1,17 @@
+// @strictNullChecks: true
+
+var a1 = null;
+var a2 = undefined;
+var a3 = void 0;
+
+var b1 = [];
+var b2 = [,];
+var b3 = [undefined];
+var b4 = [[], []];
+var b5 = [[], [,]];
+
+declare function f(x: T): T;
+
+var c1 = f(null);
+var c2 = f(undefined);
+var c3 = f([]);

From fb2df77a59453e72e3bbc92ab4bb9e5c68cb4a8a Mon Sep 17 00:00:00 2001
From: Anders Hejlsberg 
Date: Thu, 2 Jun 2016 10:47:47 -0700
Subject: [PATCH 38/41] Remove unused variable

---
 src/compiler/checker.ts | 1 -
 1 file changed, 1 deletion(-)

diff --git a/src/compiler/checker.ts b/src/compiler/checker.ts
index 6166234dcf4..b8f078ea9a5 100644
--- a/src/compiler/checker.ts
+++ b/src/compiler/checker.ts
@@ -120,7 +120,6 @@ namespace ts {
         const undefinedWideningType = strictNullChecks ? undefinedType : createIntrinsicType(TypeFlags.Undefined | TypeFlags.ContainsWideningType, "undefined");
         const nullType = createIntrinsicType(TypeFlags.Null, "null");
         const nullWideningType = strictNullChecks ? nullType : createIntrinsicType(TypeFlags.Null | TypeFlags.ContainsWideningType, "null");
-        const emptyArrayElementType = createIntrinsicType(TypeFlags.Undefined | TypeFlags.ContainsWideningType, "undefined");
         const unknownType = createIntrinsicType(TypeFlags.Any, "unknown");
         const neverType = createIntrinsicType(TypeFlags.Never, "never");
 

From d41ac8aa9a85203f413a52842975af4a4d4e09ec Mon Sep 17 00:00:00 2001
From: zhengbli 
Date: Thu, 2 Jun 2016 11:12:38 -0700
Subject: [PATCH 39/41] Add null check and CR feedback

---
 src/compiler/parser.ts | 4 ++--
 1 file changed, 2 insertions(+), 2 deletions(-)

diff --git a/src/compiler/parser.ts b/src/compiler/parser.ts
index 13f16f3848a..e377f988d55 100644
--- a/src/compiler/parser.ts
+++ b/src/compiler/parser.ts
@@ -441,14 +441,14 @@ namespace ts {
     /* @internal */
     export function parseIsolatedJSDocComment(content: string, start?: number, length?: number) {
         const result = Parser.JSDocParser.parseIsolatedJSDocComment(content, start, length);
-        if (result.jsDocComment) {
+        if (result && result.jsDocComment) {
             // because the jsDocComment was parsed out of the source file, it might
             // not be covered by the fixupParentReferences.
             let parentNode: Node = result.jsDocComment;
             forEachChild(result.jsDocComment, visitNode);
 
             function visitNode(n: Node): void {
-                if (n.parent !== parentNode) {
+                if (n.parent === undefined) {
                     n.parent = parentNode;
 
                     const saveParent = parentNode;

From fc3e040c5167868ed623612e8f33fb3beedf73b1 Mon Sep 17 00:00:00 2001
From: Nathan Shively-Sanders 
Date: Thu, 2 Jun 2016 12:57:24 -0700
Subject: [PATCH 40/41] Revert "Merge pull request #7235 from
 weswigham/narrow-all-types"

This reverts commit ef0f6c8fe4f94a7e294cfe42d7025c9dca6535d5, reversing
changes made to 9f087cb62ade7a879e23c229df752fc8f87d679c.
---
 src/compiler/checker.ts                       |  2 +-
 src/compiler/types.ts                         |  8 +-
 .../typeGuardNarrowsPrimitiveIntersection.js  | 38 ----------
 ...eGuardNarrowsPrimitiveIntersection.symbols | 70 -----------------
 ...ypeGuardNarrowsPrimitiveIntersection.types | 76 -------------------
 .../typeGuardNarrowsToLiteralType.js          | 21 -----
 .../typeGuardNarrowsToLiteralType.symbols     | 32 --------
 .../typeGuardNarrowsToLiteralType.types       | 35 ---------
 .../typeGuardNarrowsToLiteralTypeUnion.js     | 21 -----
 ...typeGuardNarrowsToLiteralTypeUnion.symbols | 32 --------
 .../typeGuardNarrowsToLiteralTypeUnion.types  | 35 ---------
 .../typeGuardNarrowsPrimitiveIntersection.ts  | 21 -----
 .../typeGuardNarrowsToLiteralType.ts          | 10 ---
 .../typeGuardNarrowsToLiteralTypeUnion.ts     | 10 ---
 14 files changed, 2 insertions(+), 409 deletions(-)
 delete mode 100644 tests/baselines/reference/typeGuardNarrowsPrimitiveIntersection.js
 delete mode 100644 tests/baselines/reference/typeGuardNarrowsPrimitiveIntersection.symbols
 delete mode 100644 tests/baselines/reference/typeGuardNarrowsPrimitiveIntersection.types
 delete mode 100644 tests/baselines/reference/typeGuardNarrowsToLiteralType.js
 delete mode 100644 tests/baselines/reference/typeGuardNarrowsToLiteralType.symbols
 delete mode 100644 tests/baselines/reference/typeGuardNarrowsToLiteralType.types
 delete mode 100644 tests/baselines/reference/typeGuardNarrowsToLiteralTypeUnion.js
 delete mode 100644 tests/baselines/reference/typeGuardNarrowsToLiteralTypeUnion.symbols
 delete mode 100644 tests/baselines/reference/typeGuardNarrowsToLiteralTypeUnion.types
 delete mode 100644 tests/cases/conformance/expressions/typeGuards/typeGuardNarrowsPrimitiveIntersection.ts
 delete mode 100644 tests/cases/conformance/expressions/typeGuards/typeGuardNarrowsToLiteralType.ts
 delete mode 100644 tests/cases/conformance/expressions/typeGuards/typeGuardNarrowsToLiteralTypeUnion.ts

diff --git a/src/compiler/checker.ts b/src/compiler/checker.ts
index ef11dc904ea..06ef2463402 100644
--- a/src/compiler/checker.ts
+++ b/src/compiler/checker.ts
@@ -7662,7 +7662,7 @@ namespace ts {
 
         function getFlowTypeOfReference(reference: Node, declaredType: Type, assumeInitialized: boolean, includeOuterFunctions: boolean) {
             let key: string;
-            if (!reference.flowNode || assumeInitialized && (declaredType.flags & TypeFlags.NotNarrowable)) {
+            if (!reference.flowNode || assumeInitialized && !(declaredType.flags & TypeFlags.Narrowable)) {
                 return declaredType;
             }
             const initialType = assumeInitialized ? declaredType : addNullableKind(declaredType, TypeFlags.Undefined);
diff --git a/src/compiler/types.ts b/src/compiler/types.ts
index 026ee5da960..c7e14a10031 100644
--- a/src/compiler/types.ts
+++ b/src/compiler/types.ts
@@ -2218,13 +2218,7 @@ namespace ts {
         ObjectType = Class | Interface | Reference | Tuple | Anonymous,
         UnionOrIntersection = Union | Intersection,
         StructuredType = ObjectType | Union | Intersection,
-
-        // 'NotNarrowable' types are types where narrowing reverts to the original type, rather than actually narrow.
-        // This is never really correct - you can _always_ narrow to an intersection with that type, _but_ we keep
-        // Void as the only non-narrowable type, since it's a non-value type construct (representing a lack of a value)
-        // and, generally speaking, narrowing `void` should fail in some way, as it is nonsensical. (`void` narrowing
-        // to `void & T`, in a structural sense, is just narrowing to T, which we wouldn't allow under normal rules)
-        NotNarrowable = Void,
+        Narrowable = Any | ObjectType | Union | TypeParameter,
         /* @internal */
         RequiresWidening = ContainsUndefinedOrNull | ContainsObjectLiteral,
         /* @internal */
diff --git a/tests/baselines/reference/typeGuardNarrowsPrimitiveIntersection.js b/tests/baselines/reference/typeGuardNarrowsPrimitiveIntersection.js
deleted file mode 100644
index a4cba6f4ab5..00000000000
--- a/tests/baselines/reference/typeGuardNarrowsPrimitiveIntersection.js
+++ /dev/null
@@ -1,38 +0,0 @@
-//// [typeGuardNarrowsPrimitiveIntersection.ts]
-type Tag = {__tag: any};
-declare function isNonBlank(value: string) : value is (string & Tag);
-declare function doThis(value: string & Tag): void;
-declare function doThat(value: string) : void;
-let value: string;
-if (isNonBlank(value)) {
-    doThis(value);
-} else {
-    doThat(value);
-}
-
-
-const enum Tag2 {}
-declare function isNonBlank2(value: string) : value is (string & Tag2);
-declare function doThis2(value: string & Tag2): void;
-declare function doThat2(value: string) : void;
-if (isNonBlank2(value)) {
-    doThis2(value);
-} else {
-    doThat2(value);
-}
-
-
-//// [typeGuardNarrowsPrimitiveIntersection.js]
-var value;
-if (isNonBlank(value)) {
-    doThis(value);
-}
-else {
-    doThat(value);
-}
-if (isNonBlank2(value)) {
-    doThis2(value);
-}
-else {
-    doThat2(value);
-}
diff --git a/tests/baselines/reference/typeGuardNarrowsPrimitiveIntersection.symbols b/tests/baselines/reference/typeGuardNarrowsPrimitiveIntersection.symbols
deleted file mode 100644
index da507fb94c5..00000000000
--- a/tests/baselines/reference/typeGuardNarrowsPrimitiveIntersection.symbols
+++ /dev/null
@@ -1,70 +0,0 @@
-=== tests/cases/conformance/expressions/typeGuards/typeGuardNarrowsPrimitiveIntersection.ts ===
-type Tag = {__tag: any};
->Tag : Symbol(Tag, Decl(typeGuardNarrowsPrimitiveIntersection.ts, 0, 0))
->__tag : Symbol(__tag, Decl(typeGuardNarrowsPrimitiveIntersection.ts, 0, 12))
-
-declare function isNonBlank(value: string) : value is (string & Tag);
->isNonBlank : Symbol(isNonBlank, Decl(typeGuardNarrowsPrimitiveIntersection.ts, 0, 24))
->value : Symbol(value, Decl(typeGuardNarrowsPrimitiveIntersection.ts, 1, 28))
->value : Symbol(value, Decl(typeGuardNarrowsPrimitiveIntersection.ts, 1, 28))
->Tag : Symbol(Tag, Decl(typeGuardNarrowsPrimitiveIntersection.ts, 0, 0))
-
-declare function doThis(value: string & Tag): void;
->doThis : Symbol(doThis, Decl(typeGuardNarrowsPrimitiveIntersection.ts, 1, 69))
->value : Symbol(value, Decl(typeGuardNarrowsPrimitiveIntersection.ts, 2, 24))
->Tag : Symbol(Tag, Decl(typeGuardNarrowsPrimitiveIntersection.ts, 0, 0))
-
-declare function doThat(value: string) : void;
->doThat : Symbol(doThat, Decl(typeGuardNarrowsPrimitiveIntersection.ts, 2, 51))
->value : Symbol(value, Decl(typeGuardNarrowsPrimitiveIntersection.ts, 3, 24))
-
-let value: string;
->value : Symbol(value, Decl(typeGuardNarrowsPrimitiveIntersection.ts, 4, 3))
-
-if (isNonBlank(value)) {
->isNonBlank : Symbol(isNonBlank, Decl(typeGuardNarrowsPrimitiveIntersection.ts, 0, 24))
->value : Symbol(value, Decl(typeGuardNarrowsPrimitiveIntersection.ts, 4, 3))
-
-    doThis(value);
->doThis : Symbol(doThis, Decl(typeGuardNarrowsPrimitiveIntersection.ts, 1, 69))
->value : Symbol(value, Decl(typeGuardNarrowsPrimitiveIntersection.ts, 4, 3))
-
-} else {
-    doThat(value);
->doThat : Symbol(doThat, Decl(typeGuardNarrowsPrimitiveIntersection.ts, 2, 51))
->value : Symbol(value, Decl(typeGuardNarrowsPrimitiveIntersection.ts, 4, 3))
-}
-
-
-const enum Tag2 {}
->Tag2 : Symbol(Tag2, Decl(typeGuardNarrowsPrimitiveIntersection.ts, 9, 1))
-
-declare function isNonBlank2(value: string) : value is (string & Tag2);
->isNonBlank2 : Symbol(isNonBlank2, Decl(typeGuardNarrowsPrimitiveIntersection.ts, 12, 18))
->value : Symbol(value, Decl(typeGuardNarrowsPrimitiveIntersection.ts, 13, 29))
->value : Symbol(value, Decl(typeGuardNarrowsPrimitiveIntersection.ts, 13, 29))
->Tag2 : Symbol(Tag2, Decl(typeGuardNarrowsPrimitiveIntersection.ts, 9, 1))
-
-declare function doThis2(value: string & Tag2): void;
->doThis2 : Symbol(doThis2, Decl(typeGuardNarrowsPrimitiveIntersection.ts, 13, 71))
->value : Symbol(value, Decl(typeGuardNarrowsPrimitiveIntersection.ts, 14, 25))
->Tag2 : Symbol(Tag2, Decl(typeGuardNarrowsPrimitiveIntersection.ts, 9, 1))
-
-declare function doThat2(value: string) : void;
->doThat2 : Symbol(doThat2, Decl(typeGuardNarrowsPrimitiveIntersection.ts, 14, 53))
->value : Symbol(value, Decl(typeGuardNarrowsPrimitiveIntersection.ts, 15, 25))
-
-if (isNonBlank2(value)) {
->isNonBlank2 : Symbol(isNonBlank2, Decl(typeGuardNarrowsPrimitiveIntersection.ts, 12, 18))
->value : Symbol(value, Decl(typeGuardNarrowsPrimitiveIntersection.ts, 4, 3))
-
-    doThis2(value);
->doThis2 : Symbol(doThis2, Decl(typeGuardNarrowsPrimitiveIntersection.ts, 13, 71))
->value : Symbol(value, Decl(typeGuardNarrowsPrimitiveIntersection.ts, 4, 3))
-
-} else {
-    doThat2(value);
->doThat2 : Symbol(doThat2, Decl(typeGuardNarrowsPrimitiveIntersection.ts, 14, 53))
->value : Symbol(value, Decl(typeGuardNarrowsPrimitiveIntersection.ts, 4, 3))
-}
-
diff --git a/tests/baselines/reference/typeGuardNarrowsPrimitiveIntersection.types b/tests/baselines/reference/typeGuardNarrowsPrimitiveIntersection.types
deleted file mode 100644
index 478363669b4..00000000000
--- a/tests/baselines/reference/typeGuardNarrowsPrimitiveIntersection.types
+++ /dev/null
@@ -1,76 +0,0 @@
-=== tests/cases/conformance/expressions/typeGuards/typeGuardNarrowsPrimitiveIntersection.ts ===
-type Tag = {__tag: any};
->Tag : { __tag: any; }
->__tag : any
-
-declare function isNonBlank(value: string) : value is (string & Tag);
->isNonBlank : (value: string) => value is string & { __tag: any; }
->value : string
->value : any
->Tag : { __tag: any; }
-
-declare function doThis(value: string & Tag): void;
->doThis : (value: string & { __tag: any; }) => void
->value : string & { __tag: any; }
->Tag : { __tag: any; }
-
-declare function doThat(value: string) : void;
->doThat : (value: string) => void
->value : string
-
-let value: string;
->value : string
-
-if (isNonBlank(value)) {
->isNonBlank(value) : boolean
->isNonBlank : (value: string) => value is string & { __tag: any; }
->value : string
-
-    doThis(value);
->doThis(value) : void
->doThis : (value: string & { __tag: any; }) => void
->value : string & { __tag: any; }
-
-} else {
-    doThat(value);
->doThat(value) : void
->doThat : (value: string) => void
->value : string
-}
-
-
-const enum Tag2 {}
->Tag2 : Tag2
-
-declare function isNonBlank2(value: string) : value is (string & Tag2);
->isNonBlank2 : (value: string) => value is string & Tag2
->value : string
->value : any
->Tag2 : Tag2
-
-declare function doThis2(value: string & Tag2): void;
->doThis2 : (value: string & Tag2) => void
->value : string & Tag2
->Tag2 : Tag2
-
-declare function doThat2(value: string) : void;
->doThat2 : (value: string) => void
->value : string
-
-if (isNonBlank2(value)) {
->isNonBlank2(value) : boolean
->isNonBlank2 : (value: string) => value is string & Tag2
->value : string
-
-    doThis2(value);
->doThis2(value) : void
->doThis2 : (value: string & Tag2) => void
->value : string & Tag2
-
-} else {
-    doThat2(value);
->doThat2(value) : void
->doThat2 : (value: string) => void
->value : string
-}
-
diff --git a/tests/baselines/reference/typeGuardNarrowsToLiteralType.js b/tests/baselines/reference/typeGuardNarrowsToLiteralType.js
deleted file mode 100644
index fdd2ee3fb7f..00000000000
--- a/tests/baselines/reference/typeGuardNarrowsToLiteralType.js
+++ /dev/null
@@ -1,21 +0,0 @@
-//// [typeGuardNarrowsToLiteralType.ts]
-declare function isFoo(value: string) : value is "foo";
-declare function doThis(value: "foo"): void;
-declare function doThat(value: string) : void;
-let value: string;
-if (isFoo(value)) {
-    doThis(value);
-} else {
-    doThat(value);
-}
-
-
-
-//// [typeGuardNarrowsToLiteralType.js]
-var value;
-if (isFoo(value)) {
-    doThis(value);
-}
-else {
-    doThat(value);
-}
diff --git a/tests/baselines/reference/typeGuardNarrowsToLiteralType.symbols b/tests/baselines/reference/typeGuardNarrowsToLiteralType.symbols
deleted file mode 100644
index 0d0ddc5f00d..00000000000
--- a/tests/baselines/reference/typeGuardNarrowsToLiteralType.symbols
+++ /dev/null
@@ -1,32 +0,0 @@
-=== tests/cases/conformance/expressions/typeGuards/typeGuardNarrowsToLiteralType.ts ===
-declare function isFoo(value: string) : value is "foo";
->isFoo : Symbol(isFoo, Decl(typeGuardNarrowsToLiteralType.ts, 0, 0))
->value : Symbol(value, Decl(typeGuardNarrowsToLiteralType.ts, 0, 23))
->value : Symbol(value, Decl(typeGuardNarrowsToLiteralType.ts, 0, 23))
-
-declare function doThis(value: "foo"): void;
->doThis : Symbol(doThis, Decl(typeGuardNarrowsToLiteralType.ts, 0, 55))
->value : Symbol(value, Decl(typeGuardNarrowsToLiteralType.ts, 1, 24))
-
-declare function doThat(value: string) : void;
->doThat : Symbol(doThat, Decl(typeGuardNarrowsToLiteralType.ts, 1, 44))
->value : Symbol(value, Decl(typeGuardNarrowsToLiteralType.ts, 2, 24))
-
-let value: string;
->value : Symbol(value, Decl(typeGuardNarrowsToLiteralType.ts, 3, 3))
-
-if (isFoo(value)) {
->isFoo : Symbol(isFoo, Decl(typeGuardNarrowsToLiteralType.ts, 0, 0))
->value : Symbol(value, Decl(typeGuardNarrowsToLiteralType.ts, 3, 3))
-
-    doThis(value);
->doThis : Symbol(doThis, Decl(typeGuardNarrowsToLiteralType.ts, 0, 55))
->value : Symbol(value, Decl(typeGuardNarrowsToLiteralType.ts, 3, 3))
-
-} else {
-    doThat(value);
->doThat : Symbol(doThat, Decl(typeGuardNarrowsToLiteralType.ts, 1, 44))
->value : Symbol(value, Decl(typeGuardNarrowsToLiteralType.ts, 3, 3))
-}
-
-
diff --git a/tests/baselines/reference/typeGuardNarrowsToLiteralType.types b/tests/baselines/reference/typeGuardNarrowsToLiteralType.types
deleted file mode 100644
index 9835206deb9..00000000000
--- a/tests/baselines/reference/typeGuardNarrowsToLiteralType.types
+++ /dev/null
@@ -1,35 +0,0 @@
-=== tests/cases/conformance/expressions/typeGuards/typeGuardNarrowsToLiteralType.ts ===
-declare function isFoo(value: string) : value is "foo";
->isFoo : (value: string) => value is "foo"
->value : string
->value : any
-
-declare function doThis(value: "foo"): void;
->doThis : (value: "foo") => void
->value : "foo"
-
-declare function doThat(value: string) : void;
->doThat : (value: string) => void
->value : string
-
-let value: string;
->value : string
-
-if (isFoo(value)) {
->isFoo(value) : boolean
->isFoo : (value: string) => value is "foo"
->value : string
-
-    doThis(value);
->doThis(value) : void
->doThis : (value: "foo") => void
->value : "foo"
-
-} else {
-    doThat(value);
->doThat(value) : void
->doThat : (value: string) => void
->value : string
-}
-
-
diff --git a/tests/baselines/reference/typeGuardNarrowsToLiteralTypeUnion.js b/tests/baselines/reference/typeGuardNarrowsToLiteralTypeUnion.js
deleted file mode 100644
index 715b362c8e0..00000000000
--- a/tests/baselines/reference/typeGuardNarrowsToLiteralTypeUnion.js
+++ /dev/null
@@ -1,21 +0,0 @@
-//// [typeGuardNarrowsToLiteralTypeUnion.ts]
-declare function isFoo(value: string) : value is ("foo" | "bar");
-declare function doThis(value: "foo" | "bar"): void;
-declare function doThat(value: string) : void;
-let value: string;
-if (isFoo(value)) {
-    doThis(value);
-} else {
-    doThat(value);
-}
-
-
-
-//// [typeGuardNarrowsToLiteralTypeUnion.js]
-var value;
-if (isFoo(value)) {
-    doThis(value);
-}
-else {
-    doThat(value);
-}
diff --git a/tests/baselines/reference/typeGuardNarrowsToLiteralTypeUnion.symbols b/tests/baselines/reference/typeGuardNarrowsToLiteralTypeUnion.symbols
deleted file mode 100644
index 356fa06a06c..00000000000
--- a/tests/baselines/reference/typeGuardNarrowsToLiteralTypeUnion.symbols
+++ /dev/null
@@ -1,32 +0,0 @@
-=== tests/cases/conformance/expressions/typeGuards/typeGuardNarrowsToLiteralTypeUnion.ts ===
-declare function isFoo(value: string) : value is ("foo" | "bar");
->isFoo : Symbol(isFoo, Decl(typeGuardNarrowsToLiteralTypeUnion.ts, 0, 0))
->value : Symbol(value, Decl(typeGuardNarrowsToLiteralTypeUnion.ts, 0, 23))
->value : Symbol(value, Decl(typeGuardNarrowsToLiteralTypeUnion.ts, 0, 23))
-
-declare function doThis(value: "foo" | "bar"): void;
->doThis : Symbol(doThis, Decl(typeGuardNarrowsToLiteralTypeUnion.ts, 0, 65))
->value : Symbol(value, Decl(typeGuardNarrowsToLiteralTypeUnion.ts, 1, 24))
-
-declare function doThat(value: string) : void;
->doThat : Symbol(doThat, Decl(typeGuardNarrowsToLiteralTypeUnion.ts, 1, 52))
->value : Symbol(value, Decl(typeGuardNarrowsToLiteralTypeUnion.ts, 2, 24))
-
-let value: string;
->value : Symbol(value, Decl(typeGuardNarrowsToLiteralTypeUnion.ts, 3, 3))
-
-if (isFoo(value)) {
->isFoo : Symbol(isFoo, Decl(typeGuardNarrowsToLiteralTypeUnion.ts, 0, 0))
->value : Symbol(value, Decl(typeGuardNarrowsToLiteralTypeUnion.ts, 3, 3))
-
-    doThis(value);
->doThis : Symbol(doThis, Decl(typeGuardNarrowsToLiteralTypeUnion.ts, 0, 65))
->value : Symbol(value, Decl(typeGuardNarrowsToLiteralTypeUnion.ts, 3, 3))
-
-} else {
-    doThat(value);
->doThat : Symbol(doThat, Decl(typeGuardNarrowsToLiteralTypeUnion.ts, 1, 52))
->value : Symbol(value, Decl(typeGuardNarrowsToLiteralTypeUnion.ts, 3, 3))
-}
-
-
diff --git a/tests/baselines/reference/typeGuardNarrowsToLiteralTypeUnion.types b/tests/baselines/reference/typeGuardNarrowsToLiteralTypeUnion.types
deleted file mode 100644
index 321a8861d55..00000000000
--- a/tests/baselines/reference/typeGuardNarrowsToLiteralTypeUnion.types
+++ /dev/null
@@ -1,35 +0,0 @@
-=== tests/cases/conformance/expressions/typeGuards/typeGuardNarrowsToLiteralTypeUnion.ts ===
-declare function isFoo(value: string) : value is ("foo" | "bar");
->isFoo : (value: string) => value is "foo" | "bar"
->value : string
->value : any
-
-declare function doThis(value: "foo" | "bar"): void;
->doThis : (value: "foo" | "bar") => void
->value : "foo" | "bar"
-
-declare function doThat(value: string) : void;
->doThat : (value: string) => void
->value : string
-
-let value: string;
->value : string
-
-if (isFoo(value)) {
->isFoo(value) : boolean
->isFoo : (value: string) => value is "foo" | "bar"
->value : string
-
-    doThis(value);
->doThis(value) : void
->doThis : (value: "foo" | "bar") => void
->value : "foo" | "bar"
-
-} else {
-    doThat(value);
->doThat(value) : void
->doThat : (value: string) => void
->value : string
-}
-
-
diff --git a/tests/cases/conformance/expressions/typeGuards/typeGuardNarrowsPrimitiveIntersection.ts b/tests/cases/conformance/expressions/typeGuards/typeGuardNarrowsPrimitiveIntersection.ts
deleted file mode 100644
index 376a78275c8..00000000000
--- a/tests/cases/conformance/expressions/typeGuards/typeGuardNarrowsPrimitiveIntersection.ts
+++ /dev/null
@@ -1,21 +0,0 @@
-type Tag = {__tag: any};
-declare function isNonBlank(value: string) : value is (string & Tag);
-declare function doThis(value: string & Tag): void;
-declare function doThat(value: string) : void;
-let value: string;
-if (isNonBlank(value)) {
-    doThis(value);
-} else {
-    doThat(value);
-}
-
-
-const enum Tag2 {}
-declare function isNonBlank2(value: string) : value is (string & Tag2);
-declare function doThis2(value: string & Tag2): void;
-declare function doThat2(value: string) : void;
-if (isNonBlank2(value)) {
-    doThis2(value);
-} else {
-    doThat2(value);
-}
diff --git a/tests/cases/conformance/expressions/typeGuards/typeGuardNarrowsToLiteralType.ts b/tests/cases/conformance/expressions/typeGuards/typeGuardNarrowsToLiteralType.ts
deleted file mode 100644
index 3b7d5bba21a..00000000000
--- a/tests/cases/conformance/expressions/typeGuards/typeGuardNarrowsToLiteralType.ts
+++ /dev/null
@@ -1,10 +0,0 @@
-declare function isFoo(value: string) : value is "foo";
-declare function doThis(value: "foo"): void;
-declare function doThat(value: string) : void;
-let value: string;
-if (isFoo(value)) {
-    doThis(value);
-} else {
-    doThat(value);
-}
-
diff --git a/tests/cases/conformance/expressions/typeGuards/typeGuardNarrowsToLiteralTypeUnion.ts b/tests/cases/conformance/expressions/typeGuards/typeGuardNarrowsToLiteralTypeUnion.ts
deleted file mode 100644
index 8f4726160f4..00000000000
--- a/tests/cases/conformance/expressions/typeGuards/typeGuardNarrowsToLiteralTypeUnion.ts
+++ /dev/null
@@ -1,10 +0,0 @@
-declare function isFoo(value: string) : value is ("foo" | "bar");
-declare function doThis(value: "foo" | "bar"): void;
-declare function doThat(value: string) : void;
-let value: string;
-if (isFoo(value)) {
-    doThis(value);
-} else {
-    doThat(value);
-}
-

From e2a1a78dd30ea9f025b3990b73d68ecbb3eec160 Mon Sep 17 00:00:00 2001
From: zhengbli 
Date: Thu, 2 Jun 2016 13:26:15 -0700
Subject: [PATCH 41/41] reuse the fixupParentReferences function

---
 src/compiler/parser.ts | 20 ++++----------------
 1 file changed, 4 insertions(+), 16 deletions(-)

diff --git a/src/compiler/parser.ts b/src/compiler/parser.ts
index e377f988d55..b24dd85f2f0 100644
--- a/src/compiler/parser.ts
+++ b/src/compiler/parser.ts
@@ -444,19 +444,7 @@ namespace ts {
         if (result && result.jsDocComment) {
             // because the jsDocComment was parsed out of the source file, it might
             // not be covered by the fixupParentReferences.
-            let parentNode: Node = result.jsDocComment;
-            forEachChild(result.jsDocComment, visitNode);
-
-            function visitNode(n: Node): void {
-                if (n.parent === undefined) {
-                    n.parent = parentNode;
-
-                    const saveParent = parentNode;
-                    parentNode = n;
-                    forEachChild(n, visitNode);
-                    parentNode = saveParent;
-                }
-            }
+            Parser.fixupParentReferences(result.jsDocComment);
         }
 
         return result;
@@ -671,14 +659,14 @@ namespace ts {
             return node;
         }
 
-        export function fixupParentReferences(sourceFile: Node) {
+        export function fixupParentReferences(rootNode: Node) {
             // normally parent references are set during binding. However, for clients that only need
             // a syntax tree, and no semantic features, then the binding process is an unnecessary
             // overhead.  This functions allows us to set all the parents, without all the expense of
             // binding.
 
-            let parent: Node = sourceFile;
-            forEachChild(sourceFile, visitNode);
+            let parent: Node = rootNode;
+            forEachChild(rootNode, visitNode);
             return;
 
             function visitNode(n: Node): void {