@@ -58,7 +58,7 @@ export class RenameProviderImpl implements RenameProvider {
5858 const docs = new Map < string , SnapshotFragment > ( [ [ tsDoc . filePath , fragment ] ] ) ;
5959 let convertedRenameLocations : ( ts . RenameLocation & {
6060 range : Range ;
61- } ) [ ] = await this . mapRenameLocationsToParent ( renameLocations , docs ) ;
61+ } ) [ ] = await this . mapAndFilterRenameLocations ( renameLocations , docs ) ;
6262 // eslint-disable-next-line max-len
6363 const additionalRenameForPropRenameInsideComponentWithProp = await this . getAdditionLocationsForRenameOfPropInsideComponentWithProp (
6464 document ,
@@ -127,7 +127,7 @@ export class RenameProviderImpl implements RenameProvider {
127127 if (
128128 ! renameInfo . canRename ||
129129 renameInfo . kind === ts . ScriptElementKind . jsxAttribute ||
130- renameInfo . fullDisplayName ?. includes ( " JSX.IntrinsicElements" )
130+ renameInfo . fullDisplayName ?. includes ( ' JSX.IntrinsicElements' )
131131 ) {
132132 return null ;
133133 }
@@ -189,7 +189,7 @@ export class RenameProviderImpl implements RenameProvider {
189189 rename . fileName !== updatePropLocation . fileName ||
190190 this . isInSvelte2TsxPropLine ( fragment , rename ) ,
191191 ) ;
192- return await this . mapRenameLocationsToParent ( replacementsForProp , fragments ) ;
192+ return await this . mapAndFilterRenameLocations ( replacementsForProp , fragments ) ;
193193 }
194194
195195 /**
@@ -222,7 +222,7 @@ export class RenameProviderImpl implements RenameProvider {
222222 const idx = ( match . index || 0 ) + match [ 0 ] . lastIndexOf ( match [ 1 ] ) ;
223223 const replacementsForProp =
224224 lang . findRenameLocations ( updatePropLocation . fileName , idx , false , false ) || [ ] ;
225- return await this . mapRenameLocationsToParent ( replacementsForProp , fragments ) ;
225+ return await this . mapAndFilterRenameLocations ( replacementsForProp , fragments ) ;
226226 }
227227
228228 // --------> svelte2tsx?
@@ -278,12 +278,13 @@ export class RenameProviderImpl implements RenameProvider {
278278 * The rename locations the ts language services hands back are relative to the
279279 * svelte2tsx generated code -> map it back to the original document positions.
280280 * Some of those positions could be unmapped (line=-1), these are handled elsewhere.
281+ * Also filter out wrong renames.
281282 */
282- private async mapRenameLocationsToParent (
283+ private async mapAndFilterRenameLocations (
283284 renameLocations : readonly ts . RenameLocation [ ] ,
284285 fragments : Map < string , SnapshotFragment > ,
285286 ) : Promise < ( ts . RenameLocation & { range : Range } ) [ ] > {
286- return Promise . all (
287+ const mappedLocations = await Promise . all (
287288 renameLocations . map ( async ( loc ) => {
288289 let doc = fragments . get ( loc . fileName ) ;
289290 if ( ! doc ) {
@@ -297,6 +298,27 @@ export class RenameProviderImpl implements RenameProvider {
297298 } ;
298299 } ) ,
299300 ) ;
301+ return this . filterWrongRenameLocations ( mappedLocations ) ;
302+ }
303+
304+ private filterWrongRenameLocations (
305+ mappedLocations : ( ts . RenameLocation & { range : Range } ) [ ] ,
306+ ) : ( ts . RenameLocation & { range : Range } ) [ ] {
307+ return mappedLocations . filter ( ( loc ) => {
308+ const snapshot = this . getSnapshot ( loc . fileName ) ;
309+ if ( ! ( snapshot instanceof SvelteDocumentSnapshot ) ) {
310+ return true ;
311+ }
312+
313+ const content = snapshot . getText ( 0 , snapshot . getLength ( ) ) ;
314+ const svelteInstanceOfFn = '__sveltets_instanceOf(' ;
315+ // When the user renames a Svelte component, ts will also want to rename
316+ // `__sveltets_instanceOf(TheComponentToRename)`. Prevent that.
317+ return (
318+ content . lastIndexOf ( svelteInstanceOfFn , loc . textSpan . start ) !==
319+ loc . textSpan . start - svelteInstanceOfFn . length
320+ ) ;
321+ } ) ;
300322 }
301323
302324 private mapRangeToOriginal ( doc : SnapshotFragment , textSpan : ts . TextSpan ) : Range {
0 commit comments