diff --git a/src/services/findAllReferences.ts b/src/services/findAllReferences.ts index 043856e4ab173..555fcd70e6fc8 100644 --- a/src/services/findAllReferences.ts +++ b/src/services/findAllReferences.ts @@ -83,8 +83,7 @@ namespace ts.FindAllReferences { } } - export function findReferencedEntries(program: Program, cancellationToken: CancellationToken, sourceFiles: ReadonlyArray, sourceFile: SourceFile, position: number, options?: Options): ReferenceEntry[] | undefined { - const node = getTouchingPropertyName(sourceFile, position); + export function findReferencedEntries(program: Program, cancellationToken: CancellationToken, sourceFiles: ReadonlyArray, node: Node, position: number, options: Options | undefined): ReferenceEntry[] | undefined { return map(flattenEntries(Core.getReferencedSymbolsForNode(position, node, program, sourceFiles, cancellationToken, options)), toReferenceEntry); } diff --git a/src/services/services.ts b/src/services/services.ts index d07f703470466..a42efb92ab611 100644 --- a/src/services/services.ts +++ b/src/services/services.ts @@ -1697,15 +1697,25 @@ namespace ts { } function findRenameLocations(fileName: string, position: number, findInStrings: boolean, findInComments: boolean): RenameLocation[] | undefined { - const refs = getReferences(fileName, position, { findInStrings, findInComments, isForRename: true }); - return refs && refs.map(({ fileName, textSpan }): RenameLocation => ({ fileName, textSpan })); + synchronizeHostData(); + const sourceFile = getValidSourceFile(fileName); + const node = getTouchingPropertyName(sourceFile, position); + if (isIdentifier(node) && isJsxOpeningElement(node.parent) || isJsxClosingElement(node.parent)) { + const { openingElement, closingElement } = node.parent.parent; + return [openingElement, closingElement].map((node): RenameLocation => ({ fileName: sourceFile.fileName, textSpan: createTextSpanFromNode(node.tagName, sourceFile) })); + } + else { + const refs = getReferences(node, position, { findInStrings, findInComments, isForRename: true }); + return refs && refs.map(({ fileName, textSpan }): RenameLocation => ({ fileName, textSpan })); + } } function getReferencesAtPosition(fileName: string, position: number): ReferenceEntry[] | undefined { - return getReferences(fileName, position)!; + synchronizeHostData(); + return getReferences(getTouchingPropertyName(getValidSourceFile(fileName), position), position); } - function getReferences(fileName: string, position: number, options?: FindAllReferences.Options): ReferenceEntry[] | undefined { + function getReferences(node: Node, position: number, options?: FindAllReferences.Options): ReferenceEntry[] | undefined { synchronizeHostData(); // Exclude default library when renaming as commonly user don't want to change that file. @@ -1713,7 +1723,7 @@ namespace ts { ? program.getSourceFiles().filter(sourceFile => !program.isSourceFileDefaultLibrary(sourceFile)) : program.getSourceFiles(); - return FindAllReferences.findReferencedEntries(program, cancellationToken, sourceFiles, getValidSourceFile(fileName), position, options); + return FindAllReferences.findReferencedEntries(program, cancellationToken, sourceFiles, node, position, options); } function findReferences(fileName: string, position: number): ReferencedSymbol[] | undefined { diff --git a/tests/baselines/reference/api/tsserverlibrary.d.ts b/tests/baselines/reference/api/tsserverlibrary.d.ts index 8fdee881535dc..a2325729a9967 100644 --- a/tests/baselines/reference/api/tsserverlibrary.d.ts +++ b/tests/baselines/reference/api/tsserverlibrary.d.ts @@ -11112,7 +11112,7 @@ declare namespace ts.FindAllReferences { } function findReferencedSymbols(program: Program, cancellationToken: CancellationToken, sourceFiles: ReadonlyArray, sourceFile: SourceFile, position: number): ReferencedSymbol[] | undefined; function getImplementationsAtPosition(program: Program, cancellationToken: CancellationToken, sourceFiles: ReadonlyArray, sourceFile: SourceFile, position: number): ImplementationLocation[] | undefined; - function findReferencedEntries(program: Program, cancellationToken: CancellationToken, sourceFiles: ReadonlyArray, sourceFile: SourceFile, position: number, options?: Options): ReferenceEntry[] | undefined; + function findReferencedEntries(program: Program, cancellationToken: CancellationToken, sourceFiles: ReadonlyArray, node: Node, position: number, options: Options | undefined): ReferenceEntry[] | undefined; function getReferenceEntriesForNode(position: number, node: Node, program: Program, sourceFiles: ReadonlyArray, cancellationToken: CancellationToken, options?: Options, sourceFilesSet?: ReadonlyMap): Entry[] | undefined; function toHighlightSpan(entry: Entry): { fileName: string; diff --git a/tests/cases/fourslash/tsxRename4.ts b/tests/cases/fourslash/tsxRename4.ts index a7e57cca258fb..ac25b04800841 100644 --- a/tests/cases/fourslash/tsxRename4.ts +++ b/tests/cases/fourslash/tsxRename4.ts @@ -1,19 +1,15 @@ /// //@Filename: file.tsx -//// declare module JSX { -//// interface Element { } -//// interface IntrinsicElements { -//// } -//// interface ElementAttributesProperty { props } -//// } -//// class [|MyClass|] { -//// props: { -//// name?: string; -//// size?: number; -//// } +////declare module JSX { +//// interface Element {} +//// interface IntrinsicElements {} +////} +////class [|MyClass|] {} //// -//// -//// var x = <[|MyClass|] name='hello'>; +////<[|MyClass|]>; +////<[|MyClass|]/>; -verify.rangesAreRenameLocations(); +const [r0, r1, r2, r3] = test.ranges(); +verify.renameLocations([r0, r3], [r0, r1, r2, r3]); +verify.renameLocations([r1, r2], [r1, r2]);