Skip to content

Commit

Permalink
fix: getDerivedClasses() isn't correct in some cases (#1557)
Browse files Browse the repository at this point in the history
  • Loading branch information
jmyrick02 authored Oct 5, 2024
1 parent 308ae4c commit 0d38985
Show file tree
Hide file tree
Showing 2 changed files with 12 additions and 1 deletion.
Original file line number Diff line number Diff line change
Expand Up @@ -1241,7 +1241,8 @@ function getImmediateDerivedClasses(classDec: ClassLikeDeclarationBaseSpecific &
if (nameNode == null)
return classes;

for (const node of nameNode.findReferencesAsNodes()) {
for (let node of nameNode.findReferencesAsNodes()) {
node = node.getParentWhileKind(SyntaxKind.PropertyAccessExpression) ?? node;
const nodeParent = node.getParentIfKind(SyntaxKind.ExpressionWithTypeArguments);
if (nodeParent == null)
continue;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ import {
} from "../../../../../structures";
import { WriterFunction } from "../../../../../types";
import { getInfoFromText, getInfoFromTextWithDescendant, OptionalKindAndTrivia } from "../../../testHelpers";
import { Project } from "../../../../../main";

describe("ClassLikeDeclarationBase", () => {
function getInfoFromTextForClassLike(text: string) {
Expand Down Expand Up @@ -1716,5 +1717,14 @@ class Child extends Mixin(Base) {}
it("should get the class descendants when there are none", () => {
doTest("class Base {} class Child1 extends Base {} class Child2 extends Base {} class Grandchild1 extends Child1 {}", "Grandchild1", []);
});

it("should get the class descendants when a subclass extends via a PropertyAccessExpression", () => {
const project = new Project();
const sourceFile1 = project.createSourceFile("test.ts", `export class ExampleClass {}`);
project.createSourceFile("test2.ts", `import * as test from "./test"; class ExampleSubclass extends test.ExampleClass {};`);

const classes = sourceFile1.getClassOrThrow("ExampleClass").getDerivedClasses();
expect(classes.map(c => c.getName())).to.deep.equal(["ExampleSubclass"]);
});
});
});

0 comments on commit 0d38985

Please sign in to comment.