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

When the method of the base class is overridden, the 'belongingClass' property of this base class is tampered with its subclass.

Signed-off-by: ganjing <[email protected]>
  • Loading branch information
Shanks0224 committed Oct 10, 2023
1 parent caabe14 commit 01e7775
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;
// the subclass inherits the method of the base class and should not modify the belongedClass of the base class method.
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 01e7775

Please sign in to comment.