From e2d63d1b34e92bcdb05c1f03de149989602003b5 Mon Sep 17 00:00:00 2001 From: Rich Harris Date: Sun, 30 Jul 2017 00:09:35 -0400 Subject: [PATCH] add dev mode warning for double destroy --- src/generators/dom/index.ts | 2 +- .../dev-warning-destroy-twice/_config.js | 21 +++++++++++++++++++ .../dev-warning-destroy-twice/main.html | 1 + 3 files changed, 23 insertions(+), 1 deletion(-) create mode 100644 test/runtime/samples/dev-warning-destroy-twice/_config.js create mode 100644 test/runtime/samples/dev-warning-destroy-twice/main.html diff --git a/src/generators/dom/index.ts b/src/generators/dom/index.ts index 33ac0a33c4..8c3f287b8d 100644 --- a/src/generators/dom/index.ts +++ b/src/generators/dom/index.ts @@ -264,7 +264,7 @@ export default function dom( }; ${name}.prototype.teardown = ${name}.prototype.destroy = function destroy ( detach ) { - if ( this._destroyed ) return; + if ( this._destroyed ) return${options.dev && ` console.warn( 'Component was already destroyed' )`}; this.fire( 'destroy' ); ${templateProperties.ondestroy && `@template.ondestroy.call( this );`} diff --git a/test/runtime/samples/dev-warning-destroy-twice/_config.js b/test/runtime/samples/dev-warning-destroy-twice/_config.js new file mode 100644 index 0000000000..94c07ed6a7 --- /dev/null +++ b/test/runtime/samples/dev-warning-destroy-twice/_config.js @@ -0,0 +1,21 @@ +export default { + dev: true, + + test(assert, component) { + const warn = console.warn; // eslint-disable-line no-console + + const warnings = []; + console.warn = warning => { // eslint-disable-line no-console + warnings.push(warning); + }; + + component.destroy(); + component.destroy(); + + assert.deepEqual(warnings, [ + `Component was already destroyed` + ]); + + console.warn = warn; // eslint-disable-line no-console + } +}; \ No newline at end of file diff --git a/test/runtime/samples/dev-warning-destroy-twice/main.html b/test/runtime/samples/dev-warning-destroy-twice/main.html new file mode 100644 index 0000000000..78e7012b82 --- /dev/null +++ b/test/runtime/samples/dev-warning-destroy-twice/main.html @@ -0,0 +1 @@ +
\ No newline at end of file