Merge pull request #101 from Swatinem/refactors

various refactorings & fixes
pull/107/head
Rich Harris 8 years ago committed by GitHub
commit 9d6e947dd0

@ -15,28 +15,41 @@ export default function generate ( parsed, source, options ) {
const generator = { const generator = {
addElement ( name, renderStatement, needsIdentifier = false ) { addElement ( name, renderStatement, needsIdentifier = false ) {
const needsTeardown = generator.current.localElementDepth === 0; const isToplevel = generator.current.localElementDepth === 0;
if ( needsIdentifier || needsTeardown ) { if ( needsIdentifier || isToplevel ) {
generator.current.initStatements.push( deindent` generator.current.initStatements.push( deindent`
var ${name} = ${renderStatement}; var ${name} = ${renderStatement};
${generator.appendToTarget( name )};
` ); ` );
generator.createMountStatement( name );
} else { } else {
generator.current.initStatements.push( deindent` generator.current.initStatements.push( deindent`
${generator.current.target}.appendChild( ${renderStatement} ); ${generator.current.target}.appendChild( ${renderStatement} );
` ); ` );
} }
if ( needsTeardown ) { if ( isToplevel ) {
generator.current.teardownStatements.push( deindent` generator.current.teardownStatements.push( deindent`
if ( detach ) ${name}.parentNode.removeChild( ${name} ); if ( detach ) ${name}.parentNode.removeChild( ${name} );
` ); ` );
} }
}, },
appendToTarget ( name ) {
createMountStatement ( name ) {
if ( generator.current.useAnchor && generator.current.target === 'target' ) { 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} )`; },
createAnchor ( _name, description = '' ) {
const name = `${_name}_anchor`;
const statement = `document.createComment( ${JSON.stringify( description )} )`;
generator.addElement( name, statement, true );
return name;
}, },
addRenderer ( fragment ) { addRenderer ( fragment ) {

@ -5,7 +5,6 @@ export default {
enter ( generator, node ) { enter ( generator, node ) {
const i = generator.counters.each++; const i = generator.counters.each++;
const name = `eachBlock_${i}`; const name = `eachBlock_${i}`;
const anchor = `${name}_anchor`;
const renderer = `renderEachBlock_${i}`; const renderer = `renderEachBlock_${i}`;
const listName = `${name}_value`; const listName = `${name}_value`;
@ -14,7 +13,7 @@ export default {
const { dependencies, snippet } = generator.contextualise( node.expression ); const { dependencies, snippet } = generator.contextualise( node.expression );
generator.addElement( anchor, `document.createComment( ${JSON.stringify( `#each ${generator.source.slice( node.expression.start, node.expression.end )}` )} )`, true ); const anchor = generator.createAnchor( name, `#each ${generator.source.slice( node.expression.start, node.expression.end )}` );
generator.current.initStatements.push( deindent` generator.current.initStatements.push( deindent`
var ${name}_value = ${snippet}; var ${name}_value = ${snippet};
@ -47,10 +46,10 @@ export default {
${name}_iterations.length = ${listName}.length; ${name}_iterations.length = ${listName}.length;
` ); ` );
const needsTeardown = generator.current.localElementDepth === 0; const isToplevel = generator.current.localElementDepth === 0;
generator.current.teardownStatements.push( deindent` generator.current.teardownStatements.push( deindent`
for ( var i = 0; i < ${name}_iterations.length; i += 1 ) { 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: [] teardown: []
}; };
const shouldDetach = generator.current.localElementDepth === 0; const isToplevel = generator.current.localElementDepth === 0;
if ( isComponent ) { if ( isComponent ) {
generator.hasComponents = true; generator.hasComponents = true;
@ -87,7 +87,7 @@ export default {
` ); ` );
} }
local.teardown.push( `${name}.teardown( ${shouldDetach} );` ); local.teardown.push( `${name}.teardown( ${isToplevel ? 'detach' : 'false'} );` );
} }
else { else {
@ -132,7 +132,7 @@ export default {
} }
local.init.unshift( render ); local.init.unshift( render );
if ( shouldDetach ) { if ( isToplevel ) {
local.teardown.push( `if ( detach ) ${name}.parentNode.removeChild( ${name} );` ); local.teardown.push( `if ( detach ) ${name}.parentNode.removeChild( ${name} );` );
} }
} }
@ -166,7 +166,6 @@ export default {
if ( isComponent ) return; if ( isComponent ) return;
generator.current.initStatements.push( generator.createMountStatement( name );
generator.appendToTarget( name ) );
} }
}; };

@ -17,12 +17,12 @@ function generateBlock ( generator, node, name ) {
counter: counter() counter: counter()
}); });
node.children.forEach( generator.visit ); node.children.forEach( generator.visit );
//generator.visit( node.children );
generator.addRenderer( generator.current ); generator.addRenderer( generator.current );
generator.pop(); generator.pop();
// unset the children, to avoid them being visited again // unset the children, to avoid them being visited again
node.children = []; node.children = [];
} }
function getConditionsAndBlocks ( generator, node, _name, i = 0 ) { function getConditionsAndBlocks ( generator, node, _name, i = 0 ) {
generator.addSourcemapLocations( node.expression ); generator.addSourcemapLocations( node.expression );
const name = `${_name}_${i}`; const name = `${_name}_${i}`;
@ -56,13 +56,12 @@ export default {
const { params, target } = generator.current; const { params, target } = generator.current;
const name = `ifBlock_${i}`; const name = `ifBlock_${i}`;
const anchor = `${name}_anchor`;
const getBlock = `getBlock_${i}`; const getBlock = `getBlock_${i}`;
const currentBlock = `currentBlock_${i}`; const currentBlock = `currentBlock_${i}`;
const conditionsAndBlocks = getConditionsAndBlocks( generator, node, `renderIfBlock_${i}` ); const conditionsAndBlocks = getConditionsAndBlocks( generator, node, `renderIfBlock_${i}` );
generator.addElement( anchor, `document.createComment( ${JSON.stringify( `#if ${generator.source.slice( node.expression.start, node.expression.end )}` )} )`, true ); const anchor = generator.createAnchor( name, `#if ${generator.source.slice( node.expression.start, node.expression.end )}` );
generator.current.initStatements.push( deindent` generator.current.initStatements.push( deindent`
function ${getBlock} ( ${params} ) { function ${getBlock} ( ${params} ) {
@ -86,6 +85,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