diff --git a/src/generators/dom/visitors/Component/EventHandler.ts b/src/generators/dom/visitors/Component/EventHandler.ts index 1010456253..0abef43821 100644 --- a/src/generators/dom/visitors/Component/EventHandler.ts +++ b/src/generators/dom/visitors/Component/EventHandler.ts @@ -13,22 +13,25 @@ export default function visitEventHandler( local ) { // TODO verify that it's a valid callee (i.e. built-in or declared method) - generator.addSourcemapLocations(attribute.expression); - generator.code.prependRight( - attribute.expression.start, - `${block.alias('component')}.` - ); - const usedContexts: string[] = []; - attribute.expression.arguments.forEach((arg: Node) => { - const { contexts } = block.contextualise(arg, null, true); - contexts.forEach(context => { - if (!~usedContexts.indexOf(context)) usedContexts.push(context); - if (!~local.allUsedContexts.indexOf(context)) - local.allUsedContexts.push(context); + if (attribute.expression) { + generator.addSourcemapLocations(attribute.expression); + generator.code.prependRight( + attribute.expression.start, + `${block.alias('component')}.` + ); + + attribute.expression.arguments.forEach((arg: Node) => { + const { contexts } = block.contextualise(arg, null, true); + + contexts.forEach(context => { + if (!~usedContexts.indexOf(context)) usedContexts.push(context); + if (!~local.allUsedContexts.indexOf(context)) + local.allUsedContexts.push(context); + }); }); - }); + } // TODO hoist event handlers? can do `this.__component.method(...)` const declarations = usedContexts.map(name => { @@ -42,7 +45,9 @@ export default function visitEventHandler( const handlerBody = (declarations.length ? declarations.join('\n') + '\n\n' : '') + - `[✂${attribute.expression.start}-${attribute.expression.end}✂];`; + (attribute.expression ? + `[✂${attribute.expression.start}-${attribute.expression.end}✂];` : + `${block.alias('component')}.fire('${attribute.name}', event);`); local.create.addBlock(deindent` ${local.name}.on( '${attribute.name}', function ( event ) { diff --git a/test/runtime/samples/event-handler-shorthand-component/Widget.html b/test/runtime/samples/event-handler-shorthand-component/Widget.html new file mode 100644 index 0000000000..47e1f95a2f --- /dev/null +++ b/test/runtime/samples/event-handler-shorthand-component/Widget.html @@ -0,0 +1 @@ + \ No newline at end of file diff --git a/test/runtime/samples/event-handler-shorthand-component/_config.js b/test/runtime/samples/event-handler-shorthand-component/_config.js new file mode 100644 index 0000000000..e0e21f1400 --- /dev/null +++ b/test/runtime/samples/event-handler-shorthand-component/_config.js @@ -0,0 +1,18 @@ +export default { + html: ` + + `, + + test (assert, component, target, window) { + const button = target.querySelector('button'); + const event = new window.MouseEvent('click'); + + let answer; + component.on('foo', event => { + answer = event.answer; + }); + + button.dispatchEvent(event); + assert.equal(answer, 42); + } +}; diff --git a/test/runtime/samples/event-handler-shorthand-component/main.html b/test/runtime/samples/event-handler-shorthand-component/main.html new file mode 100644 index 0000000000..5eb9bbb3eb --- /dev/null +++ b/test/runtime/samples/event-handler-shorthand-component/main.html @@ -0,0 +1,11 @@ + + + \ No newline at end of file