Skip to content

Commit

Permalink
Fix crash in goto-def on @override (#51016)
Browse files Browse the repository at this point in the history
* Fix crash in goto-def on `@override`

When the base type is not defined, getDefinitionFromOverriddenMember
will have its type as errorType, which has no symbol. The error handling
previously only handled the case of no baseType at all -- which I'm not
sure ever actually happens.

* Improve checking

1. getTypeAtLocation never returns undefined, only errorType, so check for that.
2. Return directly after missing baseTypeNode instead of continuing to return later.

* Experiment with making goto-def on `override` more consistent

* Unify static/instance node->symbol->type path

* Make getSymbolAtLocation support class expressions

and parenthesized expressions

* Revert "Make getSymbolAtLocation support class expressions"

This reverts commit 4c1b031.

* fix semicolon lint
  • Loading branch information
sandersn committed Oct 3, 2022
1 parent 7dcf11f commit 299745c
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 4 deletions.
10 changes: 6 additions & 4 deletions src/services/goToDefinition.ts
Expand Up @@ -171,13 +171,15 @@ namespace ts.GoToDefinition {
if (!baseDeclaration) return;

const baseTypeNode = getEffectiveBaseTypeNode(baseDeclaration);
const baseType = baseTypeNode ? typeChecker.getTypeAtLocation(baseTypeNode) : undefined;
if (!baseType) return;
if (!baseTypeNode) return;
const expression = skipParentheses(baseTypeNode.expression);
const base = isClassExpression(expression) ? expression.symbol : typeChecker.getSymbolAtLocation(expression);
if (!base) return;

const name = unescapeLeadingUnderscores(getTextOfPropertyName(classElement.name));
const symbol = hasStaticModifier(classElement)
? typeChecker.getPropertyOfType(typeChecker.getTypeOfSymbolAtLocation(baseType.symbol, baseDeclaration), name)
: typeChecker.getPropertyOfType(baseType, name);
? typeChecker.getPropertyOfType(typeChecker.getTypeOfSymbol(base), name)
: typeChecker.getPropertyOfType(typeChecker.getDeclaredTypeOfSymbol(base), name);
if (!symbol) return;

return getDefinitionFromSymbol(typeChecker, symbol, node);
Expand Down
16 changes: 16 additions & 0 deletions tests/cases/fourslash/goToDefinitionOverriddenMember16.ts
@@ -0,0 +1,16 @@
/// <reference path="fourslash.ts" />
// @Filename: goToDefinitionOverrideJsdoc.ts
// @allowJs: true
// @checkJs: true

//// export class C extends CompletelyUndefined {
//// /**
//// * @override/*1*/
//// * @returns {{}}
//// */
//// static foo() {
//// return {}
//// }
//// }

verify.goToDefinition(['1'], [])

0 comments on commit 299745c

Please sign in to comment.