Skip to content

Commit 0d38985

Browse files
authored
fix: getDerivedClasses() isn't correct in some cases (#1557)
1 parent 308ae4c commit 0d38985

File tree

2 files changed

+12
-1
lines changed

2 files changed

+12
-1
lines changed

packages/ts-morph/src/compiler/ast/class/base/ClassLikeDeclarationBase.ts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1241,7 +1241,8 @@ function getImmediateDerivedClasses(classDec: ClassLikeDeclarationBaseSpecific &
12411241
if (nameNode == null)
12421242
return classes;
12431243

1244-
for (const node of nameNode.findReferencesAsNodes()) {
1244+
for (let node of nameNode.findReferencesAsNodes()) {
1245+
node = node.getParentWhileKind(SyntaxKind.PropertyAccessExpression) ?? node;
12451246
const nodeParent = node.getParentIfKind(SyntaxKind.ExpressionWithTypeArguments);
12461247
if (nodeParent == null)
12471248
continue;

packages/ts-morph/src/tests/compiler/ast/class/base/classLikeDeclarationBaseTests.ts

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,7 @@ import {
2727
} from "../../../../../structures";
2828
import { WriterFunction } from "../../../../../types";
2929
import { getInfoFromText, getInfoFromTextWithDescendant, OptionalKindAndTrivia } from "../../../testHelpers";
30+
import { Project } from "../../../../../main";
3031

3132
describe("ClassLikeDeclarationBase", () => {
3233
function getInfoFromTextForClassLike(text: string) {
@@ -1716,5 +1717,14 @@ class Child extends Mixin(Base) {}
17161717
it("should get the class descendants when there are none", () => {
17171718
doTest("class Base {} class Child1 extends Base {} class Child2 extends Base {} class Grandchild1 extends Child1 {}", "Grandchild1", []);
17181719
});
1720+
1721+
it("should get the class descendants when a subclass extends via a PropertyAccessExpression", () => {
1722+
const project = new Project();
1723+
const sourceFile1 = project.createSourceFile("test.ts", `export class ExampleClass {}`);
1724+
project.createSourceFile("test2.ts", `import * as test from "./test"; class ExampleSubclass extends test.ExampleClass {};`);
1725+
1726+
const classes = sourceFile1.getClassOrThrow("ExampleClass").getDerivedClasses();
1727+
expect(classes.map(c => c.getName())).to.deep.equal(["ExampleSubclass"]);
1728+
});
17191729
});
17201730
});

0 commit comments

Comments
 (0)