remove snippet from contextualise return value

pull/924/head
Rich Harris 8 years ago
parent 9f2c54359e
commit b49f7474ec

@ -214,8 +214,7 @@ export default class Generator {
isEventHandler: boolean isEventHandler: boolean
): { ): {
contexts: Set<string>, contexts: Set<string>,
indexes: Set<string>, indexes: Set<string>
snippet: string
} { } {
// this.addSourcemapLocations(expression); // this.addSourcemapLocations(expression);
@ -299,8 +298,7 @@ export default class Generator {
return { return {
contexts: usedContexts, contexts: usedContexts,
indexes: usedIndexes, indexes: usedIndexes
snippet: `[✂${expression.start}-${expression.end}✂]`,
}; };
} }
@ -694,6 +692,7 @@ export default class Generator {
}); });
return { return {
snippet: `[✂${node.start}-${node.end}✂]`,
dependencies: Array.from(dependencies) dependencies: Array.from(dependencies)
}; };
} }
@ -721,7 +720,6 @@ export default class Generator {
const name = node.destructuredContexts[i]; const name = node.destructuredContexts[i];
const value = `${node.context}[${i}]`; const value = `${node.context}[${i}]`;
// contexts.set(node.destructuredContexts[i], `${context}[${i}]`);
contextDependencies.set(name, node.metadata.dependencies); contextDependencies.set(name, node.metadata.dependencies);
} }
} }

@ -352,8 +352,8 @@ function mungeAttribute(attribute: Node, block: Block): Attribute {
} }
// simple dynamic attributes // simple dynamic attributes
const { snippet } = block.contextualise(value.expression); block.contextualise(value.expression); // TODO remove
const dependencies = value.metadata.dependencies; const { dependencies, snippet } = value.metadata;
// TODO only update attributes that have changed // TODO only update attributes that have changed
return { return {
@ -374,16 +374,14 @@ function mungeAttribute(attribute: Node, block: Block): Attribute {
if (chunk.type === 'Text') { if (chunk.type === 'Text') {
return stringify(chunk.data); return stringify(chunk.data);
} else { } else {
const { snippet } = block.contextualise( block.contextualise(chunk.expression); // TODO remove
chunk.expression const { dependencies, snippet } = chunk.metadata;
);
const dependencies = chunk.metadata.dependencies;
dependencies.forEach(dependency => { dependencies.forEach((dependency: string) => {
allDependencies.add(dependency); allDependencies.add(dependency);
}); });
return getExpressionPrecedence(chunk.expression) <= 13 ? `( ${snippet} )` : snippet; return getExpressionPrecedence(chunk.expression) <= 13 ? `(${snippet})` : snippet;
} }
}) })
.join(' + '); .join(' + ');
@ -398,10 +396,8 @@ function mungeAttribute(attribute: Node, block: Block): Attribute {
function mungeBinding(binding: Node, block: Block): Binding { function mungeBinding(binding: Node, block: Block): Binding {
const { name } = getObject(binding.value); const { name } = getObject(binding.value);
const { snippet, contexts } = block.contextualise( const { contexts } = block.contextualise(binding.value);
binding.value const { dependencies, snippet } = binding.metadata;
);
const dependencies = binding.metadata.dependencies;
const contextual = block.contexts.has(name); const contextual = block.contexts.has(name);

@ -45,7 +45,8 @@ export default function visitEachBlock(
mountOrIntro, mountOrIntro,
}; };
const { snippet } = block.contextualise(node.expression); block.contextualise(node.expression);
const { snippet } = node.metadata;
block.builders.init.addLine(`var ${each_block_value} = ${snippet};`); block.builders.init.addLine(`var ${each_block_value} = ${snippet};`);
@ -362,7 +363,7 @@ function unkeyed(
block: Block, block: Block,
state: State, state: State,
node: Node, node: Node,
snippet, snippet: string,
{ {
create_each_block, create_each_block,
each_block_value, each_block_value,
@ -402,8 +403,8 @@ function unkeyed(
} }
`); `);
const dependencies = node.metadata.dependencies;
const allDependencies = new Set(node._block.dependencies); const allDependencies = new Set(node._block.dependencies);
const { dependencies } = node.metadata;
dependencies.forEach((dependency: string) => { dependencies.forEach((dependency: string) => {
allDependencies.add(dependency); allDependencies.add(dependency);
}); });

@ -72,8 +72,8 @@ export default function visitAttribute(
if (attribute.value.length === 1) { if (attribute.value.length === 1) {
// single {{tag}} — may be a non-string // single {{tag}} — may be a non-string
const { expression } = attribute.value[0]; const { expression } = attribute.value[0];
const { snippet, indexes } = block.contextualise(expression); const { indexes } = block.contextualise(expression);
const dependencies = attribute.value[0].metadata.dependencies; const { dependencies, snippet } = attribute.value[0].metadata;
value = snippet; value = snippet;
dependencies.forEach(d => { dependencies.forEach(d => {
@ -96,8 +96,8 @@ export default function visitAttribute(
if (chunk.type === 'Text') { if (chunk.type === 'Text') {
return stringify(chunk.data); return stringify(chunk.data);
} else { } else {
const { snippet, indexes } = block.contextualise(chunk.expression); const { indexes } = block.contextualise(chunk.expression);
const dependencies = chunk.metadata.dependencies; const { dependencies, snippet } = chunk.metadata;
if (Array.from(indexes).some(index => block.changeableIndexes.get(index))) { if (Array.from(indexes).some(index => block.changeableIndexes.get(index))) {
hasChangeableIndex = true; hasChangeableIndex = true;

@ -36,8 +36,8 @@ export default function visitStyleAttribute(
if (chunk.type === 'Text') { if (chunk.type === 'Text') {
return stringify(chunk.data); return stringify(chunk.data);
} else { } else {
const { snippet, indexes } = block.contextualise(chunk.expression); const { indexes } = block.contextualise(chunk.expression);
const dependencies = chunk.metadata.dependencies; const { dependencies, snippet } = chunk.metadata;
if (Array.from(indexes).some(index => block.changeableIndexes.get(index))) { if (Array.from(indexes).some(index => block.changeableIndexes.get(index))) {
hasChangeableIndex = true; hasChangeableIndex = true;

@ -92,9 +92,8 @@ export default function addBindings(
let updateCondition: string; let updateCondition: string;
const { name } = getObject(binding.value); const { name } = getObject(binding.value);
const { snippet, contexts } = block.contextualise( const { contexts } = block.contextualise(binding.value);
binding.value const { snippet } = binding.metadata;
);
// special case: if you have e.g. `<input type=checkbox bind:checked=selected.done>` // special case: if you have e.g. `<input type=checkbox bind:checked=selected.done>`
// and `selected` is an object chosen with a <select>, then when `checked` changes, // and `selected` is an object chosen with a <select>, then when `checked` changes,

@ -15,10 +15,13 @@ export default function addTransitions(
if (!intro && !outro) return; if (!intro && !outro) return;
if (intro) block.contextualise(intro.expression); // TODO remove all these
if (outro) block.contextualise(outro.expression);
if (intro === outro) { if (intro === outro) {
const name = block.getUniqueName(`${node.var}_transition`); const name = block.getUniqueName(`${node.var}_transition`);
const snippet = intro.expression const snippet = intro.expression
? block.contextualise(intro.expression).snippet ? intro.expression.metadata.snippet
: '{}'; : '{}';
block.addVariable(name); block.addVariable(name);
@ -48,7 +51,7 @@ export default function addTransitions(
if (intro) { if (intro) {
block.addVariable(introName); block.addVariable(introName);
const snippet = intro.expression const snippet = intro.expression
? block.contextualise(intro.expression).snippet ? intro.expression.metadata.snippet
: '{}'; : '{}';
const fn = `%transitions-${intro.name}`; // TODO add built-in transitions? const fn = `%transitions-${intro.name}`; // TODO add built-in transitions?
@ -73,7 +76,7 @@ export default function addTransitions(
if (outro) { if (outro) {
block.addVariable(outroName); block.addVariable(outroName);
const snippet = outro.expression const snippet = outro.expression
? block.contextualise(outro.expression).snippet ? intro.expression.metadata.snippet
: '{}'; : '{}';
const fn = `%transitions-${outro.name}`; const fn = `%transitions-${outro.name}`;

@ -24,9 +24,11 @@ function getBranches(
elementStack: Node[], elementStack: Node[],
componentStack: Node[] componentStack: Node[]
) { ) {
block.contextualise(node.expression); // TODO remove
const branches = [ const branches = [
{ {
condition: block.contextualise(node.expression).snippet, condition: node.metadata.snippet,
block: node._block.name, block: node._block.name,
hasUpdateMethod: node._block.hasUpdateMethod, hasUpdateMethod: node._block.hasUpdateMethod,
hasIntroMethod: node._block.hasIntroMethod, hasIntroMethod: node._block.hasIntroMethod,

@ -12,8 +12,8 @@ export default function visitTag(
name: string, name: string,
update: (value: string) => string update: (value: string) => string
) { ) {
const { indexes, snippet } = block.contextualise(node.expression); const { indexes } = block.contextualise(node.expression);
const dependencies = node.metadata.dependencies; const { dependencies, snippet } = node.metadata;
const hasChangeableIndex = Array.from(indexes).some(index => block.changeableIndexes.get(index)); const hasChangeableIndex = Array.from(indexes).some(index => block.changeableIndexes.get(index));

@ -16,7 +16,8 @@ export default function visitComponent(
function stringifyAttribute(chunk: Node) { function stringifyAttribute(chunk: Node) {
if (chunk.type === 'Text') return chunk.data; if (chunk.type === 'Text') return chunk.data;
if (chunk.type === 'MustacheTag') { if (chunk.type === 'MustacheTag') {
const { snippet } = block.contextualise(chunk.expression); block.contextualise(chunk.expression);
const { snippet } = chunk.metadata;
return '${__escape( ' + snippet + ')}'; return '${__escape( ' + snippet + ')}';
} }
} }
@ -45,7 +46,8 @@ export default function visitComponent(
if (chunk.type === 'Text') { if (chunk.type === 'Text') {
value = isNaN(chunk.data) ? stringify(chunk.data) : chunk.data; value = isNaN(chunk.data) ? stringify(chunk.data) : chunk.data;
} else { } else {
const { snippet } = block.contextualise(chunk.expression); block.contextualise(chunk.expression);
const { snippet } = chunk.metadata;
value = snippet; value = snippet;
} }
} else { } else {

@ -8,7 +8,8 @@ export default function visitEachBlock(
block: Block, block: Block,
node: Node node: Node
) { ) {
const { dependencies, snippet } = block.contextualise(node.expression); block.contextualise(node.expression);
const { dependencies, snippet } = node.metadata;
const open = `\${ ${node.else ? `${snippet}.length ? ` : ''}${snippet}.map(${node.index ? `(${node.context}, ${node.index})` : node.context} => \``; const open = `\${ ${node.else ? `${snippet}.length ? ` : ''}${snippet}.map(${node.index ? `(${node.context}, ${node.index})` : node.context} => \``;
generator.append(open); generator.append(open);

@ -19,7 +19,8 @@ function stringifyAttributeValue(block: Block, chunks: Node[]) {
return escape(chunk.data).replace(/"/g, '&quot;'); return escape(chunk.data).replace(/"/g, '&quot;');
} }
const { snippet } = block.contextualise(chunk.expression); block.contextualise(chunk.expression);
const { snippet } = chunk.metadata;
return '${' + snippet + '}'; return '${' + snippet + '}';
}) })
.join(''); .join('');

@ -8,7 +8,8 @@ export default function visitIfBlock(
block: Block, block: Block,
node: Node node: Node
) { ) {
const { snippet } = block.contextualise(node.expression); block.contextualise(node.expression);
const { snippet } = node.metadata;
generator.append('${ ' + snippet + ' ? `'); generator.append('${ ' + snippet + ' ? `');

@ -7,6 +7,8 @@ export default function visitMustacheTag(
block: Block, block: Block,
node: Node node: Node
) { ) {
const { snippet } = block.contextualise(node.expression); block.contextualise(node.expression);
const { snippet } = node.metadata;
generator.append('${__escape(' + snippet + ')}'); generator.append('${__escape(' + snippet + ')}');
} }

@ -7,6 +7,8 @@ export default function visitRawMustacheTag(
block: Block, block: Block,
node: Node node: Node
) { ) {
const { snippet } = block.contextualise(node.expression); block.contextualise(node.expression);
const { snippet } = node.metadata;
generator.append('${' + snippet + '}'); generator.append('${' + snippet + '}');
} }

Loading…
Cancel
Save