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: getOverloads for a class method should take into account if static #1337

Merged
merged 1 commit into from
Oct 1, 2022
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
13 changes: 10 additions & 3 deletions deno/ts_morph.js
Original file line number Diff line number Diff line change
Expand Up @@ -13647,11 +13647,12 @@ function OverloadableNode(Base) {
function getOverloadsAndImplementation(node) {
const parent = node.getParentOrThrow();
const name = getNameIfNamedNode(node);
const isStatic = getStaticIfStaticable(node);
const kind = node.getKind();
return parent.forEachChildAsArray().filter(n => {
const hasSameName = getNameIfNamedNode(n) === name;
const hasSameKind = n.getKind() === kind;
return hasSameName && hasSameKind;
return getNameIfNamedNode(n) === name
&& n.getKind() === kind
&& getStaticIfStaticable(n) === isStatic;
});
}
function getNameIfNamedNode(node) {
Expand All @@ -13660,6 +13661,12 @@ function getNameIfNamedNode(node) {
return nodeAsNamedNode.getName();
return undefined;
}
function getStaticIfStaticable(node) {
const nodeAsStaticableNode = node;
if (nodeAsStaticableNode.isStatic instanceof Function)
return nodeAsStaticableNode.isStatic();
return false;
}
function insertOverloads(opts) {
if (opts.structures.length === 0)
return [];
Expand Down
18 changes: 13 additions & 5 deletions packages/ts-morph/src/compiler/ast/function/OverloadableNode.ts
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
import { errors, ObjectUtils, SyntaxKind } from "@ts-morph/common";
import { errors, SyntaxKind } from "@ts-morph/common";
import { CodeBlockWriter } from "../../../codeBlockWriter";
import { getRangeWithoutCommentsFromArray, insertIntoParentTextRange, verifyAndGetIndex } from "../../../manipulation";
import { Structure } from "../../../structures";
import { Constructor } from "../../../types";
import { BodyableNode, NamedNode } from "../base";
import { BodyableNode, NamedNode, StaticableNode } from "../base";
import { Node } from "../common";

export type OverloadableNodeExtensionType = Node & BodyableNode;
Expand Down Expand Up @@ -63,11 +63,12 @@ export function OverloadableNode<T extends Constructor<OverloadableNodeExtension
function getOverloadsAndImplementation(node: OverloadableNodeExtensionType & OverloadableNode) {
const parent = node.getParentOrThrow();
const name = getNameIfNamedNode(node);
const isStatic = getStaticIfStaticable(node);
const kind = node.getKind();
return parent.forEachChildAsArray().filter(n => {
const hasSameName = getNameIfNamedNode(n) === name;
const hasSameKind = n.getKind() === kind;
return hasSameName && hasSameKind;
return getNameIfNamedNode(n) === name
&& n.getKind() === kind
&& getStaticIfStaticable(n) === isStatic;
}) as (OverloadableNodeExtensionType & OverloadableNode)[];
}

Expand All @@ -78,6 +79,13 @@ function getNameIfNamedNode(node: Node) {
return undefined;
}

function getStaticIfStaticable(node: Node) {
const nodeAsStaticableNode = (node as any as StaticableNode);
if (nodeAsStaticableNode.isStatic instanceof Function)
return nodeAsStaticableNode.isStatic();
return false;
}

/**
* @internal
*/
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,8 @@ describe("OverloadableNode", () => {
const { sourceFile: functionSourceFile } = getInfoFromText<FunctionDeclaration>(functionCode);
const functions = functionSourceFile.getChildSyntaxListOrThrow().getChildren() as FunctionDeclaration[];

const constructorCode = `class MyClass { constructor();constructor();constructor() {} myMethod(): void;myMethod() {} abstract test(); }`;
const constructorCode =
`class MyClass { constructor();constructor();constructor() {} myMethod(): void;myMethod() {} static myMethod(): void;static myMethod(): void;static myMethod() {} abstract test(); }`;
const { firstChild: classChild } = getInfoFromText<ClassDeclaration>(constructorCode);
const constructors = classChild.getChildSyntaxListOrThrow().getChildren().filter(c => c instanceof ConstructorDeclaration) as ConstructorDeclaration[];

Expand Down Expand Up @@ -84,6 +85,13 @@ describe("OverloadableNode", () => {
expect(firstChild.getOverloads().length).to.equal(2);
});
});

describe("methods", () => {
it("should get the correct number of overloads for method", () => {
expect(classChild.getInstanceMethodOrThrow("myMethod").getOverloads().length).to.equal(1);
expect(classChild.getStaticMethodOrThrow("myMethod").getOverloads().length).to.equal(2);
});
});
});

describe(nameof<OverloadableNode>("getImplementation"), () => {
Expand Down