From fe22c2b0b6852a4808d8f5477ec84df2c29a5103 Mon Sep 17 00:00:00 2001 From: Andrew Scott Date: Thu, 6 May 2021 16:35:42 -0700 Subject: [PATCH] fix(language-service): Correct rename info for pipe name expressions (#41974) Prior to this PR, attempting to get rename info for pipe name expressions would defer to the typescript language service, which would return no rename info. This was not caught because the test was written incorrectly. This PR corrects the test behavior and adjusts the logic in getting rename info to account for indirect renames (like pipe names). PR Close #41974 --- .../ivy/references_and_rename.ts | 24 ++++++++++++++++++- .../ivy/test/references_and_rename_spec.ts | 11 +++++---- 2 files changed, 30 insertions(+), 5 deletions(-) diff --git a/packages/language-service/ivy/references_and_rename.ts b/packages/language-service/ivy/references_and_rename.ts index d4e16baa2c..74b85be19f 100644 --- a/packages/language-service/ivy/references_and_rename.ts +++ b/packages/language-service/ivy/references_and_rename.ts @@ -154,7 +154,29 @@ export class RenameBuilder { // We could not get a template at position so we assume the request came from outside the // template. if (templateInfo === undefined) { - return this.tsLS.getRenameInfo(filePath, position); + const renameRequest = this.buildRenameRequestAtTypescriptPosition(filePath, position); + if (renameRequest === null) { + return { + canRename: false, + localizedErrorMessage: 'Could not determine rename info at typescript position.', + }; + } + if (renameRequest.type === RequestKind.PipeName) { + const pipeName = renameRequest.pipeNameExpr.text; + return { + canRename: true, + displayName: pipeName, + fullDisplayName: pipeName, + triggerSpan: { + length: pipeName.length, + // Offset the pipe name by 1 to account for start of string '/`/" + start: renameRequest.pipeNameExpr.getStart() + 1, + }, + }; + } else { + // TODO(atscott): Add support for other special indirect renames from typescript files. + return this.tsLS.getRenameInfo(filePath, position); + } } const allTargetDetails = getTargetDetailsAtTemplatePosition(templateInfo, position, this.ttc); diff --git a/packages/language-service/ivy/test/references_and_rename_spec.ts b/packages/language-service/ivy/test/references_and_rename_spec.ts index 64f4d2be22..a8a994cbf3 100644 --- a/packages/language-service/ivy/test/references_and_rename_spec.ts +++ b/packages/language-service/ivy/test/references_and_rename_spec.ts @@ -809,20 +809,23 @@ describe('find references and rename locations', () => { birthday = ''; } `, - 'prefix-pipe.ts': prefixPipe + 'prefix_pipe.ts': prefixPipe }; env = LanguageServiceTestEnv.setup(); const project = createModuleAndProjectWithDeclarations(env, 'test', files); - const file = project.openFile('app.ts'); - file.moveCursorToText('prefi¦xPipe:'); + const file = project.openFile('prefix_pipe.ts'); + file.moveCursorToText(`'prefi¦xPipe'`); const renameLocations = getRenameLocationsAtPosition(file)!; expect(renameLocations.length).toBe(2); - assertFileNames(renameLocations, ['prefix-pipe.ts', 'app.ts']); + assertFileNames(renameLocations, ['prefix_pipe.ts', 'app.ts']); assertTextSpans(renameLocations, ['prefixPipe']); const result = file.getRenameInfo() as ts.RenameInfoSuccess; expect(result.canRename).toEqual(true); expect(result.displayName).toEqual('prefixPipe'); + expect(file.contents.substring( + result.triggerSpan.start, result.triggerSpan.start + result.triggerSpan.length)) + .toBe('prefixPipe'); }); it('finds rename locations in base class', () => {