Skip to content

Commit 5001433

Browse files
authored
Allow valid contextual keyword completions even when an auto-import by the same name exists (#56001)
1 parent 0f3a76c commit 5001433

File tree

2 files changed

+65
-1
lines changed

2 files changed

+65
-1
lines changed

src/services/completions.ts

Lines changed: 33 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1329,7 +1329,11 @@ function completionInfoFromData(
13291329

13301330
if (keywordFilters !== KeywordCompletionFilters.None) {
13311331
for (const keywordEntry of getKeywordCompletions(keywordFilters, !insideJsDocTagTypeExpression && isSourceFileJS(sourceFile))) {
1332-
if (isTypeOnlyLocation && isTypeKeyword(stringToToken(keywordEntry.name)!) || !uniqueNames.has(keywordEntry.name)) {
1332+
if (
1333+
isTypeOnlyLocation && isTypeKeyword(stringToToken(keywordEntry.name)!) ||
1334+
!isTypeOnlyLocation && isContextualKeywordInAutoImportableExpressionSpace(keywordEntry.name) ||
1335+
!uniqueNames.has(keywordEntry.name)
1336+
) {
13331337
uniqueNames.add(keywordEntry.name);
13341338
insertSorted(entries, keywordEntry, compareCompletionEntries, /*allowDuplicates*/ true);
13351339
}
@@ -5829,3 +5833,31 @@ function toUpperCharCode(charCode: number) {
58295833
}
58305834
return charCode;
58315835
}
5836+
5837+
/**
5838+
* These are all the contextual keywords that would be valid to auto-import
5839+
* in expression space and also a valid keyword in the same location, depending
5840+
* on what gets typed afterwards. In these cases, we want to offer both the
5841+
* auto-import and the keyword completion. For example,
5842+
*
5843+
* ```ts
5844+
* type
5845+
* ```
5846+
*
5847+
* may be the beginning of a type alias declaration (keyword completion), or
5848+
* it may be the beginning of
5849+
*
5850+
* ```ts
5851+
* import { type } from "os";
5852+
* type() === "Darwin" ? doSomething() : doSomethingElse();
5853+
* ```
5854+
*/
5855+
function isContextualKeywordInAutoImportableExpressionSpace(keyword: string) {
5856+
return keyword === "abstract" ||
5857+
keyword === "async" ||
5858+
keyword === "await" ||
5859+
keyword === "declare" ||
5860+
keyword === "module" ||
5861+
keyword === "namespace" ||
5862+
keyword === "type";
5863+
}
Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
/// <reference path="fourslash.ts" />
2+
3+
// @module: nodenext
4+
5+
// @Filename: /os.d.ts
6+
//// declare module "os" {
7+
//// export function type(): string;
8+
//// }
9+
10+
// @Filename: /index.ts
11+
//// type/**/
12+
13+
verify.completions({
14+
marker: "",
15+
includes: [
16+
{
17+
name: "type",
18+
sortText: completion.SortText.GlobalsOrKeywords,
19+
},
20+
{
21+
name: "type",
22+
source: "os",
23+
sourceDisplay: "os",
24+
hasAction: true,
25+
sortText: completion.SortText.AutoImportSuggestions
26+
}
27+
],
28+
preferences: {
29+
includeCompletionsForModuleExports: true,
30+
allowIncompleteCompletions: true,
31+
},
32+
});

0 commit comments

Comments
 (0)