Skip to content

Commit

Permalink
fix: getOverloads for a class method should take into account if stat…
Browse files Browse the repository at this point in the history
…ic (#1337)

Closes #1298
  • Loading branch information
dsherret authored Oct 1, 2022
1 parent 7680bae commit f927d01
Show file tree
Hide file tree
Showing 3 changed files with 32 additions and 9 deletions.
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

0 comments on commit f927d01

Please sign in to comment.