diff --git a/src/generators/nodes/AwaitBlock.ts b/src/generators/nodes/AwaitBlock.ts index 0418d42710..fd11c561cf 100644 --- a/src/generators/nodes/AwaitBlock.ts +++ b/src/generators/nodes/AwaitBlock.ts @@ -21,7 +21,6 @@ export default class AwaitBlock extends Node { block: Block, state: State, inEachBlock: boolean, - componentStack: Node[], stripWhitespace: boolean, nextSibling: Node ) { @@ -53,7 +52,7 @@ export default class AwaitBlock extends Node { 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); if (child._block.dependencies.size > 0) { @@ -69,8 +68,7 @@ export default class AwaitBlock extends Node { build( block: Block, - state: State, - componentStack: Node[] + state: State ) { const name = this.var; @@ -211,7 +209,7 @@ export default class AwaitBlock extends Node { [this.pending, this.then, this.catch].forEach(status => { status.children.forEach(child => { - child.build(status._block, status._state, componentStack); + child.build(status._block, status._state); }); }); } diff --git a/src/generators/nodes/Component.ts b/src/generators/nodes/Component.ts index 75541bab86..61915dd43d 100644 --- a/src/generators/nodes/Component.ts +++ b/src/generators/nodes/Component.ts @@ -21,7 +21,6 @@ export default class Component extends Node { block: Block, state: State, inEachBlock: boolean, - componentStack: Node[], stripWhitespace: boolean, nextSibling: Node ) { @@ -62,15 +61,14 @@ export default class Component extends Node { this._slots = new Set(['default']); this.children.forEach(child => { - child.init(block, state, inEachBlock, componentStack.concat(this), stripWhitespace, nextSibling); + child.init(block, state, inEachBlock, stripWhitespace, nextSibling); }); } } build( block: Block, - state: State, - componentStack: Node[] + state: State ) { const { generator } = this; generator.hasComponents = true; @@ -84,7 +82,7 @@ export default class Component extends Node { componentInitProperties.push(`slots: { ${slots.join(', ')} }`); 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); } } + + nearestComponent() { + return this; + } } function mungeAttribute(attribute: Node, block: Block): Attribute { diff --git a/src/generators/nodes/EachBlock.ts b/src/generators/nodes/EachBlock.ts index eb2c348d96..af4633d1a2 100644 --- a/src/generators/nodes/EachBlock.ts +++ b/src/generators/nodes/EachBlock.ts @@ -25,7 +25,6 @@ export default class EachBlock extends Node { block: Block, state: State, inEachBlock: boolean, - componentStack: Node[], stripWhitespace: boolean, nextSibling: Node ) { @@ -89,7 +88,7 @@ export default class EachBlock extends Node { }); 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); this._block.hasUpdateMethod = this._block.dependencies.size > 0; @@ -106,7 +105,6 @@ export default class EachBlock extends Node { this.else._block, this.else._state, inEachBlock, - componentStack, stripWhitespace, nextSibling ); @@ -116,8 +114,7 @@ export default class EachBlock extends Node { build( block: Block, - state: State, - componentStack: Node[] + state: State ) { const { generator } = this; @@ -235,12 +232,12 @@ export default class EachBlock extends Node { } this.children.forEach((child: Node) => { - child.build(this._block, this._state, componentStack); + child.build(this._block, this._state); }); if (this.else) { this.else.children.forEach((child: Node) => { - child.build(this.else._block, this.else._state, componentStack); + child.build(this.else._block, this.else._state); }); } } diff --git a/src/generators/nodes/Element.ts b/src/generators/nodes/Element.ts index 10c6904e45..7b5edfeee7 100644 --- a/src/generators/nodes/Element.ts +++ b/src/generators/nodes/Element.ts @@ -28,7 +28,6 @@ export default class Element extends Node { block: Block, state: State, inEachBlock: boolean, - componentStack: Node[], stripWhitespace: boolean, nextSibling: Node ) { @@ -137,7 +136,7 @@ export default class Element extends Node { this.cannotUseInnerHTML(); this.slotted = true; // TODO validate slots — no nesting, no dynamic names... - const component = componentStack[componentStack.length - 1]; + const component = this.nearestComponent(); component._slots.add(slot); } @@ -159,14 +158,13 @@ export default class Element extends Node { if (this.children.length) { 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( block: Block, - state: State, - componentStack: Node[] + state: State ) { const { generator } = this; @@ -180,7 +178,7 @@ export default class Element extends Node { const slot = this.attributes.find((attribute: Node) => attribute.name === 'slot'); 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; block.addVariable(name); @@ -238,7 +236,7 @@ export default class Element extends Node { } } else { this.children.forEach((child: Node) => { - child.build(block, childState, componentStack); + child.build(block, childState); }); } diff --git a/src/generators/nodes/Fragment.ts b/src/generators/nodes/Fragment.ts index e6628628d9..82aa6c98d7 100644 --- a/src/generators/nodes/Fragment.ts +++ b/src/generators/nodes/Fragment.ts @@ -32,7 +32,7 @@ export default class Fragment extends Node { }); 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; } @@ -41,7 +41,7 @@ export default class Fragment extends Node { this.init(); this.children.forEach(child => { - child.build(this.block, this.state, []); + child.build(this.block, this.state); }); } } \ No newline at end of file diff --git a/src/generators/nodes/IfBlock.ts b/src/generators/nodes/IfBlock.ts index 835d6743fd..980067ac68 100644 --- a/src/generators/nodes/IfBlock.ts +++ b/src/generators/nodes/IfBlock.ts @@ -27,7 +27,6 @@ export default class IfBlock extends Node { block: Block, state: State, inEachBlock: boolean, - componentStack: Node[], stripWhitespace: boolean, nextSibling: Node ) { @@ -53,7 +52,7 @@ export default class IfBlock extends Node { node._state = state.child(); 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) { dynamic = true; @@ -78,7 +77,6 @@ export default class IfBlock extends Node { node.else._block, node.else._state, inEachBlock, - componentStack, stripWhitespace, nextSibling ); @@ -103,8 +101,7 @@ export default class IfBlock extends Node { build( block: Block, - state: State, - componentStack: Node[] + state: State ) { const name = this.var; @@ -114,7 +111,7 @@ export default class IfBlock extends Node { : (this.next && this.next.var) || 'null'; 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 if_name = hasElse ? '' : `if (${name}) `; @@ -169,8 +166,7 @@ function getBranches( generator: DomGenerator, block: Block, state: State, - node: Node, - componentStack: Node[] + node: Node ) { 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)) { branches.push( - ...getBranches(generator, block, state, node.else.children[0], componentStack) + ...getBranches(generator, block, state, node.else.children[0]) ); } else { branches.push({ @@ -200,7 +196,7 @@ function getBranches( }); if (node.else) { - visitChildren(generator, block, state, node.else, componentStack); + visitChildren(generator, block, state, node.else); } } @@ -211,11 +207,10 @@ function visitChildren( generator: DomGenerator, block: Block, state: State, - node: Node, - componentStack: Node[] + node: Node ) { node.children.forEach((child: Node) => { - child.build(node._block, node._state, componentStack); + child.build(node._block, node._state); }); } diff --git a/src/generators/nodes/MustacheTag.ts b/src/generators/nodes/MustacheTag.ts index d39e1479d1..c7122b9cfc 100644 --- a/src/generators/nodes/MustacheTag.ts +++ b/src/generators/nodes/MustacheTag.ts @@ -12,8 +12,7 @@ export default class MustacheTag extends Tag { build( block: Block, - state: State, - componentStack: Node[] + state: State ) { const { init } = this.renameThisMethod( block, diff --git a/src/generators/nodes/RawMustacheTag.ts b/src/generators/nodes/RawMustacheTag.ts index 7d9ccde65f..c067cb0683 100644 --- a/src/generators/nodes/RawMustacheTag.ts +++ b/src/generators/nodes/RawMustacheTag.ts @@ -13,8 +13,7 @@ export default class RawMustacheTag extends Tag { build( block: Block, - state: State, - componentStack: Node[] + state: State ) { const name = this.var; diff --git a/src/generators/nodes/Slot.ts b/src/generators/nodes/Slot.ts index a8f03927ef..aca17fc5a6 100644 --- a/src/generators/nodes/Slot.ts +++ b/src/generators/nodes/Slot.ts @@ -15,7 +15,6 @@ export default class Slot extends Element { block: Block, state: State, inEachBlock: boolean, - componentStack: Node[], stripWhitespace: boolean, nextSibling: Node ) { @@ -34,14 +33,13 @@ export default class Slot extends Element { }); if (this.children.length) { - this.initChildren(block, this._state, inEachBlock, componentStack, stripWhitespace, nextSibling); + this.initChildren(block, this._state, inEachBlock, stripWhitespace, nextSibling); } } build( block: Block, - state: State, - componentStack: Node[] + state: State ) { const { generator } = this; @@ -72,7 +70,7 @@ export default class Slot extends Element { block.builders.destroy.pushCondition(`!${content_name}`); this.children.forEach((child: Node) => { - child.build(block, state, componentStack); + child.build(block, state); }); block.builders.create.popCondition(); diff --git a/src/generators/nodes/Text.ts b/src/generators/nodes/Text.ts index 11926fd903..d848435820 100644 --- a/src/generators/nodes/Text.ts +++ b/src/generators/nodes/Text.ts @@ -33,8 +33,7 @@ export default class Text extends Node { build( block: Block, - state: State, - componentStack: Node[] + state: State ) { if (this.shouldSkip) return; diff --git a/src/generators/nodes/Window.ts b/src/generators/nodes/Window.ts index 6441133c74..ef81c75bde 100644 --- a/src/generators/nodes/Window.ts +++ b/src/generators/nodes/Window.ts @@ -35,7 +35,6 @@ export default class Window extends Node { block: Block, state: State, inEachBlock: boolean, - componentStack: Node[], stripWhitespace: boolean, nextSibling: Node ) { @@ -44,8 +43,7 @@ export default class Window extends Node { build( block: Block, - state: State, - componentStack: Node[] + state: State ) { const { generator } = this; diff --git a/src/generators/nodes/shared/Node.ts b/src/generators/nodes/shared/Node.ts index 82addcf3f9..eb1aea1b5c 100644 --- a/src/generators/nodes/shared/Node.ts +++ b/src/generators/nodes/shared/Node.ts @@ -38,7 +38,6 @@ export default class Node { block: Block, state: State, inEachBlock: boolean, - componentStack: Node[], stripWhitespace: boolean, nextSibling: Node ) { @@ -49,7 +48,6 @@ export default class Node { block: Block, state: State, inEachBlock: boolean, - componentStack: Node[], stripWhitespace: boolean, nextSibling: Node ) { @@ -89,7 +87,7 @@ export default class Node { cleaned.forEach((child: Node, i: number) => { 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; @@ -122,8 +120,7 @@ export default class Node { build( block: Block, - state: State, - componentStack: Node[] + state: State ) { // implemented by subclasses } @@ -137,4 +134,8 @@ export default class Node { isDomNode() { return this.type === 'Element' || this.type === 'Text' || this.type === 'MustacheTag'; } + + nearestComponent() { + if (this.parent) return this.parent.nearestComponent(); + } } \ No newline at end of file diff --git a/src/generators/server-side-rendering/interfaces.ts b/src/generators/server-side-rendering/interfaces.ts index 127d8bf34c..54b07be881 100644 --- a/src/generators/server-side-rendering/interfaces.ts +++ b/src/generators/server-side-rendering/interfaces.ts @@ -5,8 +5,7 @@ import { Node } from '../../interfaces'; export type Visitor = ( generator: SsrGenerator, block: Block, - node: Node, - componentStack: Node[] + node: Node ) => void; export interface AppendTarget {