fix(54818): "from" keyword displaying incorrect completions (#54843)

This commit is contained in:
Oleksandr T 2023-06-30 22:06:11 +03:00 committed by GitHub
parent bd1e9752d2
commit a437de66b6
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 37 additions and 3 deletions

View File

@ -2847,7 +2847,7 @@ function getCompletionEntryCodeActionsAndSourceDisplay(
cancellationToken: CancellationToken,
): CodeActionsAndSourceDisplay {
if (data?.moduleSpecifier) {
if (previousToken && getImportStatementCompletionInfo(contextToken || previousToken).replacementSpan) {
if (previousToken && getImportStatementCompletionInfo(contextToken || previousToken, sourceFile).replacementSpan) {
// Import statement completion: 'import c|'
return { codeActions: undefined, sourceDisplay: [textPart(data.moduleSpecifier)] };
}
@ -3176,7 +3176,7 @@ function getCompletionData(
let flags = CompletionInfoFlags.None;
if (contextToken) {
const importStatementCompletionInfo = getImportStatementCompletionInfo(contextToken);
const importStatementCompletionInfo = getImportStatementCompletionInfo(contextToken, sourceFile);
if (importStatementCompletionInfo.keywordCompletion) {
if (importStatementCompletionInfo.isKeywordOnlyCompletion) {
return {
@ -5469,7 +5469,7 @@ export interface ImportStatementCompletionInfo {
replacementSpan: TextSpan | undefined;
}
function getImportStatementCompletionInfo(contextToken: Node): ImportStatementCompletionInfo {
function getImportStatementCompletionInfo(contextToken: Node, sourceFile: SourceFile): ImportStatementCompletionInfo {
let keywordCompletion: TokenSyntaxKind | undefined;
let isKeywordOnlyCompletion = false;
const candidate = getCandidate();
@ -5485,6 +5485,15 @@ function getImportStatementCompletionInfo(contextToken: Node): ImportStatementCo
function getCandidate() {
const parent = contextToken.parent;
if (isImportEqualsDeclaration(parent)) {
// import Foo |
// import Foo f|
const lastToken = parent.getLastToken(sourceFile);
if (isIdentifier(contextToken) && lastToken !== contextToken) {
keywordCompletion = SyntaxKind.FromKeyword;
isKeywordOnlyCompletion = true;
return undefined;
}
keywordCompletion = contextToken.kind === SyntaxKind.TypeKeyword ? undefined : SyntaxKind.TypeKeyword;
return isModuleSpecifierMissingOrEmpty(parent.moduleReference) ? parent : undefined;
}

View File

@ -0,0 +1,25 @@
/// <reference path="fourslash.ts" />
// @allowJs: true
// @Filename: /a.js
////import Foo /*a*/
////
////function fromBar() {}
// @Filename: /b.jsx
////import Foo /*b*/
////
////function fromBar() {}
verify.completions({
marker: ["a", "b"],
exact: {
name: "from",
sortText: completion.SortText.GlobalsOrKeywords,
},
preferences: {
includeCompletionsForImportStatements: true,
includeInsertTextCompletions: true,
},
});