mirror of https://github.com/sveltejs/svelte
parent
4ef3e005fb
commit
72f4941417
@ -0,0 +1,118 @@
|
|||||||
|
import Block from './Block.js';
|
||||||
|
|
||||||
|
function isElseIf ( node ) {
|
||||||
|
return node && node.children.length === 1 && node.children[0].type === 'IfBlock';
|
||||||
|
}
|
||||||
|
|
||||||
|
const preprocessors = {
|
||||||
|
MustacheTag: ( generator, block, node ) => {
|
||||||
|
const { dependencies } = block.contextualise( node.expression );
|
||||||
|
dependencies.forEach( dependency => {
|
||||||
|
block.dependencies.add( dependency );
|
||||||
|
});
|
||||||
|
},
|
||||||
|
|
||||||
|
IfBlock: ( generator, block, node ) => {
|
||||||
|
function attachBlocks ( node ) {
|
||||||
|
const { dependencies } = block.contextualise( node.expression );
|
||||||
|
|
||||||
|
node._block = block.child({
|
||||||
|
name: generator.getUniqueName( `create_if_block` )
|
||||||
|
});
|
||||||
|
|
||||||
|
preprocessChildren( generator, node._block, node.children );
|
||||||
|
|
||||||
|
if ( isElseIf( node.else ) ) {
|
||||||
|
attachBlocks( node.else );
|
||||||
|
} else if ( node.else ) {
|
||||||
|
node.else._block = block.child({
|
||||||
|
name: generator.getUniqueName( `create_if_block` )
|
||||||
|
});
|
||||||
|
|
||||||
|
preprocessChildren( generator, node.else._block, node.else.children );
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
attachBlocks ( node );
|
||||||
|
},
|
||||||
|
|
||||||
|
EachBlock: ( generator, block, node ) => {
|
||||||
|
const { dependencies } = block.contextualise( node.expression );
|
||||||
|
|
||||||
|
const indexNames = new Map( block.indexNames );
|
||||||
|
const indexName = node.index || block.getUniqueName( `${node.context}_index` );
|
||||||
|
indexNames.set( node.context, indexName );
|
||||||
|
|
||||||
|
const listNames = new Map( block.listNames );
|
||||||
|
const listName = block.getUniqueName( `each_block_value` );
|
||||||
|
listNames.set( node.context, listName );
|
||||||
|
|
||||||
|
const context = generator.getUniqueName( node.context );
|
||||||
|
const contexts = new Map( block.contexts );
|
||||||
|
contexts.set( node.context, context );
|
||||||
|
|
||||||
|
const indexes = new Map( block.indexes );
|
||||||
|
if ( node.index ) indexes.set( indexName, node.context );
|
||||||
|
|
||||||
|
const contextDependencies = new Map( block.contextDependencies );
|
||||||
|
contextDependencies.set( node.context, dependencies );
|
||||||
|
|
||||||
|
node._block = block.child({
|
||||||
|
name: generator.getUniqueName( 'create_each_block' ),
|
||||||
|
expression: node.expression,
|
||||||
|
context: node.context,
|
||||||
|
key: node.key,
|
||||||
|
|
||||||
|
contextDependencies,
|
||||||
|
contexts,
|
||||||
|
indexes,
|
||||||
|
|
||||||
|
indexNames,
|
||||||
|
listNames,
|
||||||
|
params: block.params.concat( listName, context, indexName )
|
||||||
|
});
|
||||||
|
|
||||||
|
preprocessChildren( generator, node._block, node.children );
|
||||||
|
},
|
||||||
|
|
||||||
|
Element: ( generator, block, node ) => {
|
||||||
|
// TODO attributes and bindings (and refs?)...
|
||||||
|
preprocessChildren( generator, block, node.children );
|
||||||
|
}
|
||||||
|
};
|
||||||
|
|
||||||
|
preprocessors.RawMustacheTag = preprocessors.MustacheTag;
|
||||||
|
|
||||||
|
function preprocessChildren ( generator, block, children ) {
|
||||||
|
children.forEach( child => {
|
||||||
|
const preprocess = preprocessors[ child.type ];
|
||||||
|
if ( preprocess ) preprocess( generator, block, child );
|
||||||
|
});
|
||||||
|
}
|
||||||
|
|
||||||
|
export default function preprocess ( generator, children, namespace ) {
|
||||||
|
const block = new Block({
|
||||||
|
generator,
|
||||||
|
name: generator.alias( 'create_main_fragment' ),
|
||||||
|
key: null,
|
||||||
|
|
||||||
|
contexts: new Map(),
|
||||||
|
indexes: new Map(),
|
||||||
|
|
||||||
|
params: [ 'root' ],
|
||||||
|
indexNames: new Map(),
|
||||||
|
listNames: new Map(),
|
||||||
|
|
||||||
|
dependencies: new Set()
|
||||||
|
});
|
||||||
|
|
||||||
|
const state = {
|
||||||
|
namespace,
|
||||||
|
parentNode: null,
|
||||||
|
isTopLevel: true
|
||||||
|
};
|
||||||
|
|
||||||
|
preprocessChildren( generator, block, children );
|
||||||
|
|
||||||
|
return { block, state };
|
||||||
|
}
|
@ -1,4 +1,4 @@
|
|||||||
export default function visitMustacheTag ( generator, block, node ) {
|
export default function visitMustacheTag ( generator, block, node ) {
|
||||||
const { snippet } = generator.contextualise( block, node.expression );
|
const { snippet } = block.contextualise( node.expression );
|
||||||
generator.append( '${__escape( ' + snippet + ' )}' );
|
generator.append( '${__escape( ' + snippet + ' )}' );
|
||||||
}
|
}
|
@ -1,4 +1,4 @@
|
|||||||
export default function visitRawMustacheTag ( generator, block, node ) {
|
export default function visitRawMustacheTag ( generator, block, node ) {
|
||||||
const { snippet } = generator.contextualise( block, node.expression );
|
const { snippet } = block.contextualise( node.expression );
|
||||||
generator.append( '${' + snippet + '}' );
|
generator.append( '${' + snippet + '}' );
|
||||||
}
|
}
|
Loading…
Reference in new issue