From 3498d6d0c68d7d73aa0d048fafed2d2b28248a22 Mon Sep 17 00:00:00 2001 From: mrkishi Date: Mon, 5 Dec 2016 11:56:42 -0200 Subject: [PATCH] Clean up generated teardown code for empty cases --- compiler/generate/index.js | 19 ++++++++++++++----- 1 file changed, 14 insertions(+), 5 deletions(-) diff --git a/compiler/generate/index.js b/compiler/generate/index.js index 0ed0ed65da..f69191147b 100644 --- a/compiler/generate/index.js +++ b/compiler/generate/index.js @@ -57,6 +57,19 @@ export default function generate ( parsed, source, options ) { fragment.initStatements.push( `${fragment.autofocus}.focus();` ); } + const detachStatements = fragment.detachStatements.join( '\n\n' ); + const teardownStatements = fragment.teardownStatements.join( '\n\n' ); + + const detachBlock = deindent` + if ( detach ) { + ${detachStatements} + } + `; + + const teardownBlock = deindent` + ${teardownStatements}${detachStatements ? `\n\n${detachBlock}` : ``} + `; + renderers.push( deindent` function ${fragment.name} ( ${fragment.params}, component ) { ${fragment.initStatements.join( '\n\n' )} @@ -71,11 +84,7 @@ export default function generate ( parsed, source, options ) { }, teardown: function ( detach ) { - ${fragment.teardownStatements.join( '\n\n' )} - - if ( detach ) { - ${fragment.detachStatements.join( '\n\n' )} - } + ${teardownBlock} } }; }