Skip to content

Commit 0531e41

Browse files
committed
fix: improve auto-renaming on file rename when import name starts with upper-case
1 parent 44464cb commit 0531e41

File tree

1 file changed

+9
-2
lines changed

1 file changed

+9
-2
lines changed

typescript/src/decorateEditsForFileRename.ts

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,14 +7,21 @@ export default (proxy: ts.LanguageService, languageService: ts.LanguageService,
77
proxy.getEditsForFileRename = (oldFilePath, newFilePath, formatOptions, preferences) => {
88
let edits = languageService.getEditsForFileRename(oldFilePath, newFilePath, formatOptions, preferences)
99
if (c('renameImportNameOfFileRename')) {
10-
const predictedNameFromPath = (p: string) => camelCase(p.split(/[/\\]/g).pop()!.replace(/\..+/, ''))
10+
const predictedNameFromPath = (p: string) => {
11+
const input = p.split(/[/\\]/g).pop()!.replace(/\..+/, '')
12+
const transformed = camelCase(input)
13+
// eslint-disable-next-line @typescript-eslint/prefer-optional-chain
14+
const isFirstUppercase = input && input.startsWith(input[0]!.toUpperCase())
15+
return isFirstUppercase ? transformed[0]!.toUpperCase() + transformed.slice(1) : transformed
16+
}
1117
const oldPredictedName = predictedNameFromPath(oldFilePath)
1218
const newPredictedName = predictedNameFromPath(newFilePath)
1319
for (const edit of edits) {
1420
const possiblyAddRename = (identifier: ts.Identifier | undefined) => {
1521
if (identifier?.text !== oldPredictedName) return
1622
const sourceFile = languageService.getProgram()!.getSourceFile(edit.fileName)!
17-
const newRenameEdits = proxy.findRenameLocations(edit.fileName, identifier.pos, false, false, preferences ?? {}) ?? []
23+
const newRenameEdits =
24+
proxy.findRenameLocations(edit.fileName, identifier.pos + identifier.getLeadingTriviaWidth(), false, false, preferences ?? {}) ?? []
1825
if (!newRenameEdits) return
1926
// maybe cancel symbol rename on collision instead?
2027
const newInsertName = tsFull.getUniqueName(newPredictedName, sourceFile as any)

0 commit comments

Comments
 (0)