-
-
Notifications
You must be signed in to change notification settings - Fork 207
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
(fix) prevent duplicate import updates (#1466)
The TS plugin and the Svelte extension want to update the same locations after a file move/rename in some cases; prevent that #1461
- Loading branch information
1 parent
59b6bbf
commit bb58b0c
Showing
5 changed files
with
76 additions
and
8 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
32 changes: 32 additions & 0 deletions
32
packages/typescript-plugin/src/language-service/update-imports.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,32 @@ | ||
import type ts from 'typescript/lib/tsserverlibrary'; | ||
import { Logger } from '../logger'; | ||
import { SvelteSnapshotManager } from '../svelte-snapshots'; | ||
import { isSvelteFilePath } from '../utils'; | ||
|
||
export function decorateUpdateImports( | ||
ls: ts.LanguageService, | ||
snapshotManager: SvelteSnapshotManager, | ||
logger: Logger | ||
): void { | ||
const getEditsForFileRename = ls.getEditsForFileRename; | ||
ls.getEditsForFileRename = (oldFilePath, newFilePath, formatOptions, preferences) => { | ||
const renameLocations = getEditsForFileRename( | ||
oldFilePath, | ||
newFilePath, | ||
formatOptions, | ||
preferences | ||
); | ||
// If a file move/rename of a TS/JS file results a Svelte file change, | ||
// the Svelte extension will notice that, too, and adjusts the same imports. | ||
// This results in duplicate adjustments or race conditions with conflicting text spans | ||
// which can break imports in some cases. | ||
// Therefore don't do any updates of Svelte files and and also no updates of mixed TS files | ||
// and let the Svelte extension handle that. | ||
return renameLocations?.filter((renameLocation) => { | ||
return ( | ||
!isSvelteFilePath(renameLocation.fileName) && | ||
!renameLocation.textChanges.some((change) => change.newText.endsWith('.svelte')) | ||
); | ||
}); | ||
}; | ||
} |