turn dom generator visitors into functions

pull/453/head
Rich-Harris 8 years ago
parent e7d324f305
commit b8705a5b0c

@ -2,7 +2,5 @@ import visitors from './visitors/index.js';
export default function visit ( node, generator ) {
const visitor = visitors[ node.type ];
if ( visitor.enter ) visitor.enter( generator, node );
if ( visitor.leave ) visitor.leave( generator, node );
visitor( generator, node );
}

@ -1,3 +1,3 @@
export default {
export default function visitComment () {
// do nothing
};
}

@ -19,8 +19,7 @@ function stringifyProps ( props ) {
return `{ ${joined} }`;
}
export default {
enter ( generator, node ) {
export default function visitComponent ( generator, node ) {
const hasChildren = node.children.length > 0;
const { current } = generator;
const name = current.getUniqueName( capDown( node.name === ':Self' ? generator.name : node.name ) );
@ -177,5 +176,4 @@ export default {
generator.elementDepth -= 1;
generator.pop();
}
};
}

@ -3,8 +3,7 @@ import deindent from '../../../utils/deindent.js';
import getBuilders from '../utils/getBuilders.js';
import visit from '../visit.js';
export default {
enter ( generator, node ) {
export default function visitEachBlock ( generator, node ) {
const name = generator.getUniqueName( `each_block` );
const renderer = generator.getUniqueName( `render_each_block` );
const elseName = generator.getUniqueName( `${name}_else` );
@ -222,5 +221,4 @@ export default {
generator.addRenderer( generator.current );
generator.pop();
}
};
}

@ -2,23 +2,20 @@ import CodeBuilder from '../../../utils/CodeBuilder.js';
import deindent from '../../../utils/deindent.js';
import visit from '../visit.js';
import addElementAttributes from './attributes/addElementAttributes.js';
import Component from './Component.js';
import Window from './meta/Window.js';
import visitComponent from './Component.js';
import visitWindow from './meta/Window.js';
const meta = {
':Window': Window
':Window': visitWindow
};
export default {
enter ( generator, node ) {
export default function visitElement ( generator, node ) {
if ( node.name in meta ) {
return meta[ node.name ].enter( generator, node );
return meta[ node.name ]( generator, node );
}
const isComponent = generator.components.has( node.name ) || node.name === ':Self';
if ( isComponent ) {
return Component.enter( generator, node );
if ( generator.components.has( node.name ) || node.name === ':Self' ) {
return visitComponent( generator, node );
}
const name = generator.current.getUniqueName( node.name );
@ -118,15 +115,9 @@ export default {
generator.elementDepth -= 1;
if ( node.name in meta ) {
if ( meta[ node.name ].leave ) meta[ node.name ].leave( generator, node );
return;
}
if ( node.initialUpdate ) {
generator.current.builders.init.addBlock( node.initialUpdate );
}
generator.pop();
}
};
}

@ -30,8 +30,7 @@ function getConditionsAndBlocks ( generator, node, _name, i = 0 ) {
return conditionsAndBlocks;
}
export default {
enter ( generator, node ) {
export default function visitIfBlock ( generator, node ) {
const params = generator.current.params.join( ', ' );
const name = generator.getUniqueName( `if_block` );
const getBlock = generator.current.getUniqueName( `get_block` );
@ -77,5 +76,4 @@ export default {
generator.current.builders.teardown.addLine(
`if ( ${name} ) ${name}.teardown( ${isToplevel ? 'detach' : 'false'} );`
);
}
};
}

@ -1,8 +1,7 @@
import deindent from '../../../utils/deindent.js';
import findBlock from '../utils/findBlock.js';
export default {
enter ( generator, node ) {
export default function visitMustacheTag ( generator, node ) {
const name = generator.current.getUniqueName( 'text' );
const { snippet } = generator.contextualise( node.expression );
@ -18,5 +17,4 @@ export default {
${name}.data = last_${name} = ${fragment.tmp};
}
` );
}
};
}

@ -1,8 +1,7 @@
import deindent from '../../../utils/deindent.js';
import findBlock from '../utils/findBlock.js';
export default {
enter ( generator, node ) {
export default function visitRawMustacheTag ( generator, node ) {
const name = generator.current.getUniqueName( 'raw' );
const { snippet } = generator.contextualise( node.expression );
@ -39,5 +38,4 @@ export default {
` );
generator.current.builders.detachRaw.addBlock( detachStatement );
}
};
}

@ -1,10 +1,8 @@
export default {
enter ( generator, node ) {
export default function visitText ( generator, node ) {
if ( generator.current.namespace && !/\S/.test( node.data ) ) {
return;
}
const name = generator.current.getUniqueName( `text` );
generator.addElement( name, `${generator.helper( 'createText' )}( ${JSON.stringify( node.data )} )`, false );
}
};
}

@ -1,5 +1,4 @@
export default {
enter ( generator ) {
export default function visitYieldTag ( generator ) {
const anchor = `yield_anchor`;
generator.createAnchor( anchor );
@ -10,5 +9,4 @@ export default {
generator.current.builders.teardown.addLine(
`${generator.current.component}._yield && ${generator.current.component}._yield.teardown( detach );`
);
}
};
}

@ -11,8 +11,7 @@ const associatedEvents = {
scrollY: 'scroll'
};
export default {
enter ( generator, node ) {
export default function visitWindow ( generator, node ) {
const events = {};
node.attributes.forEach( attribute => {
@ -80,5 +79,4 @@ export default {
window.removeEventListener( '${event}', ${handlerName} );
` );
});
}
};
}
Loading…
Cancel
Save