From 4f4f59a78165dfec2cfcb57cd823009840597315 Mon Sep 17 00:00:00 2001 From: Mohamed Hegazy Date: Mon, 13 Oct 2014 14:06:16 -0700 Subject: [PATCH] Merge changes from master in services.ts --- src/compiler/checker.ts | 1 - src/compiler/types.ts | 1 - src/services/services.ts | 68 +++++++------------ .../bestCommonTypeObjectLiterals1.ts | 13 +++- .../completionEntryForUnionProperty.ts | 4 +- .../completionEntryForUnionProperty2.ts | 2 +- .../contextualTypingOfArrayLiterals1.ts | 4 +- .../genericTypeArgumentInference1.ts | 6 +- .../genericTypeArgumentInference2.ts | 4 +- .../fourslash/quickinfoForUnionProperty.ts | 6 +- 10 files changed, 49 insertions(+), 60 deletions(-) diff --git a/src/compiler/checker.ts b/src/compiler/checker.ts index a3edc699821..5544253b944 100644 --- a/src/compiler/checker.ts +++ b/src/compiler/checker.ts @@ -94,7 +94,6 @@ module ts { symbolToString: symbolToString, writeSymbol: writeSymbol, getAugmentedPropertiesOfApparentType: getAugmentedPropertiesOfApparentType, - getRootSymbol: getRootSymbol, getRootSymbols: getRootSymbols, getContextualType: getContextualType, getFullyQualifiedName: getFullyQualifiedName, diff --git a/src/compiler/types.ts b/src/compiler/types.ts index 5534c908d70..3b25f6b4a6b 100644 --- a/src/compiler/types.ts +++ b/src/compiler/types.ts @@ -653,7 +653,6 @@ module ts { writeSymbol(symbol: Symbol, writer: SymbolWriter, enclosingDeclaration?: Node, meaning?: SymbolFlags, flags?: SymbolFormatFlags): void; getFullyQualifiedName(symbol: Symbol): string; getAugmentedPropertiesOfApparentType(type: Type): Symbol[]; - getRootSymbol(symbol: Symbol): Symbol; getRootSymbols(symbol: Symbol): Symbol[]; getContextualType(node: Node): Type; getResolvedSignature(node: CallExpression, candidatesOutArray?: Signature[]): Signature; diff --git a/src/services/services.ts b/src/services/services.ts index 13f6b390d4b..96aff32cb74 100644 --- a/src/services/services.ts +++ b/src/services/services.ts @@ -2259,9 +2259,13 @@ module ts { return undefined; } + // TODO(drosen): Right now we just permit *all* semantic meanings when calling 'getSymbolKind' + // which is permissible given that it is backwards compatible; but really we should consider + // passing the meaning for the node so that we don't report that a suggestion for a value is an interface. + // We COULD also just do what 'getSymbolModifiers' does, which is to use the first declaration. return { name: displayName, - kind: getSymbolKind(symbol), + kind: getSymbolKind(symbol, SemanticMeaning.All), kindModifiers: getSymbolModifiers(symbol) }; } @@ -2613,7 +2617,7 @@ module ts { // which is permissible given that it is backwards compatible; but really we should consider // passing the meaning for the node so that we don't report that a suggestion for a value is an interface. // We COULD also just do what 'getSymbolModifiers' does, which is to use the first declaration. - var displayPartsDocumentationsAndSymbolKind = getSymbolDisplayPartsDocumentationAndSymbolKind(symbol, getSourceFile(filename), session.location, session.typeChecker, session.location); + var displayPartsDocumentationsAndSymbolKind = getSymbolDisplayPartsDocumentationAndSymbolKind(symbol, getSourceFile(filename), session.location, session.typeChecker, session.location, SemanticMeaning.All); return { name: entryName, kind: displayPartsDocumentationsAndSymbolKind.symbolKind, @@ -2656,15 +2660,19 @@ module ts { } } - // TODO(drosen): use contextual SemanticMeaning. - function getSymbolKind(symbol: Symbol): string { + function getSymbolKind(symbol: Symbol, meaningAtLocation: SemanticMeaning): string { var flags = typeInfoResolver.getRootSymbols(symbol)[0].getFlags(); if (flags & SymbolFlags.Class) return ScriptElementKind.classElement; if (flags & SymbolFlags.Enum) return ScriptElementKind.enumElement; - if (flags & SymbolFlags.Interface) return ScriptElementKind.interfaceElement; - if (flags & SymbolFlags.TypeParameter) return ScriptElementKind.typeParameterElement; - + + // The following should only apply if encountered at a type position, + // and need to have precedence over other meanings if this is the case. + if (meaningAtLocation & SemanticMeaning.Type) { + if (flags & SymbolFlags.Interface) return ScriptElementKind.interfaceElement; + if (flags & SymbolFlags.TypeParameter) return ScriptElementKind.typeParameterElement; + } + var result = getSymbolKindOfConstructorPropertyMethodAccessorFunctionOrVar(symbol, flags); if (result === ScriptElementKind.unknown) { if (flags & SymbolFlags.TypeParameter) return ScriptElementKind.typeParameterElement; @@ -2737,15 +2745,13 @@ module ts { : ScriptElementKindModifier.none; } - // TODO(drosen): use contextual SemanticMeaning. - function getSymbolDisplayPartsDocumentationAndSymbolKind(symbol: Symbol, - sourceFile: SourceFile, - enclosingDeclaration: Node, - typeResolver: TypeChecker, - location: Node) { + function getSymbolDisplayPartsDocumentationAndSymbolKind(symbol: Symbol, sourceFile: SourceFile, enclosingDeclaration: Node, + typeResolver: TypeChecker, location: Node, + // TODO(drosen): Currently completion entry details passes the SemanticMeaning.All instead of using semanticMeaning of location + semanticMeaning = getMeaningFromLocation(location)) { var displayParts: SymbolDisplayPart[] = []; var documentation: SymbolDisplayPart[]; - var symbolFlags = typeResolver.getRootSymbol(symbol).flags; + var symbolFlags = typeResolver.getRootSymbols(symbol)[0].flags; var symbolKind = getSymbolKindOfConstructorPropertyMethodAccessorFunctionOrVar(symbol, symbolFlags); var hasAddedSymbolInfo: boolean; // Class at constructor site need to be shown as constructor apart from property,method, vars @@ -2847,14 +2853,13 @@ module ts { } } } - if (symbolFlags & SymbolFlags.Class && !hasAddedSymbolInfo) { displayParts.push(keywordPart(SyntaxKind.ClassKeyword)); displayParts.push(spacePart()); displayParts.push.apply(displayParts, symbolToDisplayParts(typeResolver, symbol, sourceFile, /*meaning*/ undefined, SymbolFormatFlags.WriteTypeParametersOrArguments)); writeTypeParametersOfSymbol(symbol, sourceFile); } - if (symbolFlags & SymbolFlags.Interface) { + if ((symbolFlags & SymbolFlags.Interface) && (semanticMeaning & SemanticMeaning.Type)) { addNewLineIfDisplayPartsExist(); displayParts.push(keywordPart(SyntaxKind.InterfaceKeyword)); displayParts.push(spacePart()); @@ -2873,7 +2878,7 @@ module ts { displayParts.push(spacePart()); displayParts.push.apply(displayParts, symbolToDisplayParts(typeResolver, symbol, sourceFile)); } - if (symbolFlags & SymbolFlags.TypeParameter) { + if ((symbolFlags & SymbolFlags.TypeParameter) && (semanticMeaning & SemanticMeaning.Type)) { addNewLineIfDisplayPartsExist(); displayParts.push(punctuationPart(SyntaxKind.OpenParenToken)); displayParts.push(textPart("type parameter")); @@ -2953,7 +2958,7 @@ module ts { } } else { - symbolKind = getSymbolKind(symbol); + symbolKind = getSymbolKind(symbol, semanticMeaning); } } @@ -3015,7 +3020,6 @@ module ts { var symbol = typeInfoResolver.getSymbolInfo(node); if (!symbol) { - // Try getting just type at this position and show switch (node.kind) { case SyntaxKind.Identifier: @@ -3107,25 +3111,6 @@ module ts { return false; } - function getDefinitionFromSymbol(symbol: Symbol, location: Node, result: DefinitionInfo[]): void { - var declarations = symbol.getDeclarations(); - if (declarations) { - var symbolName = typeInfoResolver.symbolToString(symbol); // Do not get scoped name, just the name of the symbol - var symbolKind = getSymbolKind(symbol); - var containerSymbol = symbol.parent; - var containerName = containerSymbol ? typeInfoResolver.symbolToString(containerSymbol, location) : ""; - var containerKind = containerSymbol ? getSymbolKind(symbol) : ""; - - if (!tryAddConstructSignature(symbol, location, symbolKind, symbolName, containerName, result) && - !tryAddCallSignature(symbol, location, symbolKind, symbolName, containerName, result)) { - // Just add all the declarations. - forEach(declarations, declaration => { - result.push(getDefinitionInfo(declaration, symbolKind, symbolName, containerName)); - }); - } - } - } - synchronizeHostData(); filename = TypeScript.switchToForwardSlashes(filename); @@ -3173,7 +3158,7 @@ module ts { var declarations = symbol.getDeclarations(); var symbolName = typeInfoResolver.symbolToString(symbol); // Do not get scoped name, just the name of the symbol - var symbolKind = getSymbolKind(symbol); + var symbolKind = getSymbolKind(symbol, getMeaningFromLocation(node)); var containerSymbol = symbol.parent; var containerName = containerSymbol ? typeInfoResolver.symbolToString(containerSymbol, node) : ""; @@ -4703,7 +4688,6 @@ module ts { function classifySymbol(symbol: Symbol, meaningAtPosition: SemanticMeaning) { var flags = symbol.getFlags(); - // TODO(drosen): use meaningAtPosition. if (flags & SymbolFlags.Class) { return ClassificationTypeNames.className; } @@ -4721,8 +4705,6 @@ module ts { else if (flags & SymbolFlags.Module) { return ClassificationTypeNames.moduleName; } - - return undefined; } function processNode(node: Node) { @@ -5192,7 +5174,7 @@ module ts { // Only allow a symbol to be renamed if it actually has at least one declaration. if (symbol && symbol.getDeclarations() && symbol.getDeclarations().length > 0) { - var kind = getSymbolKind(symbol); + var kind = getSymbolKind(symbol, getMeaningFromLocation(node)); if (kind) { return getRenameInfo(symbol.name, typeInfoResolver.getFullyQualifiedName(symbol), kind, getSymbolModifiers(symbol), diff --git a/tests/cases/fourslash/bestCommonTypeObjectLiterals1.ts b/tests/cases/fourslash/bestCommonTypeObjectLiterals1.ts index 1632fff5df8..917ed526d69 100644 --- a/tests/cases/fourslash/bestCommonTypeObjectLiterals1.ts +++ b/tests/cases/fourslash/bestCommonTypeObjectLiterals1.ts @@ -28,10 +28,19 @@ goTo.marker('2'); verify.quickInfoIs('(var) c1: {\n name: string;\n age: number;\n}[]'); goTo.marker('3'); -verify.quickInfoIs('(var) c2: {}[]'); +verify.quickInfoIs('(var) c2: Array<{\n\ + name: string;\n\ + age: number;\n\ + address: string;\n\ +} | {\n\ + name: string;\n\ + age: number;\n\ + dob: Date;\n\ +}>'); goTo.marker('4'); verify.quickInfoIs('(var) c2a: {\n name: string;\n age: number;\n}[]'); goTo.marker('5'); -verify.quickInfoIs('(var) c3: I[]'); \ No newline at end of file +verify.quickInfoIs('(var) c3: I[]'); + diff --git a/tests/cases/fourslash/completionEntryForUnionProperty.ts b/tests/cases/fourslash/completionEntryForUnionProperty.ts index 6d5a3627a9c..bc0f9405a1b 100644 --- a/tests/cases/fourslash/completionEntryForUnionProperty.ts +++ b/tests/cases/fourslash/completionEntryForUnionProperty.ts @@ -15,6 +15,6 @@ ////x./**/ goTo.marker(); -verify.memberListContains("commonProperty", "string | number", undefined, undefined, "property"); -verify.memberListContains("commonFunction", "() => number", undefined, undefined, "method"); +verify.memberListContains("commonProperty", "(property) commonProperty: string | number"); +verify.memberListContains("commonFunction", "(method) commonFunction(): number"); verify.memberListCount(2); \ No newline at end of file diff --git a/tests/cases/fourslash/completionEntryForUnionProperty2.ts b/tests/cases/fourslash/completionEntryForUnionProperty2.ts index fef5e0dfb50..b32e6d05345 100644 --- a/tests/cases/fourslash/completionEntryForUnionProperty2.ts +++ b/tests/cases/fourslash/completionEntryForUnionProperty2.ts @@ -15,5 +15,5 @@ ////x.commonProperty./**/ goTo.marker(); -verify.memberListContains("toString", "() => string", undefined, undefined, "method"); +verify.memberListContains("toString", "(method) toString(): string"); verify.memberListCount(1); \ No newline at end of file diff --git a/tests/cases/fourslash/contextualTypingOfArrayLiterals1.ts b/tests/cases/fourslash/contextualTypingOfArrayLiterals1.ts index 9e59d21c53b..128f801300b 100644 --- a/tests/cases/fourslash/contextualTypingOfArrayLiterals1.ts +++ b/tests/cases/fourslash/contextualTypingOfArrayLiterals1.ts @@ -43,7 +43,7 @@ goTo.marker('4'); verify.quickInfoIs('(var) r4: C'); goTo.marker('5'); -verify.quickInfoIs('(var) x5: {\n name: string;\n age: number;\n}[]'); +verify.quickInfoIs('(var) x5: C[]'); goTo.marker('6'); -verify.quickInfoIs('(var) r5: {\n name: string;\n age: number;\n}'); \ No newline at end of file +verify.quickInfoIs('(var) r5: C'); \ No newline at end of file diff --git a/tests/cases/fourslash/genericTypeArgumentInference1.ts b/tests/cases/fourslash/genericTypeArgumentInference1.ts index 96d603b939c..eadec7ad0ce 100644 --- a/tests/cases/fourslash/genericTypeArgumentInference1.ts +++ b/tests/cases/fourslash/genericTypeArgumentInference1.ts @@ -18,9 +18,9 @@ ////var /*4*/r4 = _./*41*/all([true], _.identity); goTo.marker('1'); -verify.quickInfoIs('(var) r: {}'); +verify.quickInfoIs('(var) r: string | number | boolean'); goTo.marker('11'); -verify.quickInfoIs('(method) Underscore.Static.all<{}>(list: {}[], iterator?: Underscore.Iterator<{}, boolean>, context?: any): {}'); +verify.quickInfoIs('(method) Underscore.Static.all(list: Array, iterator?: Underscore.Iterator, context?: any): string | number | boolean'); goTo.marker('2'); verify.quickInfoIs('(var) r2: boolean'); @@ -37,4 +37,4 @@ verify.quickInfoIs('(var) r4: any'); goTo.marker('41'); verify.quickInfoIs('(method) Underscore.Static.all(list: any[], iterator?: Underscore.Iterator, context?: any): any'); -verify.numberOfErrorsInCurrentFile(0); +verify.numberOfErrorsInCurrentFile(0); \ No newline at end of file diff --git a/tests/cases/fourslash/genericTypeArgumentInference2.ts b/tests/cases/fourslash/genericTypeArgumentInference2.ts index 0ae5114b133..a660675c51e 100644 --- a/tests/cases/fourslash/genericTypeArgumentInference2.ts +++ b/tests/cases/fourslash/genericTypeArgumentInference2.ts @@ -18,9 +18,9 @@ ////var /*4*/r4 = _./*41*/all([true], _.identity); goTo.marker('1'); -verify.quickInfoIs('(var) r: {}'); +verify.quickInfoIs('(var) r: string | number | boolean'); goTo.marker('11'); -verify.quickInfoIs('(method) Underscore.Static.all<{}>(list: {}[], iterator?: Underscore.Iterator<{}, boolean>, context?: any): {}'); +verify.quickInfoIs('(method) Underscore.Static.all(list: Array, iterator?: Underscore.Iterator, context?: any): string | number | boolean'); goTo.marker('2'); verify.quickInfoIs('(var) r2: boolean'); diff --git a/tests/cases/fourslash/quickinfoForUnionProperty.ts b/tests/cases/fourslash/quickinfoForUnionProperty.ts index 665d03f9435..b5c7dc0a01c 100644 --- a/tests/cases/fourslash/quickinfoForUnionProperty.ts +++ b/tests/cases/fourslash/quickinfoForUnionProperty.ts @@ -17,11 +17,11 @@ goTo.marker("1"); -verify.quickInfoIs("One | Two", "", "x", "var"); +verify.quickInfoIs('(var) x: One | Two'); goTo.marker("2"); -verify.quickInfoIs("string | number", "", "commonProperty", "property"); +verify.quickInfoIs('(property) commonProperty: string | number'); goTo.marker("3"); -verify.quickInfoIs("() => number", "", "commonFunction", "method"); +verify.quickInfoIs('(method) commonFunction(): number');