remove componentStack

pull/992/head
Rich Harris 8 years ago
parent 56f34c70c1
commit fd24a5c8dc

@ -21,7 +21,6 @@ export default class AwaitBlock extends Node {
block: Block, block: Block,
state: State, state: State,
inEachBlock: boolean, inEachBlock: boolean,
componentStack: Node[],
stripWhitespace: boolean, stripWhitespace: boolean,
nextSibling: Node nextSibling: Node
) { ) {
@ -53,7 +52,7 @@ export default class AwaitBlock extends Node {
child._state = state.child(); child._state = state.child();
child.initChildren(child._block, child._state, inEachBlock, componentStack, stripWhitespace, nextSibling); child.initChildren(child._block, child._state, inEachBlock, stripWhitespace, nextSibling);
this.generator.blocks.push(child._block); this.generator.blocks.push(child._block);
if (child._block.dependencies.size > 0) { if (child._block.dependencies.size > 0) {
@ -69,8 +68,7 @@ export default class AwaitBlock extends Node {
build( build(
block: Block, block: Block,
state: State, state: State
componentStack: Node[]
) { ) {
const name = this.var; const name = this.var;
@ -211,7 +209,7 @@ export default class AwaitBlock extends Node {
[this.pending, this.then, this.catch].forEach(status => { [this.pending, this.then, this.catch].forEach(status => {
status.children.forEach(child => { status.children.forEach(child => {
child.build(status._block, status._state, componentStack); child.build(status._block, status._state);
}); });
}); });
} }

@ -21,7 +21,6 @@ export default class Component extends Node {
block: Block, block: Block,
state: State, state: State,
inEachBlock: boolean, inEachBlock: boolean,
componentStack: Node[],
stripWhitespace: boolean, stripWhitespace: boolean,
nextSibling: Node nextSibling: Node
) { ) {
@ -62,15 +61,14 @@ export default class Component extends Node {
this._slots = new Set(['default']); this._slots = new Set(['default']);
this.children.forEach(child => { this.children.forEach(child => {
child.init(block, state, inEachBlock, componentStack.concat(this), stripWhitespace, nextSibling); child.init(block, state, inEachBlock, stripWhitespace, nextSibling);
}); });
} }
} }
build( build(
block: Block, block: Block,
state: State, state: State
componentStack: Node[]
) { ) {
const { generator } = this; const { generator } = this;
generator.hasComponents = true; generator.hasComponents = true;
@ -84,7 +82,7 @@ export default class Component extends Node {
componentInitProperties.push(`slots: { ${slots.join(', ')} }`); componentInitProperties.push(`slots: { ${slots.join(', ')} }`);
this.children.forEach((child: Node) => { this.children.forEach((child: Node) => {
child.build(block, this._state, componentStack.concat(this)); child.build(block, this._state);
}); });
} }
@ -429,6 +427,10 @@ export default class Component extends Node {
block.builders.update.addBlock(updates); block.builders.update.addBlock(updates);
} }
} }
nearestComponent() {
return this;
}
} }
function mungeAttribute(attribute: Node, block: Block): Attribute { function mungeAttribute(attribute: Node, block: Block): Attribute {

@ -25,7 +25,6 @@ export default class EachBlock extends Node {
block: Block, block: Block,
state: State, state: State,
inEachBlock: boolean, inEachBlock: boolean,
componentStack: Node[],
stripWhitespace: boolean, stripWhitespace: boolean,
nextSibling: Node nextSibling: Node
) { ) {
@ -89,7 +88,7 @@ export default class EachBlock extends Node {
}); });
this.generator.blocks.push(this._block); this.generator.blocks.push(this._block);
this.initChildren(this._block, this._state, true, componentStack, stripWhitespace, nextSibling); this.initChildren(this._block, this._state, true, stripWhitespace, nextSibling);
block.addDependencies(this._block.dependencies); block.addDependencies(this._block.dependencies);
this._block.hasUpdateMethod = this._block.dependencies.size > 0; this._block.hasUpdateMethod = this._block.dependencies.size > 0;
@ -106,7 +105,6 @@ export default class EachBlock extends Node {
this.else._block, this.else._block,
this.else._state, this.else._state,
inEachBlock, inEachBlock,
componentStack,
stripWhitespace, stripWhitespace,
nextSibling nextSibling
); );
@ -116,8 +114,7 @@ export default class EachBlock extends Node {
build( build(
block: Block, block: Block,
state: State, state: State
componentStack: Node[]
) { ) {
const { generator } = this; const { generator } = this;
@ -235,12 +232,12 @@ export default class EachBlock extends Node {
} }
this.children.forEach((child: Node) => { this.children.forEach((child: Node) => {
child.build(this._block, this._state, componentStack); child.build(this._block, this._state);
}); });
if (this.else) { if (this.else) {
this.else.children.forEach((child: Node) => { this.else.children.forEach((child: Node) => {
child.build(this.else._block, this.else._state, componentStack); child.build(this.else._block, this.else._state);
}); });
} }
} }

@ -28,7 +28,6 @@ export default class Element extends Node {
block: Block, block: Block,
state: State, state: State,
inEachBlock: boolean, inEachBlock: boolean,
componentStack: Node[],
stripWhitespace: boolean, stripWhitespace: boolean,
nextSibling: Node nextSibling: Node
) { ) {
@ -137,7 +136,7 @@ export default class Element extends Node {
this.cannotUseInnerHTML(); this.cannotUseInnerHTML();
this.slotted = true; this.slotted = true;
// TODO validate slots — no nesting, no dynamic names... // TODO validate slots — no nesting, no dynamic names...
const component = componentStack[componentStack.length - 1]; const component = this.nearestComponent();
component._slots.add(slot); component._slots.add(slot);
} }
@ -159,14 +158,13 @@ export default class Element extends Node {
if (this.children.length) { if (this.children.length) {
if (this.name === 'pre' || this.name === 'textarea') stripWhitespace = false; if (this.name === 'pre' || this.name === 'textarea') stripWhitespace = false;
this.initChildren(block, this._state, inEachBlock, componentStack, stripWhitespace, nextSibling); this.initChildren(block, this._state, inEachBlock, stripWhitespace, nextSibling);
} }
} }
build( build(
block: Block, block: Block,
state: State, state: State
componentStack: Node[]
) { ) {
const { generator } = this; const { generator } = this;
@ -180,7 +178,7 @@ export default class Element extends Node {
const slot = this.attributes.find((attribute: Node) => attribute.name === 'slot'); const slot = this.attributes.find((attribute: Node) => attribute.name === 'slot');
const parentNode = this.slotted ? const parentNode = this.slotted ?
`${componentStack[componentStack.length - 1].var}._slotted.${slot.value[0].data}` : // TODO this looks bonkers `${this.nearestComponent().var}._slotted.${slot.value[0].data}` : // TODO this looks bonkers
state.parentNode; state.parentNode;
block.addVariable(name); block.addVariable(name);
@ -238,7 +236,7 @@ export default class Element extends Node {
} }
} else { } else {
this.children.forEach((child: Node) => { this.children.forEach((child: Node) => {
child.build(block, childState, componentStack); child.build(block, childState);
}); });
} }

@ -32,7 +32,7 @@ export default class Fragment extends Node {
}); });
this.generator.blocks.push(this.block); this.generator.blocks.push(this.block);
this.initChildren(this.block, this.state, false, [], true, null); this.initChildren(this.block, this.state, false, true, null);
this.block.hasUpdateMethod = true; this.block.hasUpdateMethod = true;
} }
@ -41,7 +41,7 @@ export default class Fragment extends Node {
this.init(); this.init();
this.children.forEach(child => { this.children.forEach(child => {
child.build(this.block, this.state, []); child.build(this.block, this.state);
}); });
} }
} }

@ -27,7 +27,6 @@ export default class IfBlock extends Node {
block: Block, block: Block,
state: State, state: State,
inEachBlock: boolean, inEachBlock: boolean,
componentStack: Node[],
stripWhitespace: boolean, stripWhitespace: boolean,
nextSibling: Node nextSibling: Node
) { ) {
@ -53,7 +52,7 @@ export default class IfBlock extends Node {
node._state = state.child(); node._state = state.child();
blocks.push(node._block); blocks.push(node._block);
node.initChildren(node._block, node._state, inEachBlock, componentStack, stripWhitespace, nextSibling); node.initChildren(node._block, node._state, inEachBlock, stripWhitespace, nextSibling);
if (node._block.dependencies.size > 0) { if (node._block.dependencies.size > 0) {
dynamic = true; dynamic = true;
@ -78,7 +77,6 @@ export default class IfBlock extends Node {
node.else._block, node.else._block,
node.else._state, node.else._state,
inEachBlock, inEachBlock,
componentStack,
stripWhitespace, stripWhitespace,
nextSibling nextSibling
); );
@ -103,8 +101,7 @@ export default class IfBlock extends Node {
build( build(
block: Block, block: Block,
state: State, state: State
componentStack: Node[]
) { ) {
const name = this.var; const name = this.var;
@ -114,7 +111,7 @@ export default class IfBlock extends Node {
: (this.next && this.next.var) || 'null'; : (this.next && this.next.var) || 'null';
const params = block.params.join(', '); const params = block.params.join(', ');
const branches = getBranches(this.generator, block, state, this, componentStack); const branches = getBranches(this.generator, block, state, this);
const hasElse = isElseBranch(branches[branches.length - 1]); const hasElse = isElseBranch(branches[branches.length - 1]);
const if_name = hasElse ? '' : `if (${name}) `; const if_name = hasElse ? '' : `if (${name}) `;
@ -169,8 +166,7 @@ function getBranches(
generator: DomGenerator, generator: DomGenerator,
block: Block, block: Block,
state: State, state: State,
node: Node, node: Node
componentStack: Node[]
) { ) {
block.contextualise(node.expression); // TODO remove block.contextualise(node.expression); // TODO remove
@ -184,11 +180,11 @@ function getBranches(
}, },
]; ];
visitChildren(generator, block, state, node, componentStack); visitChildren(generator, block, state, node);
if (isElseIf(node.else)) { if (isElseIf(node.else)) {
branches.push( branches.push(
...getBranches(generator, block, state, node.else.children[0], componentStack) ...getBranches(generator, block, state, node.else.children[0])
); );
} else { } else {
branches.push({ branches.push({
@ -200,7 +196,7 @@ function getBranches(
}); });
if (node.else) { if (node.else) {
visitChildren(generator, block, state, node.else, componentStack); visitChildren(generator, block, state, node.else);
} }
} }
@ -211,11 +207,10 @@ function visitChildren(
generator: DomGenerator, generator: DomGenerator,
block: Block, block: Block,
state: State, state: State,
node: Node, node: Node
componentStack: Node[]
) { ) {
node.children.forEach((child: Node) => { node.children.forEach((child: Node) => {
child.build(node._block, node._state, componentStack); child.build(node._block, node._state);
}); });
} }

@ -12,8 +12,7 @@ export default class MustacheTag extends Tag {
build( build(
block: Block, block: Block,
state: State, state: State
componentStack: Node[]
) { ) {
const { init } = this.renameThisMethod( const { init } = this.renameThisMethod(
block, block,

@ -13,8 +13,7 @@ export default class RawMustacheTag extends Tag {
build( build(
block: Block, block: Block,
state: State, state: State
componentStack: Node[]
) { ) {
const name = this.var; const name = this.var;

@ -15,7 +15,6 @@ export default class Slot extends Element {
block: Block, block: Block,
state: State, state: State,
inEachBlock: boolean, inEachBlock: boolean,
componentStack: Node[],
stripWhitespace: boolean, stripWhitespace: boolean,
nextSibling: Node nextSibling: Node
) { ) {
@ -34,14 +33,13 @@ export default class Slot extends Element {
}); });
if (this.children.length) { if (this.children.length) {
this.initChildren(block, this._state, inEachBlock, componentStack, stripWhitespace, nextSibling); this.initChildren(block, this._state, inEachBlock, stripWhitespace, nextSibling);
} }
} }
build( build(
block: Block, block: Block,
state: State, state: State
componentStack: Node[]
) { ) {
const { generator } = this; const { generator } = this;
@ -72,7 +70,7 @@ export default class Slot extends Element {
block.builders.destroy.pushCondition(`!${content_name}`); block.builders.destroy.pushCondition(`!${content_name}`);
this.children.forEach((child: Node) => { this.children.forEach((child: Node) => {
child.build(block, state, componentStack); child.build(block, state);
}); });
block.builders.create.popCondition(); block.builders.create.popCondition();

@ -33,8 +33,7 @@ export default class Text extends Node {
build( build(
block: Block, block: Block,
state: State, state: State
componentStack: Node[]
) { ) {
if (this.shouldSkip) return; if (this.shouldSkip) return;

@ -35,7 +35,6 @@ export default class Window extends Node {
block: Block, block: Block,
state: State, state: State,
inEachBlock: boolean, inEachBlock: boolean,
componentStack: Node[],
stripWhitespace: boolean, stripWhitespace: boolean,
nextSibling: Node nextSibling: Node
) { ) {
@ -44,8 +43,7 @@ export default class Window extends Node {
build( build(
block: Block, block: Block,
state: State, state: State
componentStack: Node[]
) { ) {
const { generator } = this; const { generator } = this;

@ -38,7 +38,6 @@ export default class Node {
block: Block, block: Block,
state: State, state: State,
inEachBlock: boolean, inEachBlock: boolean,
componentStack: Node[],
stripWhitespace: boolean, stripWhitespace: boolean,
nextSibling: Node nextSibling: Node
) { ) {
@ -49,7 +48,6 @@ export default class Node {
block: Block, block: Block,
state: State, state: State,
inEachBlock: boolean, inEachBlock: boolean,
componentStack: Node[],
stripWhitespace: boolean, stripWhitespace: boolean,
nextSibling: Node nextSibling: Node
) { ) {
@ -89,7 +87,7 @@ export default class Node {
cleaned.forEach((child: Node, i: number) => { cleaned.forEach((child: Node, i: number) => {
child.canUseInnerHTML = !this.generator.hydratable; child.canUseInnerHTML = !this.generator.hydratable;
child.init(block, state, inEachBlock, componentStack, stripWhitespace, cleaned[i + 1] || nextSibling); child.init(block, state, inEachBlock, stripWhitespace, cleaned[i + 1] || nextSibling);
if (child.shouldSkip) return; if (child.shouldSkip) return;
@ -122,8 +120,7 @@ export default class Node {
build( build(
block: Block, block: Block,
state: State, state: State
componentStack: Node[]
) { ) {
// implemented by subclasses // implemented by subclasses
} }
@ -137,4 +134,8 @@ export default class Node {
isDomNode() { isDomNode() {
return this.type === 'Element' || this.type === 'Text' || this.type === 'MustacheTag'; return this.type === 'Element' || this.type === 'Text' || this.type === 'MustacheTag';
} }
nearestComponent() {
if (this.parent) return this.parent.nearestComponent();
}
} }

@ -5,8 +5,7 @@ import { Node } from '../../interfaces';
export type Visitor = ( export type Visitor = (
generator: SsrGenerator, generator: SsrGenerator,
block: Block, block: Block,
node: Node, node: Node
componentStack: Node[]
) => void; ) => void;
export interface AppendTarget { export interface AppendTarget {

Loading…
Cancel
Save