Skip to content

Commit f927d01

Browse files
authoredOct 1, 2022
fix: getOverloads for a class method should take into account if static (#1337)
Closes #1298
1 parent 7680bae commit f927d01

File tree

3 files changed

+32
-9
lines changed

3 files changed

+32
-9
lines changed
 

‎deno/ts_morph.js

+10-3
Original file line numberDiff line numberDiff line change
@@ -13647,11 +13647,12 @@ function OverloadableNode(Base) {
1364713647
function getOverloadsAndImplementation(node) {
1364813648
const parent = node.getParentOrThrow();
1364913649
const name = getNameIfNamedNode(node);
13650+
const isStatic = getStaticIfStaticable(node);
1365013651
const kind = node.getKind();
1365113652
return parent.forEachChildAsArray().filter(n => {
13652-
const hasSameName = getNameIfNamedNode(n) === name;
13653-
const hasSameKind = n.getKind() === kind;
13654-
return hasSameName && hasSameKind;
13653+
return getNameIfNamedNode(n) === name
13654+
&& n.getKind() === kind
13655+
&& getStaticIfStaticable(n) === isStatic;
1365513656
});
1365613657
}
1365713658
function getNameIfNamedNode(node) {
@@ -13660,6 +13661,12 @@ function getNameIfNamedNode(node) {
1366013661
return nodeAsNamedNode.getName();
1366113662
return undefined;
1366213663
}
13664+
function getStaticIfStaticable(node) {
13665+
const nodeAsStaticableNode = node;
13666+
if (nodeAsStaticableNode.isStatic instanceof Function)
13667+
return nodeAsStaticableNode.isStatic();
13668+
return false;
13669+
}
1366313670
function insertOverloads(opts) {
1366413671
if (opts.structures.length === 0)
1366513672
return [];

‎packages/ts-morph/src/compiler/ast/function/OverloadableNode.ts

+13-5
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,9 @@
1-
import { errors, ObjectUtils, SyntaxKind } from "@ts-morph/common";
1+
import { errors, SyntaxKind } from "@ts-morph/common";
22
import { CodeBlockWriter } from "../../../codeBlockWriter";
33
import { getRangeWithoutCommentsFromArray, insertIntoParentTextRange, verifyAndGetIndex } from "../../../manipulation";
44
import { Structure } from "../../../structures";
55
import { Constructor } from "../../../types";
6-
import { BodyableNode, NamedNode } from "../base";
6+
import { BodyableNode, NamedNode, StaticableNode } from "../base";
77
import { Node } from "../common";
88

99
export type OverloadableNodeExtensionType = Node & BodyableNode;
@@ -63,11 +63,12 @@ export function OverloadableNode<T extends Constructor<OverloadableNodeExtension
6363
function getOverloadsAndImplementation(node: OverloadableNodeExtensionType & OverloadableNode) {
6464
const parent = node.getParentOrThrow();
6565
const name = getNameIfNamedNode(node);
66+
const isStatic = getStaticIfStaticable(node);
6667
const kind = node.getKind();
6768
return parent.forEachChildAsArray().filter(n => {
68-
const hasSameName = getNameIfNamedNode(n) === name;
69-
const hasSameKind = n.getKind() === kind;
70-
return hasSameName && hasSameKind;
69+
return getNameIfNamedNode(n) === name
70+
&& n.getKind() === kind
71+
&& getStaticIfStaticable(n) === isStatic;
7172
}) as (OverloadableNodeExtensionType & OverloadableNode)[];
7273
}
7374

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

82+
function getStaticIfStaticable(node: Node) {
83+
const nodeAsStaticableNode = (node as any as StaticableNode);
84+
if (nodeAsStaticableNode.isStatic instanceof Function)
85+
return nodeAsStaticableNode.isStatic();
86+
return false;
87+
}
88+
8189
/**
8290
* @internal
8391
*/

‎packages/ts-morph/src/tests/compiler/ast/function/overloadableNodeTests.ts

+9-1
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,8 @@ describe("OverloadableNode", () => {
88
const { sourceFile: functionSourceFile } = getInfoFromText<FunctionDeclaration>(functionCode);
99
const functions = functionSourceFile.getChildSyntaxListOrThrow().getChildren() as FunctionDeclaration[];
1010

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

@@ -84,6 +85,13 @@ describe("OverloadableNode", () => {
8485
expect(firstChild.getOverloads().length).to.equal(2);
8586
});
8687
});
88+
89+
describe("methods", () => {
90+
it("should get the correct number of overloads for method", () => {
91+
expect(classChild.getInstanceMethodOrThrow("myMethod").getOverloads().length).to.equal(1);
92+
expect(classChild.getStaticMethodOrThrow("myMethod").getOverloads().length).to.equal(2);
93+
});
94+
});
8795
});
8896

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

0 commit comments

Comments
 (0)
Please sign in to comment.