remove inEachBlock

pull/992/head
Rich Harris 8 years ago
parent fd24a5c8dc
commit 0cf9259a70

@ -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[];

@ -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) {

@ -15,7 +15,7 @@ const readOnlyMediaAttributes = new Set([
export default class Binding extends Node {
name: string;
value: Node[]
value: Node;
expression: Node;
munge(

@ -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);
});
}
}

@ -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
);

@ -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}✂];` :

@ -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;
}

@ -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
);

@ -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);
}
}

@ -34,7 +34,6 @@ export default class Window extends Node {
init(
block: Block,
state: State,
inEachBlock: boolean,
stripWhitespace: boolean,
nextSibling: Node
) {

@ -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();
}

Loading…
Cancel
Save