From 67967b1a83c4903cf7e6367e45c662ec7dd1ff37 Mon Sep 17 00:00:00 2001 From: Rich-Harris Date: Sat, 8 Apr 2017 17:22:52 -0400 Subject: [PATCH] DRY out block.tmp stuff --- src/generators/dom/Block.js | 10 +++++++--- src/generators/dom/visitors/MustacheTag.js | 6 ++---- src/generators/dom/visitors/RawMustacheTag.js | 6 ++---- .../dom/visitors/attributes/addElementAttributes.js | 6 ++---- 4 files changed, 13 insertions(+), 15 deletions(-) diff --git a/src/generators/dom/Block.js b/src/generators/dom/Block.js index 9e2cbc0ab6..3f5b838bbb 100644 --- a/src/generators/dom/Block.js +++ b/src/generators/dom/Block.js @@ -104,17 +104,16 @@ export default class Block { if ( this.builders.update.isEmpty() ) { properties.addBlock( `update: ${this.generator.helper( 'noop' )},` ); } else { + if ( this._tmp ) this.builders.update.addBlockAtStart( `var ${this._tmp};` ); properties.addBlock( deindent` update: function ( changed, ${this.params.join( ', ' )} ) { - ${this.tmp ? `var ${this.tmp};` : ''} - ${this.builders.update} }, ` ); } if ( this.builders.destroy.isEmpty() ) { - properties.addBlock( `destroy: ${this.generator.helper( 'noop' )},` ); + properties.addBlock( `destroy: ${this.generator.helper( 'noop' )}` ); } else { properties.addBlock( deindent` destroy: function ( detach ) { @@ -133,4 +132,9 @@ export default class Block { } `; } + + tmp () { + if ( !this._tmp ) this._tmp = this.getUniqueName( 'tmp' ); + return this._tmp; + } } \ No newline at end of file diff --git a/src/generators/dom/visitors/MustacheTag.js b/src/generators/dom/visitors/MustacheTag.js index 5cbdfb76a8..e6b6ee593a 100644 --- a/src/generators/dom/visitors/MustacheTag.js +++ b/src/generators/dom/visitors/MustacheTag.js @@ -8,11 +8,9 @@ export default function visitMustacheTag ( generator, block, state, node ) { block.builders.create.addLine( `var last_${name} = ${snippet};` ); block.addElement( name, `${generator.helper( 'createText' )}( last_${name} )`, state.parentNode, true ); - if ( !block.tmp ) block.tmp = block.getUniqueName( 'tmp' ); - block.builders.update.addBlock( deindent` - if ( ( ${block.tmp} = ${snippet} ) !== last_${name} ) { - ${name}.data = last_${name} = ${block.tmp}; + if ( ( ${block.tmp()} = ${snippet} ) !== last_${name} ) { + ${name}.data = last_${name} = ${block.tmp()}; } ` ); } \ No newline at end of file diff --git a/src/generators/dom/visitors/RawMustacheTag.js b/src/generators/dom/visitors/RawMustacheTag.js index c0751477e0..13a9398056 100644 --- a/src/generators/dom/visitors/RawMustacheTag.js +++ b/src/generators/dom/visitors/RawMustacheTag.js @@ -25,11 +25,9 @@ export default function visitRawMustacheTag ( generator, block, state, node ) { block.builders.create.addLine( mountStatement ); } - if ( !block.tmp ) block.tmp = block.getUniqueName( 'tmp' ); - block.builders.update.addBlock( deindent` - if ( ( ${block.tmp} = ${snippet} ) !== last_${name} ) { - last_${name} = ${block.tmp}; + if ( ( ${block.tmp()} = ${snippet} ) !== last_${name} ) { + last_${name} = ${block.tmp()}; ${detachStatement} ${mountStatement} } diff --git a/src/generators/dom/visitors/attributes/addElementAttributes.js b/src/generators/dom/visitors/attributes/addElementAttributes.js index 3036cab8c5..8936db3d17 100644 --- a/src/generators/dom/visitors/attributes/addElementAttributes.js +++ b/src/generators/dom/visitors/attributes/addElementAttributes.js @@ -106,11 +106,9 @@ export default function addElementAttributes ( generator, block, node, local ) { local.create.addLine( updater ); - if ( !block.tmp ) block.tmp = block.getUniqueName( 'tmp' ); - local.update.addBlock( deindent` - if ( ( ${block.tmp} = ${snippet} ) !== ${last} ) { - ${last} = ${block.tmp}; + if ( ( ${block.tmp()} = ${snippet} ) !== ${last} ) { + ${last} = ${block.tmp()}; ${updater} } ` );