include destroy as valid built-in method for event handlers (fixes #523)

pull/526/head
Rich-Harris 8 years ago
parent 9427f63b95
commit 754688cf48

@ -1,5 +1,11 @@
import flattenReference from '../../utils/flattenReference.js';
const validBuiltins = new Set([
'set',
'fire',
'destroy'
]);
export default function validateElement ( validator, node ) {
const isComponent = node.name === ':Self' || validator.components.has( node.name );
@ -56,10 +62,15 @@ export default function validateElement ( validator, node ) {
const { name } = flattenReference( callee );
if ( name === 'this' || name === 'event' ) return;
if ( callee.type === 'Identifier' && callee.name === 'set' || callee.name === 'fire' || validator.methods.has( callee.name ) ) return;
if ( callee.type === 'Identifier' && validBuiltins.has( callee.name ) || validator.methods.has( callee.name ) ) return;
const validCallees = [ 'this.*', 'event.*' ]
.concat(
Array.from( validBuiltins ),
Array.from( validator.methods.keys() )
);
const validCallees = list( [ 'this.*', 'event.*', 'set', 'fire' ].concat( Array.from( validator.methods.keys() ) ) );
let message = `'${validator.source.slice( callee.start, callee.end )}' is an invalid callee (should be one of ${validCallees})`;
let message = `'${validator.source.slice( callee.start, callee.end )}' is an invalid callee (should be one of ${list( validCallees )})`;
if ( callee.type === 'Identifier' && validator.helpers.has( callee.name ) ) {
message += `. '${callee.name}' exists on 'helpers', did you put it in the wrong place?`;

@ -0,0 +1,20 @@
export default {
html: `
<button>destroy</button>
`,
test ( assert, component, target, window ) {
const button = target.querySelector( 'button' );
const event = new window.MouseEvent( 'click' );
let destroyed = false;
component.on( 'destroy', () => {
destroyed = true;
});
button.dispatchEvent( event );
assert.htmlEqual( target.innerHTML, `` );
assert.ok( destroyed );
}
};

@ -0,0 +1 @@
<button on:click='destroy()'>destroy</button>

@ -1,5 +1,5 @@
[{
"message": "'foo' is an invalid callee (should be one of this.*, event.*, set, fire or bar). 'foo' exists on 'helpers', did you put it in the wrong place?",
"message": "'foo' is an invalid callee (should be one of this.*, event.*, set, fire, destroy or bar). 'foo' exists on 'helpers', did you put it in the wrong place?",
"pos": 18,
"loc": {
"line": 1,

@ -1,5 +1,5 @@
[{
"message": "'foo' is an invalid callee (should be one of this.*, event.*, set, fire or bar)",
"message": "'foo' is an invalid callee (should be one of this.*, event.*, set, fire, destroy or bar)",
"pos": 18,
"loc": {
"line": 1,

Loading…
Cancel
Save