rename state.target to more self-explanatory state.parentNode

pull/453/head
Rich-Harris 8 years ago
parent 4b3bdcf946
commit c87967be69

@ -3,16 +3,16 @@ export default class Fragment {
Object.assign( this, options ); Object.assign( this, options );
} }
addElement ( name, renderStatement, target, needsIdentifier = false ) { addElement ( name, renderStatement, parentNode, needsIdentifier = false ) {
const isToplevel = !target; const isToplevel = !parentNode;
if ( needsIdentifier || isToplevel ) { if ( needsIdentifier || isToplevel ) {
this.builders.create.addLine( this.builders.create.addLine(
`var ${name} = ${renderStatement};` `var ${name} = ${renderStatement};`
); );
this.createMountStatement( name, target ); this.createMountStatement( name, parentNode );
} else { } else {
this.builders.create.addLine( `${this.generator.helper( 'appendNode' )}( ${renderStatement}, ${target} );` ); this.builders.create.addLine( `${this.generator.helper( 'appendNode' )}( ${renderStatement}, ${parentNode} );` );
} }
if ( isToplevel ) { if ( isToplevel ) {
@ -24,14 +24,14 @@ export default class Fragment {
return new Fragment( Object.assign( {}, this, options, { parent: this } ) ); return new Fragment( Object.assign( {}, this, options, { parent: this } ) );
} }
createAnchor ( name, target ) { createAnchor ( name, parentNode ) {
const renderStatement = `${this.generator.helper( 'createComment' )}()`; const renderStatement = `${this.generator.helper( 'createComment' )}()`;
this.addElement( name, renderStatement, target, true ); this.addElement( name, renderStatement, parentNode, true );
} }
createMountStatement ( name, target ) { createMountStatement ( name, parentNode ) {
if ( target ) { if ( parentNode ) {
this.builders.create.addLine( `${this.generator.helper( 'appendNode' )}( ${name}, ${target} );` ); this.builders.create.addLine( `${this.generator.helper( 'appendNode' )}( ${name}, ${parentNode} );` );
} else { } else {
this.builders.mount.addLine( `${this.generator.helper( 'insertNode' )}( ${name}, target, anchor );` ); this.builders.mount.addLine( `${this.generator.helper( 'insertNode' )}( ${name}, target, anchor );` );
} }

@ -129,7 +129,7 @@ export default function dom ( parsed, source, options ) {
const state = { const state = {
namespace, namespace,
target: null, parentNode: null,
isTopLevel: true isTopLevel: true
}; };

@ -35,7 +35,7 @@ export default function visitComponent ( generator, fragment, state, node ) {
update: new CodeBuilder() update: new CodeBuilder()
}; };
const isToplevel = !state.target; const isToplevel = !state.parentNode;
generator.hasComponents = true; generator.hasComponents = true;
@ -70,7 +70,7 @@ export default function visitComponent ( generator, fragment, state, node ) {
} }
const componentInitProperties = [ const componentInitProperties = [
`target: ${!isToplevel ? state.target: 'null'}`, `target: ${!isToplevel ? state.parentNode: 'null'}`,
`_root: ${fragment.component}._root || ${fragment.component}` `_root: ${fragment.component}._root || ${fragment.component}`
]; ];
@ -85,7 +85,7 @@ export default function visitComponent ( generator, fragment, state, node ) {
}); });
const childState = Object.assign( {}, state, { const childState = Object.assign( {}, state, {
target: null parentNode: null
}); });
node.children.forEach( child => { node.children.forEach( child => {

@ -13,14 +13,14 @@ export default function visitEachBlock ( generator, fragment, state, node ) {
const listName = fragment.getUniqueName( `${name}_value` ); const listName = fragment.getUniqueName( `${name}_value` );
const isToplevel = !state.target; const isToplevel = !state.parentNode;
generator.addSourcemapLocations( node.expression ); generator.addSourcemapLocations( node.expression );
const { dependencies, snippet } = generator.contextualise( fragment, node.expression ); const { dependencies, snippet } = generator.contextualise( fragment, node.expression );
const anchor = fragment.getUniqueName( `${name}_anchor` ); const anchor = fragment.getUniqueName( `${name}_anchor` );
fragment.createAnchor( anchor, state.target ); fragment.createAnchor( anchor, state.parentNode );
const localVars = {}; const localVars = {};
@ -210,7 +210,7 @@ export default function visitEachBlock ( generator, fragment, state, node ) {
}); });
const childState = Object.assign( {}, state, { const childState = Object.assign( {}, state, {
target: null parentNode: null
}); });
node.children.forEach( child => { node.children.forEach( child => {

@ -32,7 +32,7 @@ export default function visitElement ( generator, fragment, state, node ) {
destroy: new CodeBuilder() destroy: new CodeBuilder()
}; };
const isToplevel = !state.target; const isToplevel = !state.parentNode;
addElementAttributes( generator, fragment, node, local ); addElementAttributes( generator, fragment, node, local );
@ -96,11 +96,11 @@ export default function visitElement ( generator, fragment, state, node ) {
if ( !local.update.isEmpty() ) fragment.builders.update.addBlock( local.update ); if ( !local.update.isEmpty() ) fragment.builders.update.addBlock( local.update );
if ( !local.destroy.isEmpty() ) fragment.builders.destroy.addBlock( local.destroy ); if ( !local.destroy.isEmpty() ) fragment.builders.destroy.addBlock( local.destroy );
fragment.createMountStatement( name, state.target ); fragment.createMountStatement( name, state.parentNode );
const childState = Object.assign( {}, state, { const childState = Object.assign( {}, state, {
isTopLevel: false, isTopLevel: false,
target: name, parentNode: name,
namespace: local.namespace namespace: local.namespace
}); });

@ -40,7 +40,7 @@ function generateBlock ( generator, fragment, state, node, name, type ) {
}); });
const childState = Object.assign( {}, state, { const childState = Object.assign( {}, state, {
target: null parentNode: null
}); });
// walk the children here // walk the children here
@ -58,11 +58,11 @@ export default function visitIfBlock ( generator, fragment, state, node ) {
const currentBlock = fragment.getUniqueName( `current_block` ); const currentBlock = fragment.getUniqueName( `current_block` );
const _currentBlock = fragment.getUniqueName( `_current_block` ); const _currentBlock = fragment.getUniqueName( `_current_block` );
const isToplevel = !state.target; const isToplevel = !state.parentNode;
const conditionsAndBlocks = getConditionsAndBlocks( generator, fragment, state, node, generator.getUniqueName( `render_if_block` ) ); const conditionsAndBlocks = getConditionsAndBlocks( generator, fragment, state, node, generator.getUniqueName( `render_if_block` ) );
const anchor = `${name}_anchor`; const anchor = `${name}_anchor`;
fragment.createAnchor( anchor, state.target ); fragment.createAnchor( anchor, state.parentNode );
fragment.builders.create.addBlock( deindent` fragment.builders.create.addBlock( deindent`
function ${getBlock} ( ${params} ) { function ${getBlock} ( ${params} ) {

@ -7,7 +7,7 @@ export default function visitMustacheTag ( generator, fragment, state, node ) {
const { snippet } = generator.contextualise( fragment, node.expression ); const { snippet } = generator.contextualise( fragment, node.expression );
fragment.builders.create.addLine( `var last_${name} = ${snippet};` ); fragment.builders.create.addLine( `var last_${name} = ${snippet};` );
fragment.addElement( name, `${generator.helper( 'createText' )}( last_${name} )`, state.target, true ); fragment.addElement( name, `${generator.helper( 'createText' )}( last_${name} )`, state.parentNode, true );
// TODO this should be unnecessary once we separate fragments from state // TODO this should be unnecessary once we separate fragments from state
const parentFragment = findBlock( fragment ); const parentFragment = findBlock( fragment );

@ -9,12 +9,12 @@ export default function visitRawMustacheTag ( generator, fragment, state, node )
// we would have used comments here, but the `insertAdjacentHTML` api only // we would have used comments here, but the `insertAdjacentHTML` api only
// exists for `Element`s. // exists for `Element`s.
const before = `${name}_before`; const before = `${name}_before`;
fragment.addElement( before, `${generator.helper( 'createElement' )}( 'noscript' )`, state.target, true ); fragment.addElement( before, `${generator.helper( 'createElement' )}( 'noscript' )`, state.parentNode, true );
const after = `${name}_after`; const after = `${name}_after`;
fragment.addElement( after, `${generator.helper( 'createElement' )}( 'noscript' )`, state.target, true ); fragment.addElement( after, `${generator.helper( 'createElement' )}( 'noscript' )`, state.parentNode, true );
const isToplevel = !state.target; const isToplevel = !state.parentNode;
fragment.builders.create.addLine( `var last_${name} = ${snippet};` ); fragment.builders.create.addLine( `var last_${name} = ${snippet};` );
const mountStatement = `${before}.insertAdjacentHTML( 'afterend', last_${name} );`; const mountStatement = `${before}.insertAdjacentHTML( 'afterend', last_${name} );`;

@ -4,5 +4,5 @@ export default function visitText ( generator, fragment, state, node ) {
} }
const name = fragment.getUniqueName( `text` ); const name = fragment.getUniqueName( `text` );
fragment.addElement( name, `${generator.helper( 'createText' )}( ${JSON.stringify( node.data )} )`, state.target, false ); fragment.addElement( name, `${generator.helper( 'createText' )}( ${JSON.stringify( node.data )} )`, state.parentNode, false );
} }

@ -1,9 +1,9 @@
export default function visitYieldTag ( generator, fragment, state ) { export default function visitYieldTag ( generator, fragment, state ) {
const anchor = `yield_anchor`; const anchor = `yield_anchor`;
fragment.createAnchor( anchor, state.target ); fragment.createAnchor( anchor, state.parentNode );
fragment.builders.mount.addLine( fragment.builders.mount.addLine(
`${fragment.component}._yield && ${fragment.component}._yield.mount( ${state.target || 'target'}, ${anchor} );` `${fragment.component}._yield && ${fragment.component}._yield.mount( ${state.parentNode || 'target'}, ${anchor} );`
); );
fragment.builders.destroy.addLine( fragment.builders.destroy.addLine(

Loading…
Cancel
Save