Skip to content

Commit

Permalink
fix: goToDefinition.ts rebase error
Browse files Browse the repository at this point in the history
  • Loading branch information
patroza committed Oct 10, 2023
1 parent 21bffb7 commit 6f5d25e
Showing 1 changed file with 40 additions and 22 deletions.
62 changes: 40 additions & 22 deletions src/services/goToDefinition.ts
Expand Up @@ -22,6 +22,7 @@ import {
findAncestor,
first,
flatMap,
forEach,
FunctionLikeDeclaration,
getAssignmentDeclarationKind,
getContainingObjectLiteralElement,
Expand All @@ -41,6 +42,7 @@ import {
hasEffectiveModifier,
hasInitializer,
hasStaticModifier,
isAnyImportOrBareOrAccessedRequire,
isAssignmentDeclaration,
isAssignmentExpression,
isBinaryExpression,
Expand Down Expand Up @@ -103,6 +105,7 @@ import {
textRangeContainsPositionInclusive,
TextSpan,
tryCast,
tryGetModuleSpecifierFromDeclaration,
TsPlusSymbolTag,
Type,
TypeChecker,
Expand Down Expand Up @@ -172,13 +175,12 @@ export function getDefinitionAtPosition(program: Program, sourceFile: SourceFile
// TSPLUS EXTENSION BEGIN
let symbol: Symbol | undefined;
let failedAliasResolution: boolean | undefined;
let fallbackNode = node;

const nodeSymbol = typeChecker.getSymbolAtLocation(node)
if (!nodeSymbol) {
if (isPropertyAccessExpression(parent)) {
const nodeType = typeChecker.getTypeAtLocation(node);
if(nodeType.symbol && isTsPlusSymbol(nodeType.symbol)) {
if (nodeType.symbol && isTsPlusSymbol(nodeType.symbol)) {
if (parent.parent && isCallExpression(parent.parent) && parent.parent.expression === parent) {
const declaration = getDeclarationForTsPlus(typeChecker, parent.parent, nodeType.symbol)
if (declaration) {
Expand All @@ -196,14 +198,14 @@ export function getDefinitionAtPosition(program: Program, sourceFile: SourceFile
const type = typeChecker.getTypeAtLocation(parent.expression);
const extensions = typeChecker.getExtensions(parent.expression);

if(extensions) {
if (extensions) {
const name = parent.name.escapedText.toString();
const staticValueSymbol = typeChecker.getStaticExtension(type, name);
if(staticValueSymbol) {
if (staticValueSymbol) {
// If execution gets here, it means we have a static variable extension,
// which needs to be treated a little differently
const declaration = staticValueSymbol.patched.valueDeclaration;
if(declaration && declaration.original) {
if (declaration && declaration.original) {
symbol = (declaration.original as Declaration).symbol;
}
} else {
Expand All @@ -229,22 +231,38 @@ export function getDefinitionAtPosition(program: Program, sourceFile: SourceFile
}
}

if (!symbol && isModuleSpecifierLike(fallbackNode)) {
// We couldn't resolve the module specifier as an external module, but it could
// be that module resolution succeeded but the target was not a module.
const ref = program.getResolvedModule(sourceFile, fallbackNode.text, getModeForUsageLocation(sourceFile, fallbackNode))?.resolvedModule;
if (ref) {
return [{
name: fallbackNode.text,
fileName: ref.resolvedFileName,
containerName: undefined!,
containerKind: undefined!,
kind: ScriptElementKind.scriptElement,
textSpan: createTextSpan(0, 0),
failedAliasResolution,
isAmbient: isDeclarationFileName(ref.resolvedFileName),
unverified: fallbackNode !== node,
}];
if (!symbol) {
({ symbol, failedAliasResolution } = getSymbol(node, typeChecker, stopAtAlias));

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, stopAtAlias));
fallbackNode = moduleSpecifier;
}
}

if (!symbol && isModuleSpecifierLike(fallbackNode)) {
// We couldn't resolve the module specifier as an external module, but it could
// be that module resolution succeeded but the target was not a module.
const ref = program.getResolvedModule(sourceFile, fallbackNode.text, getModeForUsageLocation(sourceFile, fallbackNode))?.resolvedModule;
if (ref) {
return [{
name: fallbackNode.text,
fileName: ref.resolvedFileName,
containerName: undefined!,
containerKind: undefined!,
kind: ScriptElementKind.scriptElement,
textSpan: createTextSpan(0, 0),
failedAliasResolution,
isAmbient: isDeclarationFileName(ref.resolvedFileName),
unverified: fallbackNode !== node,
}];
}
}
}
// TSPLUS EXTENSION END
Expand Down Expand Up @@ -512,7 +530,7 @@ export function getTypeDefinitionAtPosition(typeChecker: TypeChecker, sourceFile

return typeDefinitions.length ? [...getFirstTypeArgumentDefinitions(typeChecker, resolvedType, node, failedAliasResolution), ...typeDefinitions]
: !(symbol.flags & SymbolFlags.Value) && symbol.flags & SymbolFlags.Type ? getDefinitionFromSymbol(typeChecker, skipAlias(symbol, typeChecker), node, failedAliasResolution)
: undefined;
: undefined;
}

function definitionFromType(type: Type, checker: TypeChecker, node: Node, failedAliasResolution: boolean | undefined): readonly DefinitionInfo[] {
Expand Down

0 comments on commit 6f5d25e

Please sign in to comment.