diff --git a/src/generators/nodes/Element.ts b/src/generators/nodes/Element.ts index a43c725ff4..555ea5ae4d 100644 --- a/src/generators/nodes/Element.ts +++ b/src/generators/nodes/Element.ts @@ -516,16 +516,16 @@ export default class Element extends Node { }); `); - if (generator.options.dev) { - block.builders.hydrate.addBlock(deindent` - if (${handlerName}.teardown) { - console.warn("Return 'destroy()' from custom event handlers. Returning 'teardown()' has been deprecated and will be unsupported in Svelte 2"); - } - `); - } + + block.builders.hydrate.addBlock(deindent` + if (${handlerName}.teardown) { + ${handlerName}.destroy = ${handlerName}.teardown; + ${generator.options.dev && `console.warn("Return 'destroy()' from custom event handlers. Returning 'teardown()' has been deprecated and will be unsupported in Svelte 2");`} + } + `); block.builders.destroy.addLine(deindent` - ${handlerName}[${handlerName}.destroy ? 'destroy' : 'teardown'](); + ${handlerName}.destroy(); `); } else { const handler = deindent` diff --git a/test/js/samples/event-handlers-custom/expected-bundle.js b/test/js/samples/event-handlers-custom/expected-bundle.js index 7c17164c74..7aea3cc47f 100644 --- a/test/js/samples/event-handlers-custom/expected-bundle.js +++ b/test/js/samples/event-handlers-custom/expected-bundle.js @@ -199,6 +199,10 @@ function create_main_fragment(component, state) { var state = component.get(); component.foo( state.bar ); }); + + if (foo_handler.teardown) { + foo_handler.destroy = foo_handler.teardown; + } }, m: function mount(target, anchor) { @@ -212,7 +216,7 @@ function create_main_fragment(component, state) { }, d: function destroy$$1() { - foo_handler[foo_handler.destroy ? 'destroy' : 'teardown'](); + foo_handler.destroy(); } }; } diff --git a/test/js/samples/event-handlers-custom/expected.js b/test/js/samples/event-handlers-custom/expected.js index 08f964c346..b20649d611 100644 --- a/test/js/samples/event-handlers-custom/expected.js +++ b/test/js/samples/event-handlers-custom/expected.js @@ -26,6 +26,10 @@ function create_main_fragment(component, state) { var state = component.get(); component.foo( state.bar ); }); + + if (foo_handler.teardown) { + foo_handler.destroy = foo_handler.teardown; + } }, m: function mount(target, anchor) { @@ -39,7 +43,7 @@ function create_main_fragment(component, state) { }, d: function destroy() { - foo_handler[foo_handler.destroy ? 'destroy' : 'teardown'](); + foo_handler.destroy(); } }; }