various refactorings & fixes

* fix some code style issues
* refactor around createMountStatement
* fix non-toplevel if blocks and elements being detached
pull/101/head
Arpad Borsos 8 years ago
parent e09a39d34f
commit c1ba15875e
No known key found for this signature in database
GPG Key ID: 908EDF65263368B4

@ -15,28 +15,34 @@ export default function generate ( parsed, source, options ) {
const generator = {
addElement ( name, renderStatement, needsIdentifier = false ) {
const needsTeardown = generator.current.localElementDepth === 0;
if ( needsIdentifier || needsTeardown ) {
const isToplevel = generator.current.localElementDepth === 0;
if ( needsIdentifier || isToplevel ) {
generator.current.initStatements.push( deindent`
var ${name} = ${renderStatement};
${generator.appendToTarget( name )};
` );
generator.createMountStatement( name );
} else {
generator.current.initStatements.push( deindent`
${generator.current.target}.appendChild( ${renderStatement} );
` );
}
if ( needsTeardown ) {
if ( isToplevel ) {
generator.current.teardownStatements.push( deindent`
if ( detach ) ${name}.parentNode.removeChild( ${name} );
` );
}
},
appendToTarget ( name ) {
createMountStatement ( name ) {
if ( generator.current.useAnchor && generator.current.target === 'target' ) {
return `anchor.parentNode.insertBefore( ${name}, anchor )`;
generator.current.initStatements.push( deindent `
anchor.parentNode.insertBefore( ${name}, anchor );
` );
} else {
generator.current.initStatements.push( deindent `
${generator.current.target}.appendChild( ${name} );
` );
}
return `${generator.current.target}.appendChild( ${name} )`;
},
addRenderer ( fragment ) {

@ -47,10 +47,10 @@ export default {
${name}_iterations.length = ${listName}.length;
` );
const needsTeardown = generator.current.localElementDepth === 0;
const isToplevel = generator.current.localElementDepth === 0;
generator.current.teardownStatements.push( deindent`
for ( var i = 0; i < ${name}_iterations.length; i += 1 ) {
${name}_iterations[i].teardown( ${needsTeardown ? 'detach' : 'false'} );
${name}_iterations[i].teardown( ${isToplevel ? 'detach' : 'false'} );
}
` );

@ -19,7 +19,7 @@ export default {
teardown: []
};
const shouldDetach = generator.current.localElementDepth === 0;
const isToplevel = generator.current.localElementDepth === 0;
if ( isComponent ) {
generator.hasComponents = true;
@ -87,7 +87,7 @@ export default {
` );
}
local.teardown.push( `${name}.teardown( ${shouldDetach} );` );
local.teardown.push( `${name}.teardown( ${isToplevel ? 'detach' : 'false'} );` );
}
else {
@ -132,7 +132,7 @@ export default {
}
local.init.unshift( render );
if ( shouldDetach ) {
if ( isToplevel ) {
local.teardown.push( `if ( detach ) ${name}.parentNode.removeChild( ${name} );` );
}
}
@ -166,7 +166,6 @@ export default {
if ( isComponent ) return;
generator.current.initStatements.push(
generator.appendToTarget( name ) );
generator.createMountStatement( name );
}
};

@ -17,12 +17,12 @@ function generateBlock ( generator, node, name ) {
counter: counter()
});
node.children.forEach( generator.visit );
//generator.visit( node.children );
generator.addRenderer( generator.current );
generator.pop();
// unset the children, to avoid them being visited again
node.children = [];
}
function getConditionsAndBlocks ( generator, node, _name, i = 0 ) {
generator.addSourcemapLocations( node.expression );
const name = `${_name}_${i}`;
@ -86,6 +86,9 @@ export default {
}
` );
generator.current.teardownStatements.push( `if ( ${name} ) ${name}.teardown( detach );` );
const isToplevel = generator.current.localElementDepth === 0;
generator.current.teardownStatements.push( deindent`
if ( ${name} ) ${name}.teardown( ${isToplevel ? 'detach' : 'false'} );
` );
}
};

Loading…
Cancel
Save