From 94c8bce810cc63d580aa12abca245ee79960a07e Mon Sep 17 00:00:00 2001 From: Joseph Caburnay Date: Thu, 12 Mar 2020 08:25:33 +0100 Subject: [PATCH] [IMP] allow anonymous Component extensions It is possible that a Component is extended dynamically and if this is the case, the class that extends it can be anonymous, with property name=''. If this is the case, current implementation interprets the empty string to be false so the while loop is terminated without further scanning the super classes. In this proposal, we allow anonymous class to be scanned until its Component ancestor. Basically, the anonymous class assumes the name of it super. --- src/component/component.ts | 5 +++-- tests/component/component.test.ts | 9 +++++++++ 2 files changed, 12 insertions(+), 2 deletions(-) diff --git a/src/component/component.ts b/src/component/component.ts index 57d1e91b..07161846 100644 --- a/src/component/component.ts +++ b/src/component/component.ts @@ -604,9 +604,10 @@ export class Component { // key. So we fall back on looking for a template matching its name (or // one of its subclass). - let template: string; - while ((template = p.name) && !(template in qweb.templates) && p !== Component) { + let template: string = p.name; + while (!(template in qweb.templates) && p !== Component) { p = p.__proto__; + template = p.name; } if (p === Component) { throw new Error(`Could not find template for component "${this.constructor.name}"`); diff --git a/tests/component/component.test.ts b/tests/component/component.test.ts index cc1118bc..969dfb57 100644 --- a/tests/component/component.test.ts +++ b/tests/component/component.test.ts @@ -3080,6 +3080,15 @@ describe("can deduce template from name", () => { expect(fixture.innerHTML).toBe("Orval"); }); + test("can find template of anonymous component", async () => { + class ABC extends Component {} + const Anonymous = class extends ABC {}; + env.qweb.addTemplate("ABC", "Orval"); + const def = new Anonymous(); + await def.mount(fixture); + expect(fixture.innerHTML).toBe("Orval"); + }); + test("can find template of parent component, defined by template key", async () => { class ABC extends Component { static template = "Achel";