Skip to content

Commit ae2ebbc

Browse files
committed
Add super to derived methods
1 parent bb85f63 commit ae2ebbc

File tree

2 files changed

+30
-2
lines changed

2 files changed

+30
-2
lines changed

src/blocks/mrc_class_method_def.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -365,9 +365,9 @@ export const pythonFromBlock = function (
365365
branch = generator.INDENT + 'super.__init__(' + class_specific + ')\n' +
366366
generator.defineClassVariables() + branch;
367367
}
368-
else if (funcName == 'update'){
368+
else if (generator.inBaseClassMethod(blocklyName)){
369369
// Special case for update, to also call the update method of the base class
370-
branch = generator.INDENT + 'super.update()\n' + branch;
370+
branch = generator.INDENT + 'super.' + blocklyName + '()\n' + branch;
371371
}
372372
if (returnValue) {
373373
returnValue = generator.INDENT + 'return ' + returnValue + '\n';

src/editor/extended_python_generator.ts

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -212,6 +212,34 @@ export class ExtendedPythonGenerator extends PythonGenerator {
212212
}
213213
return ''
214214
}
215+
216+
/**
217+
* This returns the list of methods that are derived from so that mrc_class_method_def
218+
* knows whether to call super() or not.
219+
* @returns list of method names
220+
*/
221+
getBaseClassMethods() : string[] {
222+
let classParent = this.context?.getClassParent();
223+
if (classParent == 'OpMode'){
224+
return ['start', 'loop', 'stop'];
225+
}
226+
else if (classParent == 'Mechanism') {
227+
return ['start', 'update', 'stop'];
228+
}
229+
else if (classParent == 'RobotBase'){
230+
return ['start', 'update', 'stop'];
231+
}
232+
return [];
233+
}
234+
235+
/**
236+
* Returns true if the method is in the base class.
237+
* @param methodName the name of the method to check
238+
*/
239+
inBaseClassMethod(methodName: string): boolean {
240+
const baseMethods = this.getBaseClassMethods();
241+
return baseMethods.includes(methodName);
242+
}
215243
}
216244

217245
export const extendedPythonGenerator = new ExtendedPythonGenerator();

0 commit comments

Comments
 (0)