Skip to content

Commit c41a7f1

Browse files
authored
fix: the 'belongedClass' property of the overridden method has been tampered with its subclass (#21)
Signed-off-by: ganjing <[email protected]>
1 parent 2514ba2 commit c41a7f1

File tree

1 file changed

+32
-4
lines changed

1 file changed

+32
-4
lines changed

src/type.ts

Lines changed: 32 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -480,7 +480,9 @@ export class TSClass extends TSTypeWithArguments {
480480

481481
addMethod(classMethod: TsClassFunc): void {
482482
classMethod.type.isMethod = true;
483-
classMethod.type.belongedClass = this;
483+
// when sub class inherits the method of the base class, it should not modify the 'belongedClass'.
484+
if (!classMethod.type.belongedClass)
485+
classMethod.type.belongedClass = this;
484486
this._methods.push(classMethod);
485487
}
486488

@@ -497,6 +499,21 @@ export class TSClass extends TSTypeWithArguments {
497499
return { index: -1, method: null };
498500
}
499501

502+
updateMethod(
503+
name: string,
504+
kind: FunctionKind = FunctionKind.METHOD,
505+
funcType: TSFunction,
506+
): boolean {
507+
const res = this.memberFuncs.findIndex((f) => {
508+
return name === f.name && kind === f.type.funcKind;
509+
});
510+
if (res !== -1) {
511+
this.memberFuncs[res].type = funcType;
512+
return true;
513+
}
514+
return false;
515+
}
516+
500517
setClassName(name: string) {
501518
this._name = name;
502519
}
@@ -1893,7 +1910,11 @@ export class TypeResolver {
18931910
infc.addMemberField(field);
18941911
}
18951912
for (const method of baseInfcType.memberFuncs) {
1896-
infc.addMethod(method);
1913+
infc.addMethod({
1914+
name: method.name,
1915+
type: method.type.clone(),
1916+
optional: method.optional,
1917+
});
18971918
}
18981919
}
18991920

@@ -2124,7 +2145,11 @@ export class TypeResolver {
21242145
classType.addStaticMemberField(staticField);
21252146
}
21262147
for (const method of baseClassType.memberFuncs) {
2127-
classType.addMethod(method);
2148+
classType.addMethod({
2149+
name: method.name,
2150+
type: method.type.clone(),
2151+
optional: method.optional,
2152+
});
21282153
}
21292154
}
21302155

@@ -2369,7 +2394,10 @@ export class TypeResolver {
23692394
const baseFuncType = baseClassType.getMethod(methodName, funcKind)
23702395
.method?.type;
23712396
if (baseFuncType) {
2372-
tsFuncType = baseFuncType;
2397+
if (!tsFuncType.belongedClass)
2398+
tsFuncType.belongedClass = classType;
2399+
// override the method of base class
2400+
classType.updateMethod(methodName, funcKind, tsFuncType);
23732401
isOverride = true;
23742402
}
23752403
}

0 commit comments

Comments
 (0)