Skip to content

Commit dc03115

Browse files
authored
Merge pull request #28585 from Microsoft/isTypeOnlyCompletion
Properly set symbolMeanings when calling getSymbolsInScope
2 parents eeaa8bb + 59c014b commit dc03115

File tree

2 files changed

+29
-5
lines changed

2 files changed

+29
-5
lines changed

src/services/completions.ts

Lines changed: 10 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1021,7 +1021,8 @@ namespace ts.Completions {
10211021
const scopeNode = getScopeNode(contextToken, adjustedPosition, sourceFile) || sourceFile;
10221022
isInSnippetScope = isSnippetScope(scopeNode);
10231023

1024-
const symbolMeanings = SymbolFlags.Type | SymbolFlags.Value | SymbolFlags.Namespace | SymbolFlags.Alias;
1024+
const isTypeOnly = isTypeOnlyCompletion();
1025+
const symbolMeanings = (isTypeOnly ? SymbolFlags.None : SymbolFlags.Value) | SymbolFlags.Type | SymbolFlags.Namespace | SymbolFlags.Alias;
10251026

10261027
symbols = Debug.assertEachDefined(typeChecker.getSymbolsInScope(scopeNode, symbolMeanings), "getSymbolsInScope() should all be defined");
10271028

@@ -1070,9 +1071,9 @@ namespace ts.Completions {
10701071
}
10711072

10721073
function filterGlobalCompletion(symbols: Symbol[]): void {
1073-
const isTypeOnlyCompletion = insideJsDocTagTypeExpression || !isContextTokenValueLocation(contextToken) && (isPartOfTypeNode(location) || isContextTokenTypeLocation(contextToken));
1074-
const allowTypes = isTypeOnlyCompletion || !isContextTokenValueLocation(contextToken) && isPossiblyTypeArgumentPosition(contextToken, sourceFile, typeChecker);
1075-
if (isTypeOnlyCompletion) keywordFilters = KeywordCompletionFilters.TypeKeywords;
1074+
const isTypeOnly = isTypeOnlyCompletion();
1075+
const allowTypes = isTypeOnly || !isContextTokenValueLocation(contextToken) && isPossiblyTypeArgumentPosition(contextToken, sourceFile, typeChecker);
1076+
if (isTypeOnly) keywordFilters = KeywordCompletionFilters.TypeKeywords;
10761077

10771078
filterMutate(symbols, symbol => {
10781079
if (!isSourceFile(location)) {
@@ -1091,7 +1092,7 @@ namespace ts.Completions {
10911092
if (allowTypes) {
10921093
// Its a type, but you can reach it by namespace.type as well
10931094
const symbolAllowedAsType = symbolCanBeReferencedAtTypeLocation(symbol);
1094-
if (symbolAllowedAsType || isTypeOnlyCompletion) {
1095+
if (symbolAllowedAsType || isTypeOnly) {
10951096
return symbolAllowedAsType;
10961097
}
10971098
}
@@ -1102,6 +1103,10 @@ namespace ts.Completions {
11021103
});
11031104
}
11041105

1106+
function isTypeOnlyCompletion(): boolean {
1107+
return insideJsDocTagTypeExpression || !isContextTokenValueLocation(contextToken) && (isPartOfTypeNode(location) || isContextTokenTypeLocation(contextToken));
1108+
}
1109+
11051110
function isContextTokenValueLocation(contextToken: Node) {
11061111
return contextToken &&
11071112
contextToken.kind === SyntaxKind.TypeOfKeyword &&
Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
/// <reference path='fourslash.ts'/>
2+
3+
// @noLib: true
4+
5+
// @Filename: /abc.ts
6+
////export type Abc = number;
7+
8+
// @Filename: /user.ts
9+
//// import { Abc } from "./abc";
10+
////function f(Abc: Ab/**/) {}
11+
12+
verify.completions({
13+
marker: "",
14+
// Should not have an import completion for 'Abc', should use the local one
15+
exact: ["Abc", ...completion.typeKeywords],
16+
preferences: {
17+
includeCompletionsForModuleExports: true,
18+
},
19+
});

0 commit comments

Comments
 (0)