mirror of https://github.com/sveltejs/svelte
parent
2d4f9eac00
commit
e7d324f305
@ -1,3 +1,3 @@
|
||||
export default {
|
||||
export default function visitComment () {
|
||||
// do nothing
|
||||
};
|
||||
}
|
||||
|
@ -1,79 +1,77 @@
|
||||
import flattenReference from '../../../utils/flattenReference.js';
|
||||
import visit from '../visit.js';
|
||||
|
||||
export default {
|
||||
enter ( generator, node ) {
|
||||
function stringify ( chunk ) {
|
||||
if ( chunk.type === 'Text' ) return chunk.data;
|
||||
if ( chunk.type === 'MustacheTag' ) {
|
||||
const { snippet } = generator.contextualise( chunk.expression );
|
||||
return '${__escape( ' + snippet + ')}';
|
||||
}
|
||||
export default function visitComponent ( generator, node ) {
|
||||
function stringify ( chunk ) {
|
||||
if ( chunk.type === 'Text' ) return chunk.data;
|
||||
if ( chunk.type === 'MustacheTag' ) {
|
||||
const { snippet } = generator.contextualise( chunk.expression );
|
||||
return '${__escape( ' + snippet + ')}';
|
||||
}
|
||||
}
|
||||
|
||||
const attributes = [];
|
||||
const bindings = [];
|
||||
const attributes = [];
|
||||
const bindings = [];
|
||||
|
||||
node.attributes.forEach( attribute => {
|
||||
if ( attribute.type === 'Attribute' ) {
|
||||
attributes.push( attribute );
|
||||
} else if ( attribute.type === 'Binding' ) {
|
||||
bindings.push( attribute );
|
||||
}
|
||||
});
|
||||
node.attributes.forEach( attribute => {
|
||||
if ( attribute.type === 'Attribute' ) {
|
||||
attributes.push( attribute );
|
||||
} else if ( attribute.type === 'Binding' ) {
|
||||
bindings.push( attribute );
|
||||
}
|
||||
});
|
||||
|
||||
const props = attributes
|
||||
.map( attribute => {
|
||||
let value;
|
||||
const props = attributes
|
||||
.map( attribute => {
|
||||
let value;
|
||||
|
||||
if ( attribute.value === true ) {
|
||||
value = `true`;
|
||||
} else if ( attribute.value.length === 0 ) {
|
||||
value = `''`;
|
||||
} else if ( attribute.value.length === 1 ) {
|
||||
const chunk = attribute.value[0];
|
||||
if ( chunk.type === 'Text' ) {
|
||||
value = isNaN( chunk.data ) ? JSON.stringify( chunk.data ) : chunk.data;
|
||||
} else {
|
||||
const { snippet } = generator.contextualise( chunk.expression );
|
||||
value = snippet;
|
||||
}
|
||||
if ( attribute.value === true ) {
|
||||
value = `true`;
|
||||
} else if ( attribute.value.length === 0 ) {
|
||||
value = `''`;
|
||||
} else if ( attribute.value.length === 1 ) {
|
||||
const chunk = attribute.value[0];
|
||||
if ( chunk.type === 'Text' ) {
|
||||
value = isNaN( chunk.data ) ? JSON.stringify( chunk.data ) : chunk.data;
|
||||
} else {
|
||||
value = '`' + attribute.value.map( stringify ).join( '' ) + '`';
|
||||
const { snippet } = generator.contextualise( chunk.expression );
|
||||
value = snippet;
|
||||
}
|
||||
} else {
|
||||
value = '`' + attribute.value.map( stringify ).join( '' ) + '`';
|
||||
}
|
||||
|
||||
return `${attribute.name}: ${value}`;
|
||||
})
|
||||
.concat( bindings.map( binding => {
|
||||
const { name, keypath } = flattenReference( binding.value );
|
||||
const value = generator.current.contexts.has( name ) ? keypath : `root.${keypath}`;
|
||||
return `${binding.name}: ${value}`;
|
||||
}))
|
||||
.join( ', ' );
|
||||
return `${attribute.name}: ${value}`;
|
||||
})
|
||||
.concat( bindings.map( binding => {
|
||||
const { name, keypath } = flattenReference( binding.value );
|
||||
const value = generator.current.contexts.has( name ) ? keypath : `root.${keypath}`;
|
||||
return `${binding.name}: ${value}`;
|
||||
}))
|
||||
.join( ', ' );
|
||||
|
||||
const expression = node.name === ':Self' ? generator.name : generator.importedComponents.get( node.name ) || `${generator.alias( 'template' )}.components.${node.name}`;
|
||||
const expression = node.name === ':Self' ? generator.name : generator.importedComponents.get( node.name ) || `${generator.alias( 'template' )}.components.${node.name}`;
|
||||
|
||||
bindings.forEach( binding => {
|
||||
generator.addBinding( binding, expression );
|
||||
});
|
||||
bindings.forEach( binding => {
|
||||
generator.addBinding( binding, expression );
|
||||
});
|
||||
|
||||
let open = `\${${expression}.render({${props}}`;
|
||||
let open = `\${${expression}.render({${props}}`;
|
||||
|
||||
if ( node.children.length ) {
|
||||
open += `, { yield: () => \``;
|
||||
}
|
||||
if ( node.children.length ) {
|
||||
open += `, { yield: () => \``;
|
||||
}
|
||||
|
||||
generator.append( open );
|
||||
generator.append( open );
|
||||
|
||||
this.elementDepth += 1;
|
||||
generator.elementDepth += 1;
|
||||
|
||||
node.children.forEach( child => {
|
||||
visit( child, generator );
|
||||
});
|
||||
node.children.forEach( child => {
|
||||
visit( child, generator );
|
||||
});
|
||||
|
||||
this.elementDepth -= 1;
|
||||
generator.elementDepth -= 1;
|
||||
|
||||
const close = node.children.length ? `\` })}` : ')}';
|
||||
generator.append( close );
|
||||
}
|
||||
};
|
||||
const close = node.children.length ? `\` })}` : ')}';
|
||||
generator.append( close );
|
||||
}
|
||||
|
@ -1,36 +1,34 @@
|
||||
import visit from '../visit.js';
|
||||
|
||||
export default {
|
||||
enter ( generator, node ) {
|
||||
const { dependencies, snippet } = generator.contextualise( node.expression );
|
||||
export default function visitEachBlock ( generator, node ) {
|
||||
const { dependencies, snippet } = generator.contextualise( node.expression );
|
||||
|
||||
const open = `\${ ${snippet}.map( ${ node.index ? `( ${node.context}, ${node.index} )` : node.context} => \``;
|
||||
generator.append( open );
|
||||
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 );
|
||||
contexts.set( node.context, node.context );
|
||||
// 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 );
|
||||
contexts.set( node.context, node.context );
|
||||
|
||||
const indexes = new Map( generator.current.indexes );
|
||||
if ( node.index ) indexes.set( node.index, node.context );
|
||||
const indexes = new Map( generator.current.indexes );
|
||||
if ( node.index ) indexes.set( node.index, node.context );
|
||||
|
||||
const contextDependencies = new Map( generator.current.contextDependencies );
|
||||
contextDependencies.set( node.context, dependencies );
|
||||
const contextDependencies = new Map( generator.current.contextDependencies );
|
||||
contextDependencies.set( node.context, dependencies );
|
||||
|
||||
generator.push({
|
||||
contexts,
|
||||
indexes,
|
||||
contextDependencies
|
||||
});
|
||||
generator.push({
|
||||
contexts,
|
||||
indexes,
|
||||
contextDependencies
|
||||
});
|
||||
|
||||
node.children.forEach( child => {
|
||||
visit( child, generator );
|
||||
});
|
||||
node.children.forEach( child => {
|
||||
visit( child, generator );
|
||||
});
|
||||
|
||||
const close = `\` ).join( '' )}`;
|
||||
generator.append( close );
|
||||
const close = `\` ).join( '' )}`;
|
||||
generator.append( close );
|
||||
|
||||
generator.pop();
|
||||
}
|
||||
};
|
||||
generator.pop();
|
||||
}
|
@ -1,72 +1,60 @@
|
||||
import Component from './Component.js';
|
||||
import visitComponent from './Component.js';
|
||||
import isVoidElementName from '../../../utils/isVoidElementName.js';
|
||||
import visit from '../visit.js';
|
||||
import Window from './meta/Window.js';
|
||||
import visitWindow from './meta/Window.js';
|
||||
|
||||
const meta = {
|
||||
':Window': Window
|
||||
':Window': visitWindow
|
||||
};
|
||||
|
||||
export default {
|
||||
enter ( generator, node ) {
|
||||
if ( node.name in meta ) {
|
||||
return meta[ node.name ].enter( generator, node );
|
||||
}
|
||||
|
||||
if ( generator.components.has( node.name ) || node.name === ':Self' ) {
|
||||
Component.enter( generator, node );
|
||||
return;
|
||||
}
|
||||
|
||||
let openingTag = `<${node.name}`;
|
||||
export default function visitElement ( generator, node ) {
|
||||
if ( node.name in meta ) {
|
||||
return meta[ node.name ]( generator, node );
|
||||
}
|
||||
|
||||
node.attributes.forEach( attribute => {
|
||||
if ( attribute.type !== 'Attribute' ) return;
|
||||
if ( generator.components.has( node.name ) || node.name === ':Self' ) {
|
||||
visitComponent( generator, node );
|
||||
return;
|
||||
}
|
||||
|
||||
let str = ` ${attribute.name}`;
|
||||
let openingTag = `<${node.name}`;
|
||||
|
||||
if ( attribute.value !== true ) {
|
||||
str += `="` + attribute.value.map( chunk => {
|
||||
if ( chunk.type === 'Text' ) {
|
||||
return chunk.data;
|
||||
}
|
||||
node.attributes.forEach( attribute => {
|
||||
if ( attribute.type !== 'Attribute' ) return;
|
||||
|
||||
const { snippet } = generator.contextualise( chunk.expression );
|
||||
return '${' + snippet + '}';
|
||||
}).join( '' ) + `"`;
|
||||
}
|
||||
let str = ` ${attribute.name}`;
|
||||
|
||||
openingTag += str;
|
||||
});
|
||||
if ( attribute.value !== true ) {
|
||||
str += `="` + attribute.value.map( chunk => {
|
||||
if ( chunk.type === 'Text' ) {
|
||||
return chunk.data;
|
||||
}
|
||||
|
||||
if ( generator.cssId && !generator.elementDepth ) {
|
||||
openingTag += ` ${generator.cssId}`;
|
||||
const { snippet } = generator.contextualise( chunk.expression );
|
||||
return '${' + snippet + '}';
|
||||
}).join( '' ) + `"`;
|
||||
}
|
||||
|
||||
openingTag += '>';
|
||||
openingTag += str;
|
||||
});
|
||||
|
||||
generator.append( openingTag );
|
||||
if ( generator.cssId && !generator.elementDepth ) {
|
||||
openingTag += ` ${generator.cssId}`;
|
||||
}
|
||||
|
||||
this.elementDepth += 1;
|
||||
openingTag += '>';
|
||||
|
||||
node.children.forEach( child => {
|
||||
visit( child, generator );
|
||||
});
|
||||
generator.append( openingTag );
|
||||
|
||||
this.elementDepth -= 1;
|
||||
generator.elementDepth += 1;
|
||||
|
||||
if ( node.name in meta ) {
|
||||
if ( meta[ node.name ].leave ) meta[ node.name ].leave( generator, node );
|
||||
return;
|
||||
}
|
||||
node.children.forEach( child => {
|
||||
visit( child, generator );
|
||||
});
|
||||
|
||||
if ( generator.components.has( node.name ) || node.name === ':Self' ) {
|
||||
Component.leave( generator, node );
|
||||
return;
|
||||
}
|
||||
generator.elementDepth -= 1;
|
||||
|
||||
if ( !isVoidElementName( node.name ) ) {
|
||||
generator.append( `</${node.name}>` );
|
||||
}
|
||||
if ( !isVoidElementName( node.name ) ) {
|
||||
generator.append( `</${node.name}>` );
|
||||
}
|
||||
};
|
||||
}
|
@ -1,29 +1,27 @@
|
||||
import visit from '../visit.js';
|
||||
|
||||
export default {
|
||||
enter ( generator, node ) {
|
||||
const { snippet } = generator.contextualise( node.expression );
|
||||
export default function visitIfBlock ( generator, node ) {
|
||||
const { snippet } = generator.contextualise( node.expression );
|
||||
|
||||
generator.append( '${ ' + snippet + ' ? `' );
|
||||
generator.append( '${ ' + snippet + ' ? `' );
|
||||
|
||||
generator.push({
|
||||
conditions: generator.current.conditions.concat( snippet )
|
||||
});
|
||||
generator.push({
|
||||
conditions: generator.current.conditions.concat( snippet )
|
||||
});
|
||||
|
||||
node.children.forEach( child => {
|
||||
visit( child, generator );
|
||||
});
|
||||
node.children.forEach( child => {
|
||||
visit( child, generator );
|
||||
});
|
||||
|
||||
generator.append( '` : `' );
|
||||
generator.append( '` : `' );
|
||||
|
||||
if ( node.else ) {
|
||||
node.else.children.forEach( child => {
|
||||
visit( child, generator );
|
||||
});
|
||||
}
|
||||
if ( node.else ) {
|
||||
node.else.children.forEach( child => {
|
||||
visit( child, generator );
|
||||
});
|
||||
}
|
||||
|
||||
generator.append( '` }' );
|
||||
generator.append( '` }' );
|
||||
|
||||
generator.pop();
|
||||
}
|
||||
};
|
||||
generator.pop();
|
||||
}
|
@ -1,6 +1,4 @@
|
||||
export default {
|
||||
enter ( generator, node ) {
|
||||
const { snippet } = generator.contextualise( node.expression );
|
||||
generator.append( '${__escape( ' + snippet + ' )}' );
|
||||
}
|
||||
};
|
||||
export default function visitMustacheTag ( generator, node ) {
|
||||
const { snippet } = generator.contextualise( node.expression );
|
||||
generator.append( '${__escape( ' + snippet + ' )}' );
|
||||
}
|
@ -1,6 +1,4 @@
|
||||
export default {
|
||||
enter ( generator, node ) {
|
||||
const { snippet } = generator.contextualise( node.expression );
|
||||
generator.append( '${' + snippet + '}' );
|
||||
}
|
||||
};
|
||||
export default function visitRawMustacheTag ( generator, node ) {
|
||||
const { snippet } = generator.contextualise( node.expression );
|
||||
generator.append( '${' + snippet + '}' );
|
||||
}
|
@ -1,5 +1,3 @@
|
||||
export default {
|
||||
enter ( generator, node ) {
|
||||
generator.append( node.data.replace( /\${/g, '\\${' ) );
|
||||
}
|
||||
};
|
||||
export default function visitText ( generator, node ) {
|
||||
generator.append( node.data.replace( /\${/g, '\\${' ) );
|
||||
}
|
@ -1,5 +1,3 @@
|
||||
export default {
|
||||
enter ( generator ) {
|
||||
generator.append( `\${options && options.yield ? options.yield() : ''}` );
|
||||
}
|
||||
};
|
||||
export default function visitYieldTag ( generator ) {
|
||||
generator.append( `\${options && options.yield ? options.yield() : ''}` );
|
||||
}
|
@ -1,9 +1,3 @@
|
||||
export default {
|
||||
enter () {
|
||||
// noop
|
||||
},
|
||||
|
||||
leave () {
|
||||
// noop
|
||||
}
|
||||
};
|
||||
export default function visitWindow () {
|
||||
// noop
|
||||
}
|
Loading…
Reference in new issue