From 4ee04154c6fd3ef7ffd7b64944e20177752286c2 Mon Sep 17 00:00:00 2001 From: Raido Kuli Date: Tue, 16 Apr 2019 21:52:14 +0300 Subject: [PATCH] [FEATURE tagless-event-assert] Update assertion for events in tagless component (cherry picked from commit 94de0c45a63072580d76e69308c6586052e97675) --- .../-internals/glimmer/lib/component.ts | 42 +++++++++---------- .../components/fragment-components-test.js | 5 ++- 2 files changed, 24 insertions(+), 23 deletions(-) diff --git a/packages/@ember/-internals/glimmer/lib/component.ts b/packages/@ember/-internals/glimmer/lib/component.ts index aee0b86eded..1bd179c7b28 100644 --- a/packages/@ember/-internals/glimmer/lib/component.ts +++ b/packages/@ember/-internals/glimmer/lib/component.ts @@ -12,6 +12,7 @@ import { ViewStateSupport, } from '@ember/-internals/views'; import { assert } from '@ember/debug'; +import { DEBUG } from '@glimmer/env'; import { DirtyableTag } from '@glimmer/reference'; import { normalizeProperty, SVG_NAMESPACE } from '@glimmer/runtime'; @@ -707,27 +708,26 @@ const Component = CoreView.extend( this[ROOT_REF] = new RootReference(this); this[BOUNDS] = null; - // If in a tagless component, assert that no event handlers are defined - assert( - // tslint:disable-next-line:max-line-length - `You can not define a function that handles DOM events in the \`${this}\` tagless component since it doesn't have any DOM element.`, - this.tagName !== '' || - !this.renderer._destinedForDOM || - !(() => { - let eventDispatcher = getOwner(this).lookup('event_dispatcher:main'); - let events = (eventDispatcher && eventDispatcher._finalEvents) || {}; - - // tslint:disable-next-line:forin - for (let key in events) { - let methodName = events[key]; - - if (typeof this[methodName] === 'function') { - return true; // indicate that the assertion should be triggered - } - } - return false; - })() - ); + if (DEBUG && this.renderer._destinedForDOM && this.tagName === '') { + let eventNames = []; + let eventDispatcher = getOwner(this).lookup('event_dispatcher:main'); + let events = (eventDispatcher && eventDispatcher._finalEvents) || {}; + + // tslint:disable-next-line:forin + for (let key in events) { + let methodName = events[key]; + + if (typeof this[methodName] === 'function') { + eventNames.push(methodName); + } + } + // If in a tagless component, assert that no event handlers are defined + assert( + // tslint:disable-next-line:max-line-length + `You can not define \`${eventNames}\` function(s) to handle DOM event in the \`${this}\` tagless component since it doesn't have any DOM element.`, + !eventNames.length + ); + } }, rerender() { diff --git a/packages/@ember/-internals/glimmer/tests/integration/components/fragment-components-test.js b/packages/@ember/-internals/glimmer/tests/integration/components/fragment-components-test.js index 6b5f68a5112..7235738ef77 100644 --- a/packages/@ember/-internals/glimmer/tests/integration/components/fragment-components-test.js +++ b/packages/@ember/-internals/glimmer/tests/integration/components/fragment-components-test.js @@ -57,6 +57,7 @@ moduleFor( let FooBarComponent = Component.extend({ tagName: '', click() {}, + mouseEnter() {}, }); this.registerComponent('foo-bar', { @@ -66,7 +67,7 @@ moduleFor( expectAssertion(() => { this.render(`{{#foo-bar}}{{/foo-bar}}`); - }, /You can not define a function that handles DOM events in the .* tagless component since it doesn't have any DOM element./); + }, /You can not define `click,mouseEnter` function\(s\) to handle DOM event in the .* tagless component since it doesn't have any DOM element./); } ['@test throws an error if a custom defined event function is defined in a tagless component']() { @@ -83,7 +84,7 @@ moduleFor( expectAssertion(() => { this.render(`{{#foo-bar}}{{/foo-bar}}`); - }, /You can not define a function that handles DOM events in the .* tagless component since it doesn't have any DOM element./); + }, /You can not define `folks` function\(s\) to handle DOM event in the .* tagless component since it doesn't have any DOM element./); } ['@test throws an error if `tagName` is an empty string and `classNameBindings` are specified']() {