Skip to content

Commit

Permalink
fix(typescript_indexer): emit edges for methods that are 2+ levels aw…
Browse files Browse the repository at this point in the history
…ay (#5725)

This fixes the following scenario:

```typescript
class One { 
  doStuff() {}
}

class Two extends One {}

class Three extends Two {
  override doStuff() {}
}
```

Previously TS indexer didn't emit `overrides` edge between `doStuff` methods. Now TS indexer traverses all parent class chain until it finds a class/interface that contains method in question or until it reaches base class/interface.
  • Loading branch information
nbeloglazov committed Jun 30, 2023
1 parent 1ad4c6d commit 580ced8
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 8 deletions.
15 changes: 10 additions & 5 deletions kythe/typescript/indexer.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2012,16 +2012,21 @@ class Visitor {
const overriddenCondition = (sym: ts.Symbol) =>
Boolean(sym.flags & funcFlags) && sym.name === funcName;

// TODO(b/181591179): Remove this alias and casts
type AnyDuringTs42Migration = any;
const overridden: AnyDuringTs42Migration =
toArray(type.symbol.members.values() as AnyDuringTs42Migration)
.find(overriddenCondition as AnyDuringTs42Migration);
const overridden = toArray<ts.Symbol>(type.symbol.members.values())
.find(overriddenCondition);
if (overridden) {
const base = this.host.getSymbolName(overridden, TSNamespace.VALUE);
if (base) {
this.emitEdge(funcVName, EdgeKind.OVERRIDES, base);
}
} else {
// If parent class or interface doesn't have this method - it's possible
// that parent's parent might. To check for that recurse to the parent's parent
// classes/interfaces.
const decl = type.symbol.declarations?.[0];
if (decl && (ts.isClassLike(decl) || ts.isInterfaceDeclaration(decl))) {
this.emitOverridesEdgeForFunction(funcSym, funcVName, decl);
}
}
}
}
Expand Down
6 changes: 3 additions & 3 deletions kythe/typescript/testdata/class.ts
Original file line number Diff line number Diff line change
Expand Up @@ -86,18 +86,18 @@ class Class implements IFace {
class Subclass extends Class {
//- @method defines/binding OverridenMethod
//- OverridenMethod overrides Method
method() {
override method() {
//- @member ref Member
this.member;
}
}

class SubSubclass extends Class implements IExtended {
class SubSubclass extends Subclass implements IExtended {
//- @ifaceMethod defines/binding OverridenIFaceMethod
//- OverridenIFaceMethod overrides ClassIFaceMethod
//- OverridenIFaceMethod overrides ExtendedIFaceMethod
//- !{ OverridenIFaceMethod overrides IFaceMethod }
ifaceMethod() {}
override ifaceMethod() {}
}

//- @Class ref ClassCtor
Expand Down

0 comments on commit 580ced8

Please sign in to comment.