refactor: improve string export name completions (#58818)

This commit is contained in:
Jack Works
2024-08-27 03:23:05 +08:00
committed by GitHub
parent e7410ce179
commit 47ae752c23
2 changed files with 38 additions and 15 deletions

View File

@@ -336,6 +336,7 @@ import {
rangeContainsPosition,
rangeContainsPositionExclusive,
rangeIsOnSingleLine,
scanner,
ScriptElementKind,
ScriptElementKindModifier,
ScriptTarget,
@@ -1889,9 +1890,16 @@ function createCompletionEntry(
if (parentNamedImportOrExport) {
const languageVersion = getEmitScriptTarget(host.getCompilationSettings());
if (!isIdentifierText(name, languageVersion)) {
insertText = JSON.stringify(name);
insertText = quotePropertyName(sourceFile, preferences, name);
if (parentNamedImportOrExport.kind === SyntaxKind.NamedImports) {
insertText += " as " + generateIdentifierForArbitraryString(name, languageVersion);
// check if it is import { ^here as name } from '...'
// we have to access the scanner here to check if it is { ^here as name } or { ^here, as, name }.
scanner.setText(sourceFile.text);
scanner.resetTokenState(position);
if (!(scanner.scan() === SyntaxKind.AsKeyword && scanner.scan() === SyntaxKind.Identifier)) {
insertText += " as " + generateIdentifierForArbitraryString(name, languageVersion);
}
}
}
else if (parentNamedImportOrExport.kind === SyntaxKind.NamedImports) {
@@ -4948,7 +4956,10 @@ function getCompletionData(
return !isFromObjectTypeDeclaration(contextToken);
case SyntaxKind.Identifier: {
if (containingNodeKind === SyntaxKind.ImportSpecifier &&
if ((
containingNodeKind === SyntaxKind.ImportSpecifier ||
containingNodeKind === SyntaxKind.ExportSpecifier
) &&
contextToken === (parent as ImportSpecifier).name &&
(contextToken as Identifier).text === "type"
) {