Skip to content

Commit a437de6

Browse files
authored
fix(54818): "from" keyword displaying incorrect completions (#54843)
1 parent bd1e975 commit a437de6

File tree

2 files changed

+37
-3
lines changed

2 files changed

+37
-3
lines changed

src/services/completions.ts

Lines changed: 12 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2847,7 +2847,7 @@ function getCompletionEntryCodeActionsAndSourceDisplay(
28472847
cancellationToken: CancellationToken,
28482848
): CodeActionsAndSourceDisplay {
28492849
if (data?.moduleSpecifier) {
2850-
if (previousToken && getImportStatementCompletionInfo(contextToken || previousToken).replacementSpan) {
2850+
if (previousToken && getImportStatementCompletionInfo(contextToken || previousToken, sourceFile).replacementSpan) {
28512851
// Import statement completion: 'import c|'
28522852
return { codeActions: undefined, sourceDisplay: [textPart(data.moduleSpecifier)] };
28532853
}
@@ -3176,7 +3176,7 @@ function getCompletionData(
31763176
let flags = CompletionInfoFlags.None;
31773177

31783178
if (contextToken) {
3179-
const importStatementCompletionInfo = getImportStatementCompletionInfo(contextToken);
3179+
const importStatementCompletionInfo = getImportStatementCompletionInfo(contextToken, sourceFile);
31803180
if (importStatementCompletionInfo.keywordCompletion) {
31813181
if (importStatementCompletionInfo.isKeywordOnlyCompletion) {
31823182
return {
@@ -5469,7 +5469,7 @@ export interface ImportStatementCompletionInfo {
54695469
replacementSpan: TextSpan | undefined;
54705470
}
54715471

5472-
function getImportStatementCompletionInfo(contextToken: Node): ImportStatementCompletionInfo {
5472+
function getImportStatementCompletionInfo(contextToken: Node, sourceFile: SourceFile): ImportStatementCompletionInfo {
54735473
let keywordCompletion: TokenSyntaxKind | undefined;
54745474
let isKeywordOnlyCompletion = false;
54755475
const candidate = getCandidate();
@@ -5485,6 +5485,15 @@ function getImportStatementCompletionInfo(contextToken: Node): ImportStatementCo
54855485
function getCandidate() {
54865486
const parent = contextToken.parent;
54875487
if (isImportEqualsDeclaration(parent)) {
5488+
// import Foo |
5489+
// import Foo f|
5490+
const lastToken = parent.getLastToken(sourceFile);
5491+
if (isIdentifier(contextToken) && lastToken !== contextToken) {
5492+
keywordCompletion = SyntaxKind.FromKeyword;
5493+
isKeywordOnlyCompletion = true;
5494+
return undefined;
5495+
}
5496+
54885497
keywordCompletion = contextToken.kind === SyntaxKind.TypeKeyword ? undefined : SyntaxKind.TypeKeyword;
54895498
return isModuleSpecifierMissingOrEmpty(parent.moduleReference) ? parent : undefined;
54905499
}
Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
/// <reference path="fourslash.ts" />
2+
3+
// @allowJs: true
4+
5+
// @Filename: /a.js
6+
////import Foo /*a*/
7+
////
8+
////function fromBar() {}
9+
10+
// @Filename: /b.jsx
11+
////import Foo /*b*/
12+
////
13+
////function fromBar() {}
14+
15+
verify.completions({
16+
marker: ["a", "b"],
17+
exact: {
18+
name: "from",
19+
sortText: completion.SortText.GlobalsOrKeywords,
20+
},
21+
preferences: {
22+
includeCompletionsForImportStatements: true,
23+
includeInsertTextCompletions: true,
24+
},
25+
});

0 commit comments

Comments
 (0)