-
Notifications
You must be signed in to change notification settings - Fork 12.5k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Don't go past import in cross-project renaming #48758
Changes from 5 commits
9acc88f
6804b7a
3242950
36af929
e0343f5
17ee2e1
a6f2e0f
2d01675
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,6 +1,6 @@ | ||
/* @internal */ | ||
namespace ts.GoToDefinition { | ||
export function getDefinitionAtPosition(program: Program, sourceFile: SourceFile, position: number, searchOtherFilesOnly?: boolean): readonly DefinitionInfo[] | undefined { | ||
export function getDefinitionAtPosition(program: Program, sourceFile: SourceFile, position: number, searchOtherFilesOnly?: boolean, skipAlias = true): readonly DefinitionInfo[] | undefined { | ||
const resolvedRef = getReferenceAtPosition(sourceFile, position, program); | ||
const fileReferenceDefinition = resolvedRef && [getDefinitionInfoForFileReference(resolvedRef.reference.fileName, resolvedRef.fileName, resolvedRef.unverified)] || emptyArray; | ||
if (resolvedRef?.file) { | ||
|
@@ -28,7 +28,7 @@ namespace ts.GoToDefinition { | |
|
||
if (isStaticModifier(node) && isClassStaticBlockDeclaration(node.parent)) { | ||
const classDecl = node.parent.parent; | ||
const { symbol, failedAliasResolution } = getSymbol(classDecl, typeChecker); | ||
const { symbol, failedAliasResolution } = getSymbol(classDecl, typeChecker, skipAlias); | ||
|
||
const staticBlocks = filter(classDecl.members, isClassStaticBlockDeclaration); | ||
const containerName = symbol ? typeChecker.symbolToString(symbol, classDecl) : ""; | ||
|
@@ -40,15 +40,15 @@ namespace ts.GoToDefinition { | |
}); | ||
} | ||
|
||
let { symbol, failedAliasResolution } = getSymbol(node, typeChecker); | ||
let { symbol, failedAliasResolution } = getSymbol(node, typeChecker, skipAlias); | ||
let fallbackNode = node; | ||
|
||
if (searchOtherFilesOnly && failedAliasResolution) { | ||
// We couldn't resolve the specific import, try on the module specifier. | ||
const importDeclaration = forEach([node, ...symbol?.declarations || emptyArray], n => findAncestor(n, isAnyImportOrBareOrAccessedRequire)); | ||
const moduleSpecifier = importDeclaration && tryGetModuleSpecifierFromDeclaration(importDeclaration); | ||
if (moduleSpecifier) { | ||
({ symbol, failedAliasResolution } = getSymbol(moduleSpecifier, typeChecker)); | ||
({ symbol, failedAliasResolution } = getSymbol(moduleSpecifier, typeChecker, skipAlias)); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. @andrewbranch I'd like you to take a look at this case and the interaction between There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
|
||
fallbackNode = moduleSpecifier; | ||
} | ||
} | ||
|
@@ -235,7 +235,7 @@ namespace ts.GoToDefinition { | |
return definitionFromType(typeChecker.getTypeAtLocation(node.parent), typeChecker, node.parent, /*failedAliasResolution*/ false); | ||
} | ||
|
||
const { symbol, failedAliasResolution } = getSymbol(node, typeChecker); | ||
const { symbol, failedAliasResolution } = getSymbol(node, typeChecker, /*skipAlias*/ true); | ||
if (!symbol) return undefined; | ||
|
||
const typeAtLocation = typeChecker.getTypeOfSymbolAtLocation(symbol, node); | ||
|
@@ -292,14 +292,14 @@ namespace ts.GoToDefinition { | |
return mapDefined(checker.getIndexInfosAtLocation(node), info => info.declaration && createDefinitionFromSignatureDeclaration(checker, info.declaration)); | ||
} | ||
|
||
function getSymbol(node: Node, checker: TypeChecker) { | ||
function getSymbol(node: Node, checker: TypeChecker, skipAlias: boolean) { | ||
const symbol = checker.getSymbolAtLocation(node); | ||
// If this is an alias, and the request came at the declaration location | ||
// get the aliased symbol instead. This allows for goto def on an import e.g. | ||
// import {A, B} from "mod"; | ||
// to jump to the implementation directly. | ||
let failedAliasResolution = false; | ||
if (symbol?.declarations && symbol.flags & SymbolFlags.Alias && shouldSkipAlias(node, symbol.declarations[0])) { | ||
if (symbol?.declarations && symbol.flags & SymbolFlags.Alias && skipAlias && shouldSkipAlias(node, symbol.declarations[0])) { | ||
const aliased = checker.getAliasedSymbol(symbol); | ||
if (aliased.declarations) { | ||
return { symbol: aliased }; | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -474,7 +474,7 @@ namespace ts { | |
|
||
/*@internal*/ | ||
// eslint-disable-next-line @typescript-eslint/unified-signatures | ||
getDefinitionAtPosition(fileName: string, position: number, searchOtherFilesOnly: boolean): readonly DefinitionInfo[] | undefined; | ||
getDefinitionAtPosition(fileName: string, position: number, searchOtherFilesOnly: boolean, skipAlias: boolean): readonly DefinitionInfo[] | undefined; | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I'm not sure this is the best way to add this new parameter, open to feedback on this There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Small nit but I think I would make it |
||
getDefinitionAtPosition(fileName: string, position: number): readonly DefinitionInfo[] | undefined; | ||
getDefinitionAndBoundSpan(fileName: string, position: number): DefinitionInfoAndBoundSpan | undefined; | ||
getTypeDefinitionAtPosition(fileName: string, position: number): readonly DefinitionInfo[] | undefined; | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,4 @@ | ||
/*====== /src/index.ts ======*/ | ||
|
||
import { [|RENAME|] } from '../lib/index'; | ||
RENAME; |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,4 @@ | ||
/*====== /src/index.ts ======*/ | ||
|
||
import * as [|RENAME|] from '../lib/index'; | ||
RENAME.someExportedVariable; |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,22 @@ | ||
/// <reference path='../fourslash.ts' /> | ||
|
||
// @Filename: /lib/tsconfig.json | ||
//// {} | ||
|
||
// @Filename: /lib/index.ts | ||
//// const unrelatedLocalVariable = 123; | ||
//// export const someExportedVariable = unrelatedLocalVariable; | ||
|
||
// @Filename: /src/tsconfig.json | ||
//// {} | ||
|
||
// @Filename: /src/index.ts | ||
//// import { /*i*/someExportedVariable } from '../lib/index'; | ||
gabritto marked this conversation as resolved.
Show resolved
Hide resolved
|
||
//// someExportedVariable; | ||
|
||
// @Filename: /tsconfig.json | ||
//// {} | ||
|
||
goTo.file("/lib/index.ts"); | ||
goTo.file("/src/index.ts"); | ||
verify.baselineRename("i", { providePrefixAndSuffixTextForRename: true }); |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,22 @@ | ||
/// <reference path='../fourslash.ts' /> | ||
|
||
// @Filename: /lib/tsconfig.json | ||
//// {} | ||
|
||
// @Filename: /lib/index.ts | ||
//// const unrelatedLocalVariable = 123; | ||
//// export const someExportedVariable = unrelatedLocalVariable; | ||
|
||
// @Filename: /src/tsconfig.json | ||
//// {} | ||
|
||
// @Filename: /src/index.ts | ||
//// import * as /*i*/lib from '../lib/index'; | ||
gabritto marked this conversation as resolved.
Show resolved
Hide resolved
|
||
//// lib.someExportedVariable; | ||
|
||
// @Filename: /tsconfig.json | ||
//// {} | ||
|
||
goTo.file("/lib/index.ts"); | ||
goTo.file("/src/index.ts"); | ||
verify.baselineRename("i", {}); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
that's where the logic changed: if we're renaming, we don't skip past aliases when getting the definition, because we want to rename the alias itself (I think), and not the original thing (e.g. we don't want to rename the original variable, just its import)