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';
|
||||
|
||||
export default function visit ( node, generator ) {
|
||||
export default function visit ( generator, fragment, node ) {
|
||||
const visitor = visitors[ node.type ];
|
||||
visitor( generator, node );
|
||||
visitor( generator, fragment, node );
|
||||
}
|
@ -1,34 +1,32 @@
|
||||
import visit from '../visit.js';
|
||||
|
||||
export default function visitEachBlock ( generator, node ) {
|
||||
const { dependencies, snippet } = generator.contextualise( node.expression );
|
||||
export default function visitEachBlock ( generator, fragment, node ) {
|
||||
const { dependencies, snippet } = generator.contextualise( fragment, node.expression );
|
||||
|
||||
const open = `\${ ${snippet}.map( ${ node.index ? `( ${node.context}, ${node.index} )` : node.context} => \``;
|
||||
generator.append( open );
|
||||
|
||||
// TODO should this be the generator's job? It's duplicated between
|
||||
// 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 );
|
||||
|
||||
const indexes = new Map( generator.current.indexes );
|
||||
const indexes = new Map( fragment.indexes );
|
||||
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 );
|
||||
|
||||
generator.push({
|
||||
const childFragment = fragment.child({
|
||||
contexts,
|
||||
indexes,
|
||||
contextDependencies
|
||||
});
|
||||
|
||||
node.children.forEach( child => {
|
||||
visit( child, generator );
|
||||
visit( generator, childFragment, child );
|
||||
});
|
||||
|
||||
const close = `\` ).join( '' )}`;
|
||||
generator.append( close );
|
||||
|
||||
generator.pop();
|
||||
}
|
@ -1,27 +1,25 @@
|
||||
import visit from '../visit.js';
|
||||
|
||||
export default function visitIfBlock ( generator, node ) {
|
||||
const { snippet } = generator.contextualise( node.expression );
|
||||
export default function visitIfBlock ( generator, fragment, node ) {
|
||||
const { snippet } = generator.contextualise( fragment, node.expression );
|
||||
|
||||
generator.append( '${ ' + snippet + ' ? `' );
|
||||
|
||||
generator.push({
|
||||
conditions: generator.current.conditions.concat( snippet )
|
||||
const childFragment = fragment.child({
|
||||
conditions: fragment.conditions.concat( snippet )
|
||||
});
|
||||
|
||||
node.children.forEach( child => {
|
||||
visit( child, generator );
|
||||
visit( generator, childFragment, child );
|
||||
});
|
||||
|
||||
generator.append( '` : `' );
|
||||
|
||||
if ( node.else ) {
|
||||
node.else.children.forEach( child => {
|
||||
visit( child, generator );
|
||||
visit( generator, childFragment, child );
|
||||
});
|
||||
}
|
||||
|
||||
generator.append( '` }' );
|
||||
|
||||
generator.pop();
|
||||
}
|
@ -1,4 +1,4 @@
|
||||
export default function visitMustacheTag ( generator, node ) {
|
||||
const { snippet } = generator.contextualise( node.expression );
|
||||
export default function visitMustacheTag ( generator, fragment, node ) {
|
||||
const { snippet } = generator.contextualise( fragment, node.expression );
|
||||
generator.append( '${__escape( ' + snippet + ' )}' );
|
||||
}
|
@ -1,4 +1,4 @@
|
||||
export default function visitRawMustacheTag ( generator, node ) {
|
||||
const { snippet } = generator.contextualise( node.expression );
|
||||
export default function visitRawMustacheTag ( generator, fragment, node ) {
|
||||
const { snippet } = generator.contextualise( fragment, node.expression );
|
||||
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, '\\${' ) );
|
||||
}
|
Loading…
Reference in new issue