Skip to content

Commit adf3635

Browse files
author
Andy
authored
For import fix, for "foo/index" module, use "foo" as default export name, not "index" (microsoft#22651)
1 parent 3b6ae85 commit adf3635

2 files changed

Lines changed: 17 additions & 4 deletions

File tree

src/services/codefixes/importFixes.ts

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -802,21 +802,22 @@ namespace ts.codefix {
802802
}
803803

804804
export function moduleSymbolToValidIdentifier(moduleSymbol: Symbol, target: ScriptTarget): string {
805-
return moduleSpecifierToValidIdentifier(removeFileExtension(getBaseFileName(moduleSymbol.name)), target);
805+
return moduleSpecifierToValidIdentifier(removeFileExtension(stripQuotes(moduleSymbol.name)), target);
806806
}
807807

808808
export function moduleSpecifierToValidIdentifier(moduleSpecifier: string, target: ScriptTarget): string {
809+
const baseName = getBaseFileName(removeSuffix(moduleSpecifier, "/index"));
809810
let res = "";
810811
let lastCharWasValid = true;
811-
const firstCharCode = moduleSpecifier.charCodeAt(0);
812+
const firstCharCode = baseName.charCodeAt(0);
812813
if (isIdentifierStart(firstCharCode, target)) {
813814
res += String.fromCharCode(firstCharCode);
814815
}
815816
else {
816817
lastCharWasValid = false;
817818
}
818-
for (let i = 1; i < moduleSpecifier.length; i++) {
819-
const ch = moduleSpecifier.charCodeAt(i);
819+
for (let i = 1; i < baseName.length; i++) {
820+
const ch = baseName.charCodeAt(i);
820821
const isValid = isIdentifierPart(ch, target);
821822
if (isValid) {
822823
let char = String.fromCharCode(ch);
Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
/// <reference path="fourslash.ts" />
2+
3+
// @Filename: /foo-bar/index.ts
4+
////export default 0;
5+
6+
// @Filename: /b.ts
7+
////[|foo/**/Bar|]
8+
9+
goTo.file("/b.ts");
10+
verify.importFixAtPosition([`import fooBar from "./foo-bar";
11+
12+
fooBar`]);

0 commit comments

Comments
 (0)