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 ) { export default function visit ( node, generator ) {
const visitor = visitors[ node.type ]; const visitor = visitors[ node.type ];
visitor( generator, node );
if ( visitor.enter ) visitor.enter( generator, node );
if ( visitor.leave ) visitor.leave( generator, node );
} }

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

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

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

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

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

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

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

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

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

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