refactor node._state stuff

pull/803/head
Rich Harris 8 years ago
parent 6499d4714d
commit 9fae7671a4

@ -48,9 +48,7 @@ const preprocessors = {
const dependencies = block.findDependencies(node.expression);
block.addDependencies(dependencies);
node._state = getChildState(state, {
name: block.getUniqueName('text'),
});
node.var = block.getUniqueName('text');
},
RawMustacheTag: (
@ -65,8 +63,7 @@ const preprocessors = {
const dependencies = block.findDependencies(node.expression);
block.addDependencies(dependencies);
const name = block.getUniqueName('raw');
node._state = getChildState(state, { name });
node.var = block.getUniqueName('raw');
},
Text: (
@ -86,7 +83,7 @@ const preprocessors = {
}
node._state.shouldCreate = true;
node._state.name = block.getUniqueName(`text`);
node.var = block.getUniqueName(`text`);
},
IfBlock: (
@ -333,13 +330,12 @@ const preprocessors = {
generator.components.has(node.name) || node.name === ':Self';
if (isComponent) {
const name = block.getUniqueName(
node.var = block.getUniqueName(
(node.name === ':Self' ? generator.name : node.name).toLowerCase()
);
node._state = getChildState(state, {
name,
parentNode: `${name}._slotted.default`
parentNode: `${node.var}._slotted.default`
});
} else {
const slot = getStaticAttributeValue(node, 'slot');
@ -349,15 +345,14 @@ const preprocessors = {
component._slots.add(slot);
}
const name = block.getUniqueName(
node.var = block.getUniqueName(
node.name.replace(/[^a-zA-Z0-9_$]/g, '_')
);
node._state = getChildState(state, {
isTopLevel: false,
name,
parentNode: name,
parentNodes: block.getUniqueName(`${name}_nodes`),
parentNode: node.var,
parentNodes: block.getUniqueName(`${node.var}_nodes`),
parentNodeName: node.name,
namespace: node.name === 'svg'
? 'http://www.w3.org/2000/svg'

@ -48,7 +48,7 @@ export default function visitComponent(
) {
generator.hasComponents = true;
const name = node._state.name;
const name = node.var;
const componentInitProperties = [`_root: #component._root`];

@ -19,10 +19,10 @@ export default function visitEachBlock(
const iterations = block.getUniqueName(`${each_block}_iterations`);
const params = block.params.join(', ');
const needsAnchor = node.next ? (!node.next._state || !node.next._state.name) : !state.parentNode;
const needsAnchor = node.next ? (!node.next._state || !node.next.var) : !state.parentNode;
const anchor = needsAnchor
? block.getUniqueName(`${each_block}_anchor`)
: (node.next && node.next._state.name) || 'null';
: (node.next && node.next.var) || 'null';
// hack the sourcemap, so that if data is missing the bug
// is easy to find

@ -45,7 +45,7 @@ export default function visitElement(
}
if (node.name === 'slot') {
return visitSlot(generator, block, state, node, elementStack);
return visitSlot(generator, block, state, node, elementStack, componentStack);
}
if (generator.components.has(node.name) || node.name === ':Self') {
@ -57,7 +57,7 @@ export default function visitElement(
const slot = node.attributes.find((attribute: Node) => attribute.name === 'slot');
const parentNode = slot ?
`${componentStack[componentStack.length - 1]._state.name}._slotted.${slot.value[0].data}` : // TODO this looks bonkers
`${componentStack[componentStack.length - 1].var}._slotted.${slot.value[0].data}` : // TODO this looks bonkers
state.parentNode;
const isToplevel = !parentNode;

@ -13,7 +13,7 @@ export default function addTransitions(
outro
) {
if (intro === outro) {
const name = block.getUniqueName(`${state.name}_transition`);
const name = block.getUniqueName(`${node.var}_transition`);
const snippet = intro.expression
? block.contextualise(intro.expression).snippet
: '{}';
@ -24,23 +24,23 @@ export default function addTransitions(
block.builders.intro.addBlock(deindent`
#component._root._aftercreate.push( function () {
if ( !${name} ) ${name} = @wrapTransition( #component, ${state.name}, ${fn}, ${snippet}, true, null );
if ( !${name} ) ${name} = @wrapTransition( #component, ${node.var}, ${fn}, ${snippet}, true, null );
${name}.run( true, function () {
#component.fire( 'intro.end', { node: ${state.name} });
#component.fire( 'intro.end', { node: ${node.var} });
});
});
`);
block.builders.outro.addBlock(deindent`
${name}.run( false, function () {
#component.fire( 'outro.end', { node: ${state.name} });
#component.fire( 'outro.end', { node: ${node.var} });
if ( --#outros === 0 ) #outrocallback();
${name} = null;
});
`);
} else {
const introName = intro && block.getUniqueName(`${state.name}_intro`);
const outroName = outro && block.getUniqueName(`${state.name}_outro`);
const introName = intro && block.getUniqueName(`${node.var}_intro`);
const outroName = outro && block.getUniqueName(`${node.var}_outro`);
if (intro) {
block.addVariable(introName);
@ -59,9 +59,9 @@ export default function addTransitions(
block.builders.intro.addBlock(deindent`
#component._root._aftercreate.push( function () {
${introName} = @wrapTransition( #component, ${state.name}, ${fn}, ${snippet}, true, null );
${introName} = @wrapTransition( #component, ${node.var}, ${fn}, ${snippet}, true, null );
${introName}.run( true, function () {
#component.fire( 'intro.end', { node: ${state.name} });
#component.fire( 'intro.end', { node: ${node.var} });
});
});
`);
@ -78,9 +78,9 @@ export default function addTransitions(
// TODO hide elements that have outro'd (unless they belong to a still-outroing
// group) prior to their removal from the DOM
block.builders.outro.addBlock(deindent`
${outroName} = @wrapTransition( #component, ${state.name}, ${fn}, ${snippet}, false, null );
${outroName} = @wrapTransition( #component, ${node.var}, ${fn}, ${snippet}, false, null );
${outroName}.run( false, function () {
#component.fire( 'outro.end', { node: ${state.name} });
#component.fire( 'outro.end', { node: ${node.var} });
if ( --#outros === 0 ) #outrocallback();
});
`);

@ -79,10 +79,10 @@ export default function visitIfBlock(
) {
const name = generator.getUniqueName(`if_block`);
const needsAnchor = node.next ? (!node.next._state || !node.next._state.name) : !state.parentNode;
const needsAnchor = node.next ? (!node.next._state || !node.next.var) : !state.parentNode;
const anchor = needsAnchor
? block.getUniqueName(`${name}_anchor`)
: (node.next && node.next._state.name) || 'null';
: (node.next && node.next.var) || 'null';
const params = block.params.join(', ');
const branches = getBranches(generator, block, state, node, elementStack, componentStack);

@ -11,19 +11,17 @@ export default function visitMustacheTag(
state: State,
node: Node
) {
const { name } = node._state;
const { init } = visitTag(
generator,
block,
state,
node,
name,
value => `${name}.data = ${value};`
node.var,
value => `${node.var}.data = ${value};`
);
block.addElement(
name,
node.var,
`@createText( ${init} )`,
`@claimText( ${state.parentNodes}, ${init} )`,
state.parentNode

@ -11,18 +11,18 @@ export default function visitRawMustacheTag(
state: State,
node: Node
) {
const name = node._state.name;
const name = node.var;
const needsAnchorBefore = node.prev ? (node.prev.type !== 'Element' || !node.prev._state || !node.prev._state.name) : !state.parentNode;
const needsAnchorAfter = node.next ? (node.next.type !== 'Element' || !node.next._state || !node.next._state.name) : !state.parentNode;
const needsAnchorBefore = node.prev ? (node.prev.type !== 'Element' || !node.prev._state || !node.prev.var) : !state.parentNode;
const needsAnchorAfter = node.next ? (node.next.type !== 'Element' || !node.next._state || !node.next.var) : !state.parentNode;
const anchorBefore = needsAnchorBefore
? block.getUniqueName(`${name}_before`)
: (node.prev && node.prev._state.name) || 'null';
: (node.prev && node.prev.var) || 'null';
const anchorAfter = needsAnchorAfter
? block.getUniqueName(`${name}_after`)
: (node.next && node.next._state.name) || 'null';
: (node.next && node.next.var) || 'null';
let detach: string;
let insert: (content: string) => string;

@ -12,7 +12,7 @@ export default function visitText(
) {
if (!node._state.shouldCreate) return;
block.addElement(
node._state.name,
node.var,
`@createText( ${stringify(node.data)} )`,
`@claimText( ${state.parentNodes}, ${stringify(node.data)} )`,
state.parentNode

Loading…
Cancel
Save