Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: getDerivedClasses() isn't correct in some cases #1557

Merged
merged 2 commits into from
Oct 5, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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"]);
});
});
});