Skip to content

Commit c35cb86

Browse files
author
Andy
authored
When renaming at a tsx opening/closing tag, just rename the current element (#25273)
1 parent d957b1c commit c35cb86

File tree

4 files changed

+27
-22
lines changed

4 files changed

+27
-22
lines changed

src/services/findAllReferences.ts

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -83,8 +83,7 @@ namespace ts.FindAllReferences {
8383
}
8484
}
8585

86-
export function findReferencedEntries(program: Program, cancellationToken: CancellationToken, sourceFiles: ReadonlyArray<SourceFile>, sourceFile: SourceFile, position: number, options?: Options): ReferenceEntry[] | undefined {
87-
const node = getTouchingPropertyName(sourceFile, position);
86+
export function findReferencedEntries(program: Program, cancellationToken: CancellationToken, sourceFiles: ReadonlyArray<SourceFile>, node: Node, position: number, options: Options | undefined): ReferenceEntry[] | undefined {
8887
return map(flattenEntries(Core.getReferencedSymbolsForNode(position, node, program, sourceFiles, cancellationToken, options)), toReferenceEntry);
8988
}
9089

src/services/services.ts

Lines changed: 15 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1697,23 +1697,33 @@ namespace ts {
16971697
}
16981698

16991699
function findRenameLocations(fileName: string, position: number, findInStrings: boolean, findInComments: boolean): RenameLocation[] | undefined {
1700-
const refs = getReferences(fileName, position, { findInStrings, findInComments, isForRename: true });
1701-
return refs && refs.map(({ fileName, textSpan }): RenameLocation => ({ fileName, textSpan }));
1700+
synchronizeHostData();
1701+
const sourceFile = getValidSourceFile(fileName);
1702+
const node = getTouchingPropertyName(sourceFile, position);
1703+
if (isIdentifier(node) && isJsxOpeningElement(node.parent) || isJsxClosingElement(node.parent)) {
1704+
const { openingElement, closingElement } = node.parent.parent;
1705+
return [openingElement, closingElement].map((node): RenameLocation => ({ fileName: sourceFile.fileName, textSpan: createTextSpanFromNode(node.tagName, sourceFile) }));
1706+
}
1707+
else {
1708+
const refs = getReferences(node, position, { findInStrings, findInComments, isForRename: true });
1709+
return refs && refs.map(({ fileName, textSpan }): RenameLocation => ({ fileName, textSpan }));
1710+
}
17021711
}
17031712

17041713
function getReferencesAtPosition(fileName: string, position: number): ReferenceEntry[] | undefined {
1705-
return getReferences(fileName, position)!;
1714+
synchronizeHostData();
1715+
return getReferences(getTouchingPropertyName(getValidSourceFile(fileName), position), position);
17061716
}
17071717

1708-
function getReferences(fileName: string, position: number, options?: FindAllReferences.Options): ReferenceEntry[] | undefined {
1718+
function getReferences(node: Node, position: number, options?: FindAllReferences.Options): ReferenceEntry[] | undefined {
17091719
synchronizeHostData();
17101720

17111721
// Exclude default library when renaming as commonly user don't want to change that file.
17121722
const sourceFiles = options && options.isForRename
17131723
? program.getSourceFiles().filter(sourceFile => !program.isSourceFileDefaultLibrary(sourceFile))
17141724
: program.getSourceFiles();
17151725

1716-
return FindAllReferences.findReferencedEntries(program, cancellationToken, sourceFiles, getValidSourceFile(fileName), position, options);
1726+
return FindAllReferences.findReferencedEntries(program, cancellationToken, sourceFiles, node, position, options);
17171727
}
17181728

17191729
function findReferences(fileName: string, position: number): ReferencedSymbol[] | undefined {

tests/baselines/reference/api/tsserverlibrary.d.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11118,7 +11118,7 @@ declare namespace ts.FindAllReferences {
1111811118
}
1111911119
function findReferencedSymbols(program: Program, cancellationToken: CancellationToken, sourceFiles: ReadonlyArray<SourceFile>, sourceFile: SourceFile, position: number): ReferencedSymbol[] | undefined;
1112011120
function getImplementationsAtPosition(program: Program, cancellationToken: CancellationToken, sourceFiles: ReadonlyArray<SourceFile>, sourceFile: SourceFile, position: number): ImplementationLocation[] | undefined;
11121-
function findReferencedEntries(program: Program, cancellationToken: CancellationToken, sourceFiles: ReadonlyArray<SourceFile>, sourceFile: SourceFile, position: number, options?: Options): ReferenceEntry[] | undefined;
11121+
function findReferencedEntries(program: Program, cancellationToken: CancellationToken, sourceFiles: ReadonlyArray<SourceFile>, node: Node, position: number, options: Options | undefined): ReferenceEntry[] | undefined;
1112211122
function getReferenceEntriesForNode(position: number, node: Node, program: Program, sourceFiles: ReadonlyArray<SourceFile>, cancellationToken: CancellationToken, options?: Options, sourceFilesSet?: ReadonlyMap<true>): Entry[] | undefined;
1112311123
function toHighlightSpan(entry: Entry): {
1112411124
fileName: string;

tests/cases/fourslash/tsxRename4.ts

Lines changed: 10 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -1,19 +1,15 @@
11
/// <reference path='fourslash.ts' />
22

33
//@Filename: file.tsx
4-
//// declare module JSX {
5-
//// interface Element { }
6-
//// interface IntrinsicElements {
7-
//// }
8-
//// interface ElementAttributesProperty { props }
9-
//// }
10-
//// class [|MyClass|] {
11-
//// props: {
12-
//// name?: string;
13-
//// size?: number;
14-
//// }
4+
////declare module JSX {
5+
//// interface Element {}
6+
//// interface IntrinsicElements {}
7+
////}
8+
////class [|MyClass|] {}
159
////
16-
////
17-
//// var x = <[|MyClass|] name='hello'></[|MyClass|]>;
10+
////<[|MyClass|]></[|MyClass|]>;
11+
////<[|MyClass|]/>;
1812

19-
verify.rangesAreRenameLocations();
13+
const [r0, r1, r2, r3] = test.ranges();
14+
verify.renameLocations([r0, r3], [r0, r1, r2, r3]);
15+
verify.renameLocations([r1, r2], [r1, r2]);

0 commit comments

Comments
 (0)