more tidying up

pull/490/head
Rich-Harris 8 years ago
parent c7f15738ee
commit 92b49eed4e

@ -24,10 +24,6 @@ class DomGenerator extends Generator {
}; };
} }
addBlock ( block ) {
this.blocks.push( block );
}
helper ( name ) { helper ( name ) {
if ( this.options.dev && `${name}Dev` in shared ) { if ( this.options.dev && `${name}Dev` in shared ) {
name = `${name}Dev`; name = `${name}Dev`;
@ -59,8 +55,6 @@ export default function dom ( parsed, source, options ) {
visit( generator, block, state, node ); visit( generator, block, state, node );
}); });
generator.addBlock( block );
const builders = { const builders = {
main: new CodeBuilder(), main: new CodeBuilder(),
init: new CodeBuilder(), init: new CodeBuilder(),
@ -68,7 +62,7 @@ export default function dom ( parsed, source, options ) {
}; };
if ( options.dev ) { if ( options.dev ) {
builders._set.addBlock ( deindent` builders._set.addBlock( deindent`
if ( typeof newState !== 'object' ) { if ( typeof newState !== 'object' ) {
throw new Error( 'Component .set was called without an object of data key-values to update.' ); throw new Error( 'Component .set was called without an object of data key-values to update.' );
} }
@ -137,8 +131,9 @@ export default function dom ( parsed, source, options ) {
` ); ` );
} }
let i = generator.blocks.length; generator.blocks.forEach( block => {
while ( i-- ) builders.main.addBlock( generator.blocks[i].render() ); builders.main.addBlock( block.render() );
});
builders.init.addLine( `this._torndown = false;` ); builders.init.addLine( `this._torndown = false;` );

@ -19,6 +19,7 @@ const preprocessors = {
name: generator.getUniqueName( `create_if_block` ) name: generator.getUniqueName( `create_if_block` )
}); });
generator.blocks.push( node._block );
preprocessChildren( generator, node._block, node.children ); preprocessChildren( generator, node._block, node.children );
block.addDependencies( node._block.dependencies ); block.addDependencies( node._block.dependencies );
@ -29,6 +30,7 @@ const preprocessors = {
name: generator.getUniqueName( `create_if_block` ) name: generator.getUniqueName( `create_if_block` )
}); });
generator.blocks.push( node.else._block );
preprocessChildren( generator, node.else._block, node.else.children ); preprocessChildren( generator, node.else._block, node.else.children );
block.addDependencies( node.else._block.dependencies ); block.addDependencies( node.else._block.dependencies );
} }
@ -77,6 +79,7 @@ const preprocessors = {
params: block.params.concat( listName, context, indexName ) params: block.params.concat( listName, context, indexName )
}); });
generator.blocks.push( node._block );
preprocessChildren( generator, node._block, node.children ); preprocessChildren( generator, node._block, node.children );
block.addDependencies( node._block.dependencies ); block.addDependencies( node._block.dependencies );
@ -85,6 +88,7 @@ const preprocessors = {
name: generator.getUniqueName( `${node._block.name}_else` ) name: generator.getUniqueName( `${node._block.name}_else` )
}); });
generator.blocks.push( node.else._block );
preprocessChildren( generator, node.else._block, node.else.children ); preprocessChildren( generator, node.else._block, node.else.children );
} }
}, },
@ -115,6 +119,7 @@ const preprocessors = {
name: generator.getUniqueName( `create_${name}_yield_fragment` ) name: generator.getUniqueName( `create_${name}_yield_fragment` )
}); });
generator.blocks.push( node._block );
preprocessChildren( generator, node._block, node.children ); preprocessChildren( generator, node._block, node.children );
} }
@ -149,6 +154,7 @@ export default function preprocess ( generator, children ) {
dependencies: new Set() dependencies: new Set()
}); });
generator.blocks.push( block );
preprocessChildren( generator, block, children ); preprocessChildren( generator, block, children );
return block; return block;

@ -118,8 +118,6 @@ export default function visitComponent ( generator, block, state, node ) {
); );
componentInitProperties.push( `_yield: ${yieldFragment}`); componentInitProperties.push( `_yield: ${yieldFragment}`);
generator.addBlock( childBlock );
} }
const statements = []; const statements = [];

@ -75,25 +75,23 @@ export default function visitEachBlock ( generator, block, state, node ) {
` ); ` );
} }
const childBlock = node._block;
const childState = Object.assign( {}, state, { const childState = Object.assign( {}, state, {
parentNode: null, parentNode: null,
inEachBlock: true inEachBlock: true
}); });
node.children.forEach( child => { node.children.forEach( child => {
visit( generator, childBlock, childState, child ); visit( generator, node._block, childState, child );
}); });
generator.addBlock( childBlock );
if ( node.else ) { if ( node.else ) {
const childState = Object.assign( {}, state, {
parentNode: null
});
node.else.children.forEach( child => { node.else.children.forEach( child => {
visit( generator, node.else._block, childState, child ); visit( generator, node.else._block, childState, child );
}); });
generator.addBlock( node.else._block );
} }
} }

@ -75,7 +75,7 @@ export default function visitEventHandler ( generator, block, state, node, attri
`; `;
if ( shouldHoist ) { if ( shouldHoist ) {
generator.addBlock({ generator.blocks.push({
render: () => handler render: () => handler
}); });
} else { } else {

@ -11,7 +11,7 @@ function getConditionsAndBlocks ( generator, block, state, node ) {
block: node._block.name block: node._block.name
}]; }];
generateBlock( generator, block, state, node ); visitChildren( generator, block, state, node );
if ( isElseIf( node.else ) ) { if ( isElseIf( node.else ) ) {
conditionsAndBlocks.push( conditionsAndBlocks.push(
@ -24,25 +24,21 @@ function getConditionsAndBlocks ( generator, block, state, node ) {
}); });
if ( node.else ) { if ( node.else ) {
generateBlock( generator, block, state, node.else ); visitChildren( generator, block, state, node.else );
} }
} }
return conditionsAndBlocks; return conditionsAndBlocks;
} }
function generateBlock ( generator, block, state, node ) { function visitChildren ( generator, block, state, node ) {
const childBlock = node._block;
const childState = Object.assign( {}, state, { const childState = Object.assign( {}, state, {
parentNode: null parentNode: null
}); });
node.children.forEach( node => { node.children.forEach( child => {
visit( generator, childBlock, childState, node ); visit( generator, node._block, childState, child );
}); });
generator.addBlock( childBlock );
} }
export default function visitIfBlock ( generator, block, state, node ) { export default function visitIfBlock ( generator, block, state, node ) {

@ -1,9 +1,15 @@
export default { export default {
html: '<div><p>Hello</p></div>', html: `
<div><p>Hello</p></div>
`,
test ( assert, component, target ) { test ( assert, component, target ) {
assert.equal( component.get( 'data' ), 'Hello' ); assert.equal( component.get( 'data' ), 'Hello' );
component.set({data: 'World'});
component.set({ data: 'World' });
assert.equal( component.get( 'data' ), 'World' ); assert.equal( component.get( 'data' ), 'World' );
assert.equal( target.innerHTML, '<div><p>World<!----></p></div>' ); assert.htmlEqual( target.innerHTML, `
<div><p>World</p></div>
` );
} }
}; };

Loading…
Cancel
Save