mirror of https://github.com/sveltejs/svelte
parent
cdb8b9d01d
commit
d93a3698fb
@ -0,0 +1,30 @@
|
|||||||
|
import deindent from '../../utils/deindent.js';
|
||||||
|
import flattenReference from '../../utils/flattenReference.js';
|
||||||
|
|
||||||
|
export default class Fragment {
|
||||||
|
constructor ( options ) {
|
||||||
|
Object.assign( this, options );
|
||||||
|
}
|
||||||
|
|
||||||
|
addBinding ( binding, name ) {
|
||||||
|
const conditions = [ `!( '${binding.name}' in root )`].concat( // TODO handle contextual bindings...
|
||||||
|
this.conditions.map( c => `(${c})` )
|
||||||
|
);
|
||||||
|
|
||||||
|
const { keypath } = flattenReference( binding.value );
|
||||||
|
|
||||||
|
this.generator.bindings.push( deindent`
|
||||||
|
if ( ${conditions.join( '&&' )} ) {
|
||||||
|
tmp = ${name}.data();
|
||||||
|
if ( '${keypath}' in tmp ) {
|
||||||
|
root.${binding.name} = tmp.${keypath};
|
||||||
|
settled = false;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
` );
|
||||||
|
}
|
||||||
|
|
||||||
|
child ( options ) {
|
||||||
|
return new Fragment( Object.assign( {}, this, options, { parent: this } ) );
|
||||||
|
}
|
||||||
|
}
|
@ -1,6 +1,6 @@
|
|||||||
import visitors from './visitors/index.js';
|
import visitors from './visitors/index.js';
|
||||||
|
|
||||||
export default function visit ( node, generator ) {
|
export default function visit ( generator, fragment, node ) {
|
||||||
const visitor = visitors[ node.type ];
|
const visitor = visitors[ node.type ];
|
||||||
visitor( generator, node );
|
visitor( generator, fragment, node );
|
||||||
}
|
}
|
@ -1,34 +1,32 @@
|
|||||||
import visit from '../visit.js';
|
import visit from '../visit.js';
|
||||||
|
|
||||||
export default function visitEachBlock ( generator, node ) {
|
export default function visitEachBlock ( generator, fragment, node ) {
|
||||||
const { dependencies, snippet } = generator.contextualise( node.expression );
|
const { dependencies, snippet } = generator.contextualise( fragment, node.expression );
|
||||||
|
|
||||||
const open = `\${ ${snippet}.map( ${ node.index ? `( ${node.context}, ${node.index} )` : node.context} => \``;
|
const open = `\${ ${snippet}.map( ${ node.index ? `( ${node.context}, ${node.index} )` : node.context} => \``;
|
||||||
generator.append( open );
|
generator.append( open );
|
||||||
|
|
||||||
// TODO should this be the generator's job? It's duplicated between
|
// TODO should this be the generator's job? It's duplicated between
|
||||||
// here and the equivalent DOM compiler visitor
|
// here and the equivalent DOM compiler visitor
|
||||||
const contexts = new Map( generator.current.contexts );
|
const contexts = new Map( fragment.contexts );
|
||||||
contexts.set( node.context, node.context );
|
contexts.set( node.context, node.context );
|
||||||
|
|
||||||
const indexes = new Map( generator.current.indexes );
|
const indexes = new Map( fragment.indexes );
|
||||||
if ( node.index ) indexes.set( node.index, node.context );
|
if ( node.index ) indexes.set( node.index, node.context );
|
||||||
|
|
||||||
const contextDependencies = new Map( generator.current.contextDependencies );
|
const contextDependencies = new Map( fragment.contextDependencies );
|
||||||
contextDependencies.set( node.context, dependencies );
|
contextDependencies.set( node.context, dependencies );
|
||||||
|
|
||||||
generator.push({
|
const childFragment = fragment.child({
|
||||||
contexts,
|
contexts,
|
||||||
indexes,
|
indexes,
|
||||||
contextDependencies
|
contextDependencies
|
||||||
});
|
});
|
||||||
|
|
||||||
node.children.forEach( child => {
|
node.children.forEach( child => {
|
||||||
visit( child, generator );
|
visit( generator, childFragment, child );
|
||||||
});
|
});
|
||||||
|
|
||||||
const close = `\` ).join( '' )}`;
|
const close = `\` ).join( '' )}`;
|
||||||
generator.append( close );
|
generator.append( close );
|
||||||
|
|
||||||
generator.pop();
|
|
||||||
}
|
}
|
@ -1,27 +1,25 @@
|
|||||||
import visit from '../visit.js';
|
import visit from '../visit.js';
|
||||||
|
|
||||||
export default function visitIfBlock ( generator, node ) {
|
export default function visitIfBlock ( generator, fragment, node ) {
|
||||||
const { snippet } = generator.contextualise( node.expression );
|
const { snippet } = generator.contextualise( fragment, node.expression );
|
||||||
|
|
||||||
generator.append( '${ ' + snippet + ' ? `' );
|
generator.append( '${ ' + snippet + ' ? `' );
|
||||||
|
|
||||||
generator.push({
|
const childFragment = fragment.child({
|
||||||
conditions: generator.current.conditions.concat( snippet )
|
conditions: fragment.conditions.concat( snippet )
|
||||||
});
|
});
|
||||||
|
|
||||||
node.children.forEach( child => {
|
node.children.forEach( child => {
|
||||||
visit( child, generator );
|
visit( generator, childFragment, child );
|
||||||
});
|
});
|
||||||
|
|
||||||
generator.append( '` : `' );
|
generator.append( '` : `' );
|
||||||
|
|
||||||
if ( node.else ) {
|
if ( node.else ) {
|
||||||
node.else.children.forEach( child => {
|
node.else.children.forEach( child => {
|
||||||
visit( child, generator );
|
visit( generator, childFragment, child );
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
generator.append( '` }' );
|
generator.append( '` }' );
|
||||||
|
|
||||||
generator.pop();
|
|
||||||
}
|
}
|
@ -1,4 +1,4 @@
|
|||||||
export default function visitMustacheTag ( generator, node ) {
|
export default function visitMustacheTag ( generator, fragment, node ) {
|
||||||
const { snippet } = generator.contextualise( node.expression );
|
const { snippet } = generator.contextualise( fragment, node.expression );
|
||||||
generator.append( '${__escape( ' + snippet + ' )}' );
|
generator.append( '${__escape( ' + snippet + ' )}' );
|
||||||
}
|
}
|
@ -1,4 +1,4 @@
|
|||||||
export default function visitRawMustacheTag ( generator, node ) {
|
export default function visitRawMustacheTag ( generator, fragment, node ) {
|
||||||
const { snippet } = generator.contextualise( node.expression );
|
const { snippet } = generator.contextualise( fragment, node.expression );
|
||||||
generator.append( '${' + snippet + '}' );
|
generator.append( '${' + snippet + '}' );
|
||||||
}
|
}
|
@ -1,3 +1,3 @@
|
|||||||
export default function visitText ( generator, node ) {
|
export default function visitText ( generator, fragment, node ) {
|
||||||
generator.append( node.data.replace( /\${/g, '\\${' ) );
|
generator.append( node.data.replace( /\${/g, '\\${' ) );
|
||||||
}
|
}
|
Loading…
Reference in new issue