remove inEachBlock

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

@ -5,7 +5,6 @@ interface StateData {
parentNode?: string; parentNode?: string;
parentNodes?: string; parentNodes?: string;
parentNodeName?: string; parentNodeName?: string;
inEachBlock?: boolean;
allUsedContexts?: string[]; allUsedContexts?: string[];
usesComponent?: boolean; usesComponent?: boolean;
selectBindingDependencies?: string[]; selectBindingDependencies?: string[];
@ -16,7 +15,6 @@ export default class State {
parentNode?: string; parentNode?: string;
parentNodes?: string; parentNodes?: string;
parentNodeName?: string; parentNodeName?: string;
inEachBlock?: boolean;
allUsedContexts?: string[]; allUsedContexts?: string[];
usesComponent?: boolean; usesComponent?: boolean;
selectBindingDependencies?: string[]; selectBindingDependencies?: string[];

@ -20,7 +20,6 @@ export default class AwaitBlock extends Node {
init( init(
block: Block, block: Block,
state: State, state: State,
inEachBlock: boolean,
stripWhitespace: boolean, stripWhitespace: boolean,
nextSibling: Node nextSibling: Node
) { ) {
@ -52,7 +51,7 @@ export default class AwaitBlock extends Node {
child._state = state.child(); 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); this.generator.blocks.push(child._block);
if (child._block.dependencies.size > 0) { if (child._block.dependencies.size > 0) {

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

@ -20,7 +20,6 @@ export default class Component extends Node {
init( init(
block: Block, block: Block,
state: State, state: State,
inEachBlock: boolean,
stripWhitespace: boolean, stripWhitespace: boolean,
nextSibling: Node nextSibling: Node
) { ) {
@ -61,7 +60,7 @@ 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, stripWhitespace, nextSibling); child.init(block, state, stripWhitespace, nextSibling);
}); });
} }
} }

@ -8,6 +8,8 @@ import visitEachBlock from '../dom/visitors/EachBlock';
import createDebuggingComment from '../../utils/createDebuggingComment'; import createDebuggingComment from '../../utils/createDebuggingComment';
export default class EachBlock extends Node { export default class EachBlock extends Node {
type: 'EachBlock';
_block: Block; _block: Block;
_state: State; _state: State;
expression: Node; expression: Node;
@ -24,7 +26,6 @@ export default class EachBlock extends Node {
init( init(
block: Block, block: Block,
state: State, state: State,
inEachBlock: boolean,
stripWhitespace: boolean, stripWhitespace: boolean,
nextSibling: Node nextSibling: Node
) { ) {
@ -83,12 +84,10 @@ export default class EachBlock extends Node {
params: block.params.concat(listName, context, indexName), params: block.params.concat(listName, context, indexName),
}); });
this._state = state.child({ this._state = state.child();
inEachBlock: true,
});
this.generator.blocks.push(this._block); 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); block.addDependencies(this._block.dependencies);
this._block.hasUpdateMethod = this._block.dependencies.size > 0; this._block.hasUpdateMethod = this._block.dependencies.size > 0;
@ -104,7 +103,6 @@ export default class EachBlock extends Node {
this.else.initChildren( this.else.initChildren(
this.else._block, this.else._block,
this.else._state, this.else._state,
inEachBlock,
stripWhitespace, stripWhitespace,
nextSibling nextSibling
); );

@ -27,7 +27,6 @@ export default class Element extends Node {
init( init(
block: Block, block: Block,
state: State, state: State,
inEachBlock: boolean,
stripWhitespace: boolean, stripWhitespace: boolean,
nextSibling: Node nextSibling: Node
) { ) {
@ -158,7 +157,7 @@ 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, stripWhitespace, nextSibling); this.initChildren(block, this._state, stripWhitespace, nextSibling);
} }
} }
@ -249,7 +248,7 @@ export default class Element extends Node {
// event handlers // event handlers
this.attributes.filter((a: Node) => a.type === 'EventHandler').forEach((attribute: Node) => { this.attributes.filter((a: Node) => a.type === 'EventHandler').forEach((attribute: Node) => {
const isCustomEvent = generator.events.has(attribute.name); const isCustomEvent = generator.events.has(attribute.name);
const shouldHoist = !isCustomEvent && state.inEachBlock; const shouldHoist = !isCustomEvent && this.hasAncestor('EachBlock');
const context = shouldHoist ? null : name; const context = shouldHoist ? null : name;
const usedContexts: string[] = []; 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 => { const declarations = usedContexts.map(name => {
if (name === 'state') { if (name === 'state') {
if (shouldHoist) childState.usesComponent = true; if (shouldHoist) childState.usesComponent = true;
@ -290,7 +289,7 @@ export default class Element extends Node {
const indexName = block.indexNames.get(name); const indexName = block.indexNames.get(name);
const contextName = block.contexts.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 // 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 // create the handler body
const handlerBody = deindent` const handlerBody = deindent`
${childState.usesComponent && ${childState.usesComponent &&
`var ${block.alias('component')} = ${_this}._svelte.component;`} `var ${block.alias('component')} = ${ctx}._svelte.component;`}
${declarations} ${declarations}
${attribute.expression ? ${attribute.expression ?
`[✂${attribute.expression.start}-${attribute.expression.end}✂];` : `[✂${attribute.expression.start}-${attribute.expression.end}✂];` :

@ -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, true, null);
this.block.hasUpdateMethod = true; this.block.hasUpdateMethod = true;
} }

@ -26,7 +26,6 @@ export default class IfBlock extends Node {
init( init(
block: Block, block: Block,
state: State, state: State,
inEachBlock: boolean,
stripWhitespace: boolean, stripWhitespace: boolean,
nextSibling: Node nextSibling: Node
) { ) {
@ -52,7 +51,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, stripWhitespace, nextSibling); node.initChildren(node._block, node._state, stripWhitespace, nextSibling);
if (node._block.dependencies.size > 0) { if (node._block.dependencies.size > 0) {
dynamic = true; dynamic = true;
@ -76,7 +75,6 @@ export default class IfBlock extends Node {
node.else.initChildren( node.else.initChildren(
node.else._block, node.else._block,
node.else._state, node.else._state,
inEachBlock,
stripWhitespace, stripWhitespace,
nextSibling nextSibling
); );

@ -14,7 +14,6 @@ export default class Slot extends Element {
init( init(
block: Block, block: Block,
state: State, state: State,
inEachBlock: boolean,
stripWhitespace: boolean, stripWhitespace: boolean,
nextSibling: Node nextSibling: Node
) { ) {
@ -33,7 +32,7 @@ export default class Slot extends Element {
}); });
if (this.children.length) { 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( init(
block: Block, block: Block,
state: State, state: State,
inEachBlock: boolean,
stripWhitespace: boolean, stripWhitespace: boolean,
nextSibling: Node nextSibling: Node
) { ) {

@ -37,7 +37,6 @@ export default class Node {
init( init(
block: Block, block: Block,
state: State, state: State,
inEachBlock: boolean,
stripWhitespace: boolean, stripWhitespace: boolean,
nextSibling: Node nextSibling: Node
) { ) {
@ -47,7 +46,6 @@ export default class Node {
initChildren( initChildren(
block: Block, block: Block,
state: State, state: State,
inEachBlock: boolean,
stripWhitespace: boolean, stripWhitespace: boolean,
nextSibling: Node nextSibling: Node
) { ) {
@ -87,7 +85,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, stripWhitespace, cleaned[i + 1] || nextSibling); child.init(block, state, stripWhitespace, cleaned[i + 1] || nextSibling);
if (child.shouldSkip) return; if (child.shouldSkip) return;
@ -101,7 +99,7 @@ export default class Node {
// *unless* there is no whitespace between this node and its next sibling // *unless* there is no whitespace between this node and its next sibling
if (stripWhitespace && lastChild && lastChild.type === 'Text') { if (stripWhitespace && lastChild && lastChild.type === 'Text') {
const shouldTrim = ( const shouldTrim = (
nextSibling ? (nextSibling.type === 'Text' && /^\s/.test(nextSibling.data)) : !inEachBlock nextSibling ? (nextSibling.type === 'Text' && /^\s/.test(nextSibling.data)) : !this.hasAncestor('EachBlock')
); );
if (shouldTrim) { if (shouldTrim) {
@ -126,15 +124,20 @@ export default class Node {
} }
isChildOfComponent() { isChildOfComponent() {
return this.parent ? // TODO remove this method
this.parent.type === 'Component' || this.parent.isChildOfComponent() : return this.hasAncestor('Component');
false;
} }
isDomNode() { isDomNode() {
return this.type === 'Element' || this.type === 'Text' || this.type === 'MustacheTag'; 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() { nearestComponent() {
if (this.parent) return this.parent.nearestComponent(); if (this.parent) return this.parent.nearestComponent();
} }

Loading…
Cancel
Save