mirror of
https://github.com/microsoft/TypeScript.git
synced 2025-11-18 17:21:48 +00:00
For import completion of default import, convert module name to identifier (#19875)
* For import completion of default import, convert module name to identifier * Suggestions from code review
This commit is contained in:
@@ -699,9 +699,10 @@ namespace ts.codefix {
|
||||
const defaultExport = checker.tryGetMemberInModuleExports("default", moduleSymbol);
|
||||
if (defaultExport) {
|
||||
const localSymbol = getLocalSymbolForExportDefault(defaultExport);
|
||||
if (localSymbol && localSymbol.escapedName === symbolName && checkSymbolHasMeaning(localSymbol, currentTokenMeaning)) {
|
||||
if ((localSymbol && localSymbol.escapedName === symbolName || moduleSymbolToValidIdentifier(moduleSymbol, context.compilerOptions.target) === symbolName)
|
||||
&& checkSymbolHasMeaning(localSymbol || defaultExport, currentTokenMeaning)) {
|
||||
// check if this symbol is already used
|
||||
const symbolId = getUniqueSymbolId(localSymbol, checker);
|
||||
const symbolId = getUniqueSymbolId(localSymbol || defaultExport, checker);
|
||||
symbolIdActionMap.addActions(symbolId, getCodeActionForImport(moduleSymbol, { ...context, kind: ImportKind.Default }));
|
||||
}
|
||||
}
|
||||
@@ -731,4 +732,35 @@ namespace ts.codefix {
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
export function moduleSymbolToValidIdentifier(moduleSymbol: Symbol, target: ScriptTarget): string {
|
||||
return moduleSpecifierToValidIdentifier(removeFileExtension(getBaseFileName(moduleSymbol.name)), target);
|
||||
}
|
||||
|
||||
function moduleSpecifierToValidIdentifier(moduleSpecifier: string, target: ScriptTarget): string {
|
||||
let res = "";
|
||||
let lastCharWasValid = true;
|
||||
const firstCharCode = moduleSpecifier.charCodeAt(0);
|
||||
if (isIdentifierStart(firstCharCode, target)) {
|
||||
res += String.fromCharCode(firstCharCode);
|
||||
}
|
||||
else {
|
||||
lastCharWasValid = false;
|
||||
}
|
||||
for (let i = 1; i < moduleSpecifier.length; i++) {
|
||||
const ch = moduleSpecifier.charCodeAt(i);
|
||||
const isValid = isIdentifierPart(ch, target);
|
||||
if (isValid) {
|
||||
let char = String.fromCharCode(ch);
|
||||
if (!lastCharWasValid) {
|
||||
char = char.toUpperCase();
|
||||
}
|
||||
res += char;
|
||||
}
|
||||
lastCharWasValid = isValid;
|
||||
}
|
||||
// Need `|| "_"` to ensure result isn't empty.
|
||||
const token = stringToToken(res);
|
||||
return token === undefined || !isNonContextualKeyword(token) ? res || "_" : `_${res}`;
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user