From 10f4223028a0ca8c2e0a6454868128df763917b3 Mon Sep 17 00:00:00 2001 From: Matthew Kilgore Date: Fri, 7 Aug 2020 19:43:01 -0400 Subject: [PATCH] Add error warning if children attr is ever used --- js/src/common/Component.ts | 20 ++++++++++++++------ 1 file changed, 14 insertions(+), 6 deletions(-) diff --git a/js/src/common/Component.ts b/js/src/common/Component.ts index 47f28aef4..91a0524fa 100644 --- a/js/src/common/Component.ts +++ b/js/src/common/Component.ts @@ -24,9 +24,7 @@ export default abstract class Component implemen abstract view(vnode: Mithril.Vnode): Mithril.Children; oninit(vnode: Mithril.Vnode) { - this.initAttrs(vnode.attrs); - - this.attrs = vnode.attrs; + this.setAttrs(vnode.attrs); } oncreate(vnode: Mithril.VnodeDOM) { @@ -34,9 +32,7 @@ export default abstract class Component implemen } onbeforeupdate(vnode: Mithril.VnodeDOM) { - this.initAttrs(vnode.attrs); - - this.attrs = vnode.attrs; + this.setAttrs(vnode.attrs); } /** @@ -67,6 +63,18 @@ export default abstract class Component implemen return m(this as any, componentProps, children); } + private setAttrs(attrs: T) { + this.initAttrs(attrs); + + if (attrs && 'children' in attrs) { + throw new Error( + 'The "children" attribute of attrs should never be used. Either pass children in as the vnode children or rename the attribute' + ); + } + + this.attrs = attrs; + } + protected initAttrs(attrs: T): T { return attrs; }