From 0cf9259a709250be6644afc105cf1f3b817f887a Mon Sep 17 00:00:00 2001 From: Rich Harris Date: Sat, 9 Dec 2017 15:07:13 -0500 Subject: [PATCH] remove inEachBlock --- src/generators/dom/State.ts | 2 -- src/generators/nodes/AwaitBlock.ts | 3 +-- src/generators/nodes/Binding.ts | 2 +- src/generators/nodes/Component.ts | 3 +-- src/generators/nodes/EachBlock.ts | 10 ++++------ src/generators/nodes/Element.ts | 11 +++++------ src/generators/nodes/Fragment.ts | 2 +- src/generators/nodes/IfBlock.ts | 4 +--- src/generators/nodes/Slot.ts | 3 +-- src/generators/nodes/Window.ts | 1 - src/generators/nodes/shared/Node.ts | 17 ++++++++++------- 11 files changed, 25 insertions(+), 33 deletions(-) diff --git a/src/generators/dom/State.ts b/src/generators/dom/State.ts index 23eed6677f..1ca0eff6de 100644 --- a/src/generators/dom/State.ts +++ b/src/generators/dom/State.ts @@ -5,7 +5,6 @@ interface StateData { parentNode?: string; parentNodes?: string; parentNodeName?: string; - inEachBlock?: boolean; allUsedContexts?: string[]; usesComponent?: boolean; selectBindingDependencies?: string[]; @@ -16,7 +15,6 @@ export default class State { parentNode?: string; parentNodes?: string; parentNodeName?: string; - inEachBlock?: boolean; allUsedContexts?: string[]; usesComponent?: boolean; selectBindingDependencies?: string[]; diff --git a/src/generators/nodes/AwaitBlock.ts b/src/generators/nodes/AwaitBlock.ts index fd11c561cf..03e2b0d80c 100644 --- a/src/generators/nodes/AwaitBlock.ts +++ b/src/generators/nodes/AwaitBlock.ts @@ -20,7 +20,6 @@ export default class AwaitBlock extends Node { init( block: Block, state: State, - inEachBlock: boolean, stripWhitespace: boolean, nextSibling: Node ) { @@ -52,7 +51,7 @@ export default class AwaitBlock extends Node { child._state = state.child(); - child.initChildren(child._block, child._state, inEachBlock, stripWhitespace, nextSibling); + child.initChildren(child._block, child._state, stripWhitespace, nextSibling); this.generator.blocks.push(child._block); if (child._block.dependencies.size > 0) { diff --git a/src/generators/nodes/Binding.ts b/src/generators/nodes/Binding.ts index 52de1cbde9..21cdbb9c4d 100644 --- a/src/generators/nodes/Binding.ts +++ b/src/generators/nodes/Binding.ts @@ -15,7 +15,7 @@ const readOnlyMediaAttributes = new Set([ export default class Binding extends Node { name: string; - value: Node[] + value: Node; expression: Node; munge( diff --git a/src/generators/nodes/Component.ts b/src/generators/nodes/Component.ts index 61915dd43d..a2e8f21de6 100644 --- a/src/generators/nodes/Component.ts +++ b/src/generators/nodes/Component.ts @@ -20,7 +20,6 @@ export default class Component extends Node { init( block: Block, state: State, - inEachBlock: boolean, stripWhitespace: boolean, nextSibling: Node ) { @@ -61,7 +60,7 @@ export default class Component extends Node { this._slots = new Set(['default']); this.children.forEach(child => { - child.init(block, state, inEachBlock, stripWhitespace, nextSibling); + child.init(block, state, stripWhitespace, nextSibling); }); } } diff --git a/src/generators/nodes/EachBlock.ts b/src/generators/nodes/EachBlock.ts index af4633d1a2..c50ba80049 100644 --- a/src/generators/nodes/EachBlock.ts +++ b/src/generators/nodes/EachBlock.ts @@ -8,6 +8,8 @@ import visitEachBlock from '../dom/visitors/EachBlock'; import createDebuggingComment from '../../utils/createDebuggingComment'; export default class EachBlock extends Node { + type: 'EachBlock'; + _block: Block; _state: State; expression: Node; @@ -24,7 +26,6 @@ export default class EachBlock extends Node { init( block: Block, state: State, - inEachBlock: boolean, stripWhitespace: boolean, nextSibling: Node ) { @@ -83,12 +84,10 @@ export default class EachBlock extends Node { params: block.params.concat(listName, context, indexName), }); - this._state = state.child({ - inEachBlock: true, - }); + this._state = state.child(); this.generator.blocks.push(this._block); - this.initChildren(this._block, this._state, true, stripWhitespace, nextSibling); + this.initChildren(this._block, this._state, stripWhitespace, nextSibling); block.addDependencies(this._block.dependencies); this._block.hasUpdateMethod = this._block.dependencies.size > 0; @@ -104,7 +103,6 @@ export default class EachBlock extends Node { this.else.initChildren( this.else._block, this.else._state, - inEachBlock, stripWhitespace, nextSibling ); diff --git a/src/generators/nodes/Element.ts b/src/generators/nodes/Element.ts index 7b5edfeee7..db284efc63 100644 --- a/src/generators/nodes/Element.ts +++ b/src/generators/nodes/Element.ts @@ -27,7 +27,6 @@ export default class Element extends Node { init( block: Block, state: State, - inEachBlock: boolean, stripWhitespace: boolean, nextSibling: Node ) { @@ -158,7 +157,7 @@ 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, stripWhitespace, nextSibling); + this.initChildren(block, this._state, stripWhitespace, nextSibling); } } @@ -249,7 +248,7 @@ export default class Element extends Node { // event handlers this.attributes.filter((a: Node) => a.type === 'EventHandler').forEach((attribute: Node) => { const isCustomEvent = generator.events.has(attribute.name); - const shouldHoist = !isCustomEvent && state.inEachBlock; + const shouldHoist = !isCustomEvent && this.hasAncestor('EachBlock'); const context = shouldHoist ? null : name; const usedContexts: string[] = []; @@ -279,7 +278,7 @@ export default class Element extends Node { }); } - const _this = context || 'this'; + const ctx = context || 'this'; const declarations = usedContexts.map(name => { if (name === 'state') { if (shouldHoist) childState.usesComponent = true; @@ -290,7 +289,7 @@ export default class Element extends Node { const indexName = block.indexNames.get(name); const contextName = block.contexts.get(name); - return `var ${listName} = ${_this}._svelte.${listName}, ${indexName} = ${_this}._svelte.${indexName}, ${contextName} = ${listName}[${indexName}];`; + return `var ${listName} = ${ctx}._svelte.${listName}, ${indexName} = ${ctx}._svelte.${indexName}, ${contextName} = ${listName}[${indexName}];`; }); // get a name for the event handler that is globally unique @@ -302,7 +301,7 @@ export default class Element extends Node { // create the handler body const handlerBody = deindent` ${childState.usesComponent && - `var ${block.alias('component')} = ${_this}._svelte.component;`} + `var ${block.alias('component')} = ${ctx}._svelte.component;`} ${declarations} ${attribute.expression ? `[✂${attribute.expression.start}-${attribute.expression.end}✂];` : diff --git a/src/generators/nodes/Fragment.ts b/src/generators/nodes/Fragment.ts index 82aa6c98d7..0147c1d9bf 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, true, null); this.block.hasUpdateMethod = true; } diff --git a/src/generators/nodes/IfBlock.ts b/src/generators/nodes/IfBlock.ts index 980067ac68..7e9b66468b 100644 --- a/src/generators/nodes/IfBlock.ts +++ b/src/generators/nodes/IfBlock.ts @@ -26,7 +26,6 @@ export default class IfBlock extends Node { init( block: Block, state: State, - inEachBlock: boolean, stripWhitespace: boolean, nextSibling: Node ) { @@ -52,7 +51,7 @@ export default class IfBlock extends Node { node._state = state.child(); blocks.push(node._block); - node.initChildren(node._block, node._state, inEachBlock, stripWhitespace, nextSibling); + node.initChildren(node._block, node._state, stripWhitespace, nextSibling); if (node._block.dependencies.size > 0) { dynamic = true; @@ -76,7 +75,6 @@ export default class IfBlock extends Node { node.else.initChildren( node.else._block, node.else._state, - inEachBlock, stripWhitespace, nextSibling ); diff --git a/src/generators/nodes/Slot.ts b/src/generators/nodes/Slot.ts index aca17fc5a6..62f44f6fc1 100644 --- a/src/generators/nodes/Slot.ts +++ b/src/generators/nodes/Slot.ts @@ -14,7 +14,6 @@ export default class Slot extends Element { init( block: Block, state: State, - inEachBlock: boolean, stripWhitespace: boolean, nextSibling: Node ) { @@ -33,7 +32,7 @@ export default class Slot extends Element { }); if (this.children.length) { - this.initChildren(block, this._state, inEachBlock, stripWhitespace, nextSibling); + this.initChildren(block, this._state, stripWhitespace, nextSibling); } } diff --git a/src/generators/nodes/Window.ts b/src/generators/nodes/Window.ts index ef81c75bde..2e7e4eb1e5 100644 --- a/src/generators/nodes/Window.ts +++ b/src/generators/nodes/Window.ts @@ -34,7 +34,6 @@ export default class Window extends Node { init( block: Block, state: State, - inEachBlock: boolean, stripWhitespace: boolean, nextSibling: Node ) { diff --git a/src/generators/nodes/shared/Node.ts b/src/generators/nodes/shared/Node.ts index eb1aea1b5c..71353d6163 100644 --- a/src/generators/nodes/shared/Node.ts +++ b/src/generators/nodes/shared/Node.ts @@ -37,7 +37,6 @@ export default class Node { init( block: Block, state: State, - inEachBlock: boolean, stripWhitespace: boolean, nextSibling: Node ) { @@ -47,7 +46,6 @@ export default class Node { initChildren( block: Block, state: State, - inEachBlock: boolean, stripWhitespace: boolean, nextSibling: Node ) { @@ -87,7 +85,7 @@ export default class Node { cleaned.forEach((child: Node, i: number) => { child.canUseInnerHTML = !this.generator.hydratable; - child.init(block, state, inEachBlock, stripWhitespace, cleaned[i + 1] || nextSibling); + child.init(block, state, stripWhitespace, cleaned[i + 1] || nextSibling); if (child.shouldSkip) return; @@ -101,7 +99,7 @@ export default class Node { // *unless* there is no whitespace between this node and its next sibling if (stripWhitespace && lastChild && lastChild.type === 'Text') { const shouldTrim = ( - nextSibling ? (nextSibling.type === 'Text' && /^\s/.test(nextSibling.data)) : !inEachBlock + nextSibling ? (nextSibling.type === 'Text' && /^\s/.test(nextSibling.data)) : !this.hasAncestor('EachBlock') ); if (shouldTrim) { @@ -126,15 +124,20 @@ export default class Node { } isChildOfComponent() { - return this.parent ? - this.parent.type === 'Component' || this.parent.isChildOfComponent() : - false; + // TODO remove this method + return this.hasAncestor('Component'); } isDomNode() { return this.type === 'Element' || this.type === 'Text' || this.type === 'MustacheTag'; } + hasAncestor(type: string) { + return this.parent ? + this.parent.type === type || this.parent.hasAncestor(type) : + false; + } + nearestComponent() { if (this.parent) return this.parent.nearestComponent(); }