From 0d38985c3a701fa38c379332e188812f5302d503 Mon Sep 17 00:00:00 2001 From: Jack Myrick <63320517+jmyrick02@users.noreply.github.com> Date: Sat, 5 Oct 2024 05:15:28 -0500 Subject: [PATCH] fix: getDerivedClasses() isn't correct in some cases (#1557) --- .../ast/class/base/ClassLikeDeclarationBase.ts | 3 ++- .../ast/class/base/classLikeDeclarationBaseTests.ts | 10 ++++++++++ 2 files changed, 12 insertions(+), 1 deletion(-) diff --git a/packages/ts-morph/src/compiler/ast/class/base/ClassLikeDeclarationBase.ts b/packages/ts-morph/src/compiler/ast/class/base/ClassLikeDeclarationBase.ts index 2d8006a87..c8b23099b 100644 --- a/packages/ts-morph/src/compiler/ast/class/base/ClassLikeDeclarationBase.ts +++ b/packages/ts-morph/src/compiler/ast/class/base/ClassLikeDeclarationBase.ts @@ -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; diff --git a/packages/ts-morph/src/tests/compiler/ast/class/base/classLikeDeclarationBaseTests.ts b/packages/ts-morph/src/tests/compiler/ast/class/base/classLikeDeclarationBaseTests.ts index 61381e46b..a10c97e58 100644 --- a/packages/ts-morph/src/tests/compiler/ast/class/base/classLikeDeclarationBaseTests.ts +++ b/packages/ts-morph/src/tests/compiler/ast/class/base/classLikeDeclarationBaseTests.ts @@ -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) { @@ -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"]); + }); }); });