Merge pull request #28585 from Microsoft/isTypeOnlyCompletion

Properly set symbolMeanings when calling getSymbolsInScope
This commit is contained in:
Sheetal Nandi 2018-11-19 08:50:41 -08:00 committed by GitHub
commit dc03115d14
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 29 additions and 5 deletions

View File

@ -1021,7 +1021,8 @@ namespace ts.Completions {
const scopeNode = getScopeNode(contextToken, adjustedPosition, sourceFile) || sourceFile;
isInSnippetScope = isSnippetScope(scopeNode);
const symbolMeanings = SymbolFlags.Type | SymbolFlags.Value | SymbolFlags.Namespace | SymbolFlags.Alias;
const isTypeOnly = isTypeOnlyCompletion();
const symbolMeanings = (isTypeOnly ? SymbolFlags.None : SymbolFlags.Value) | SymbolFlags.Type | SymbolFlags.Namespace | SymbolFlags.Alias;
symbols = Debug.assertEachDefined(typeChecker.getSymbolsInScope(scopeNode, symbolMeanings), "getSymbolsInScope() should all be defined");
@ -1070,9 +1071,9 @@ namespace ts.Completions {
}
function filterGlobalCompletion(symbols: Symbol[]): void {
const isTypeOnlyCompletion = insideJsDocTagTypeExpression || !isContextTokenValueLocation(contextToken) && (isPartOfTypeNode(location) || isContextTokenTypeLocation(contextToken));
const allowTypes = isTypeOnlyCompletion || !isContextTokenValueLocation(contextToken) && isPossiblyTypeArgumentPosition(contextToken, sourceFile, typeChecker);
if (isTypeOnlyCompletion) keywordFilters = KeywordCompletionFilters.TypeKeywords;
const isTypeOnly = isTypeOnlyCompletion();
const allowTypes = isTypeOnly || !isContextTokenValueLocation(contextToken) && isPossiblyTypeArgumentPosition(contextToken, sourceFile, typeChecker);
if (isTypeOnly) keywordFilters = KeywordCompletionFilters.TypeKeywords;
filterMutate(symbols, symbol => {
if (!isSourceFile(location)) {
@ -1091,7 +1092,7 @@ namespace ts.Completions {
if (allowTypes) {
// Its a type, but you can reach it by namespace.type as well
const symbolAllowedAsType = symbolCanBeReferencedAtTypeLocation(symbol);
if (symbolAllowedAsType || isTypeOnlyCompletion) {
if (symbolAllowedAsType || isTypeOnly) {
return symbolAllowedAsType;
}
}
@ -1102,6 +1103,10 @@ namespace ts.Completions {
});
}
function isTypeOnlyCompletion(): boolean {
return insideJsDocTagTypeExpression || !isContextTokenValueLocation(contextToken) && (isPartOfTypeNode(location) || isContextTokenTypeLocation(contextToken));
}
function isContextTokenValueLocation(contextToken: Node) {
return contextToken &&
contextToken.kind === SyntaxKind.TypeOfKeyword &&

View File

@ -0,0 +1,19 @@
/// <reference path='fourslash.ts'/>
// @noLib: true
// @Filename: /abc.ts
////export type Abc = number;
// @Filename: /user.ts
//// import { Abc } from "./abc";
////function f(Abc: Ab/**/) {}
verify.completions({
marker: "",
// Should not have an import completion for 'Abc', should use the local one
exact: ["Abc", ...completion.typeKeywords],
preferences: {
includeCompletionsForModuleExports: true,
},
});