diff --git a/deno/ts_morph.js b/deno/ts_morph.js index f515e0ab5..e0fe16ced 100644 --- a/deno/ts_morph.js +++ b/deno/ts_morph.js @@ -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) { @@ -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 []; diff --git a/packages/ts-morph/src/compiler/ast/function/OverloadableNode.ts b/packages/ts-morph/src/compiler/ast/function/OverloadableNode.ts index bf087c87c..1cebd0d77 100644 --- a/packages/ts-morph/src/compiler/ast/function/OverloadableNode.ts +++ b/packages/ts-morph/src/compiler/ast/function/OverloadableNode.ts @@ -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; @@ -63,11 +63,12 @@ export function OverloadableNode { - 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)[]; } @@ -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 */ diff --git a/packages/ts-morph/src/tests/compiler/ast/function/overloadableNodeTests.ts b/packages/ts-morph/src/tests/compiler/ast/function/overloadableNodeTests.ts index 9ce804f2b..06e83116b 100644 --- a/packages/ts-morph/src/tests/compiler/ast/function/overloadableNodeTests.ts +++ b/packages/ts-morph/src/tests/compiler/ast/function/overloadableNodeTests.ts @@ -8,7 +8,8 @@ describe("OverloadableNode", () => { const { sourceFile: functionSourceFile } = getInfoFromText(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(constructorCode); const constructors = classChild.getChildSyntaxListOrThrow().getChildren().filter(c => c instanceof ConstructorDeclaration) as ConstructorDeclaration[]; @@ -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("getImplementation"), () => {