Skip to content

Commit

Permalink
fix: the 'belongedClass' property of the overridden method has been t…
Browse files Browse the repository at this point in the history
…ampered with its subclass (#21)

Signed-off-by: ganjing <[email protected]>
  • Loading branch information
Shanks0224 authored Oct 12, 2023
1 parent 2514ba2 commit c41a7f1
Showing 1 changed file with 32 additions and 4 deletions.
36 changes: 32 additions & 4 deletions src/type.ts
Original file line number Diff line number Diff line change
Expand Up @@ -480,7 +480,9 @@ export class TSClass extends TSTypeWithArguments {

addMethod(classMethod: TsClassFunc): void {
classMethod.type.isMethod = true;
classMethod.type.belongedClass = this;
// when sub class inherits the method of the base class, it should not modify the 'belongedClass'.
if (!classMethod.type.belongedClass)
classMethod.type.belongedClass = this;
this._methods.push(classMethod);
}

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

updateMethod(
name: string,
kind: FunctionKind = FunctionKind.METHOD,
funcType: TSFunction,
): boolean {
const res = this.memberFuncs.findIndex((f) => {
return name === f.name && kind === f.type.funcKind;
});
if (res !== -1) {
this.memberFuncs[res].type = funcType;
return true;
}
return false;
}

setClassName(name: string) {
this._name = name;
}
Expand Down Expand Up @@ -1893,7 +1910,11 @@ export class TypeResolver {
infc.addMemberField(field);
}
for (const method of baseInfcType.memberFuncs) {
infc.addMethod(method);
infc.addMethod({
name: method.name,
type: method.type.clone(),
optional: method.optional,
});
}
}

Expand Down Expand Up @@ -2124,7 +2145,11 @@ export class TypeResolver {
classType.addStaticMemberField(staticField);
}
for (const method of baseClassType.memberFuncs) {
classType.addMethod(method);
classType.addMethod({
name: method.name,
type: method.type.clone(),
optional: method.optional,
});
}
}

Expand Down Expand Up @@ -2369,7 +2394,10 @@ export class TypeResolver {
const baseFuncType = baseClassType.getMethod(methodName, funcKind)
.method?.type;
if (baseFuncType) {
tsFuncType = baseFuncType;
if (!tsFuncType.belongedClass)
tsFuncType.belongedClass = classType;
// override the method of base class
classType.updateMethod(methodName, funcKind, tsFuncType);
isOverride = true;
}
}
Expand Down

0 comments on commit c41a7f1

Please sign in to comment.