diff --git a/src/generators/dom/Block.ts b/src/generators/dom/Block.ts index 596c59e1ef..5e50cbcb37 100644 --- a/src/generators/dom/Block.ts +++ b/src/generators/dom/Block.ts @@ -11,8 +11,6 @@ export interface BlockOptions { comment?: string; key?: string; contexts?: Map; - indexes?: Map; - changeableIndexes?: Map; indexNames?: Map; listNames?: Map; dependencies?: Set; @@ -27,8 +25,6 @@ export default class Block { first: string; contexts: Map; - indexes: Map; - changeableIndexes: Map; dependencies: Set; indexNames: Map; listNames: Map; @@ -68,8 +64,6 @@ export default class Block { this.first = null; this.contexts = options.contexts; - this.indexes = options.indexes; - this.changeableIndexes = options.changeableIndexes; this.dependencies = new Set(); this.indexNames = options.indexNames; diff --git a/src/generators/dom/TemplateScope.ts b/src/generators/dom/TemplateScope.ts index e8bfa23d9a..a5a7ec90a2 100644 --- a/src/generators/dom/TemplateScope.ts +++ b/src/generators/dom/TemplateScope.ts @@ -1,12 +1,9 @@ export default class TemplateScope { names: Set; - indexes: Set; dependenciesForName: Map; constructor(parent?: TemplateScope) { this.names = new Set(parent ? parent.names : []); - this.indexes = new Set(parent ? parent.names : []); - this.dependenciesForName = new Map(parent ? parent.dependenciesForName : []); } diff --git a/src/generators/nodes/Attribute.ts b/src/generators/nodes/Attribute.ts index 943076208f..354323c53e 100644 --- a/src/generators/nodes/Attribute.ts +++ b/src/generators/nodes/Attribute.ts @@ -147,7 +147,7 @@ export default class Attribute extends Node { if (this.chunks.length === 1) { // single {tag} — may be a non-string const expression = this.chunks[0]; - const { snippet, indexes } = expression; + const { snippet } = expression; value = snippet; @@ -164,9 +164,9 @@ export default class Attribute extends Node { if (chunk.type === 'Text') { return stringify(chunk.data); } else { - const { dependencies, snippet, indexes } = chunk; - - return chunk.getPrecedence() <= 13 ? `(${snippet})` : snippet; + return chunk.getPrecedence() <= 13 + ? `(${chunk.snippet})` + : chunk.snippet; } }) .join(' + '); diff --git a/src/generators/nodes/EachBlock.ts b/src/generators/nodes/EachBlock.ts index 3e596a4db0..bcd5c15f28 100644 --- a/src/generators/nodes/EachBlock.ts +++ b/src/generators/nodes/EachBlock.ts @@ -33,7 +33,6 @@ export default class EachBlock extends Node { this.scope = scope.child(); - // TODO handle indexes and destructuring this.scope.add(this.context, this.expression.dependencies); if (this.index) { @@ -76,8 +75,6 @@ export default class EachBlock extends Node { key: this.key, contexts: new Map(block.contexts), - indexes: new Map(block.indexes), - changeableIndexes: new Map(block.changeableIndexes), indexNames: new Map(block.indexNames), listNames: new Map(block.listNames) @@ -91,8 +88,6 @@ export default class EachBlock extends Node { if (this.index) { this.block.getUniqueName(this.index); // this prevents name collisions (#1254) - this.block.indexes.set(this.index, this.context); - this.block.changeableIndexes.set(this.index, this.key); // TODO is this right? } const context = this.block.getUniqueName(this.context); diff --git a/src/generators/nodes/Fragment.ts b/src/generators/nodes/Fragment.ts index a08b533504..a5e89c31c4 100644 --- a/src/generators/nodes/Fragment.ts +++ b/src/generators/nodes/Fragment.ts @@ -25,8 +25,6 @@ export default class Fragment extends Node { key: null, contexts: new Map(), - indexes: new Map(), - changeableIndexes: new Map(), indexNames: new Map(), listNames: new Map(), diff --git a/src/generators/nodes/Title.ts b/src/generators/nodes/Title.ts index 20e16f763d..fbc85c8380 100644 --- a/src/generators/nodes/Title.ts +++ b/src/generators/nodes/Title.ts @@ -30,7 +30,7 @@ export default class Title extends Node { if (this.children.length === 1) { // single {{tag}} — may be a non-string const { expression } = this.children[0]; - const { dependencies, snippet, indexes } = this.children[0].expression; + const { dependencies, snippet } = this.children[0].expression; value = snippet; dependencies.forEach(d => { diff --git a/src/generators/nodes/shared/Expression.ts b/src/generators/nodes/shared/Expression.ts index 59ddcba396..d5c0d827a1 100644 --- a/src/generators/nodes/shared/Expression.ts +++ b/src/generators/nodes/shared/Expression.ts @@ -62,7 +62,6 @@ export default class Expression { references: Set; dependencies: Set; contexts: Set; - indexes: Set; thisReferences: Array<{ start: number, end: number }>; @@ -80,8 +79,6 @@ export default class Expression { this.snippet = `[✂${info.start}-${info.end}✂]`; this.usesContext = false; - const contextDependencies = new Map(); // TODO - const indexes = new Map(); const dependencies = new Set(); @@ -137,7 +134,7 @@ export default class Expression { scope.dependenciesForName.get(name).forEach(dependency => { dependencies.add(dependency); }); - } else if (!indexes.has(name)) { + } else { dependencies.add(name); compiler.expectedProperties.add(name); } @@ -163,7 +160,6 @@ export default class Expression { this.dependencies = dependencies; this.contexts = new Set(); // TODO... - this.indexes = new Set(); // TODO... } getPrecedence() { diff --git a/src/generators/nodes/shared/Tag.ts b/src/generators/nodes/shared/Tag.ts index 8416a607c0..2f06decc8a 100644 --- a/src/generators/nodes/shared/Tag.ts +++ b/src/generators/nodes/shared/Tag.ts @@ -4,32 +4,30 @@ import Block from '../../dom/Block'; export default class Tag extends Node { expression: Expression; + shouldCache: boolean; constructor(compiler, parent, scope, info) { super(compiler, parent, scope, info); this.expression = new Expression(compiler, this, scope, info.expression); + + this.shouldCache = ( + info.expression.type !== 'Identifier' || + (this.expression.dependencies.size && scope.names.has(info.expression.name)) + ); } renameThisMethod( block: Block, update: ((value: string) => string) ) { - const { snippet, dependencies, indexes } = this.expression; - - const hasChangeableIndex = Array.from(indexes).some(index => block.changeableIndexes.get(index)); - - const shouldCache = ( - this.expression.node.type !== 'Identifier' || - block.contexts.has(this.expression.node.name) || - hasChangeableIndex - ); + const { snippet, dependencies } = this.expression; - const value = shouldCache && block.getUniqueName(`${this.var}_value`); - const content = shouldCache ? value : snippet; + const value = this.shouldCache && block.getUniqueName(`${this.var}_value`); + const content = this.shouldCache ? value : snippet; - if (shouldCache) block.addVariable(value, snippet); + if (this.shouldCache) block.addVariable(value, snippet); - if (dependencies.size || hasChangeableIndex) { + if (dependencies.size) { const changedCheck = ( (block.hasOutroMethod ? `#outroing || ` : '') + [...dependencies].map((dependency: string) => `changed.${dependency}`).join(' || ') @@ -37,7 +35,7 @@ export default class Tag extends Node { const updateCachedValue = `${value} !== (${value} = ${snippet})`; - const condition = shouldCache ? + const condition = this.shouldCache ? (dependencies.size ? `(${changedCheck}) && ${updateCachedValue}` : updateCachedValue) : changedCheck; diff --git a/src/generators/server-side-rendering/Block.ts b/src/generators/server-side-rendering/Block.ts index 88fdf257e0..112c812656 100644 --- a/src/generators/server-side-rendering/Block.ts +++ b/src/generators/server-side-rendering/Block.ts @@ -13,7 +13,6 @@ export default class Block { conditions: string[]; contexts: Map; - indexes: Map; constructor(options: BlockOptions) { Object.assign(this, options); @@ -41,8 +40,4 @@ export default class Block { child(options: BlockOptions) { return new Block(Object.assign({}, this, options, { parent: this })); } - - // contextualise(expression: Node, context?: string, isEventHandler?: boolean) { - // return this.generator.contextualise(this.contexts, this.indexes, expression, context, isEventHandler); - // } } diff --git a/src/generators/server-side-rendering/index.ts b/src/generators/server-side-rendering/index.ts index f49ac60728..a5882df18f 100644 --- a/src/generators/server-side-rendering/index.ts +++ b/src/generators/server-side-rendering/index.ts @@ -60,7 +60,6 @@ export default function ssr( const mainBlock = new Block({ generator, contexts: new Map(), - indexes: new Map(), conditions: [], }); diff --git a/src/generators/server-side-rendering/visitors/EachBlock.ts b/src/generators/server-side-rendering/visitors/EachBlock.ts index 4c542e65bb..76b89d64a4 100644 --- a/src/generators/server-side-rendering/visitors/EachBlock.ts +++ b/src/generators/server-side-rendering/visitors/EachBlock.ts @@ -25,9 +25,6 @@ export default function visitEachBlock( const contexts = new Map(block.contexts); contexts.set(node.context, node.context); - const indexes = new Map(block.indexes); - if (node.index) indexes.set(node.index, node.context); - if (node.destructuredContexts) { for (let i = 0; i < node.destructuredContexts.length; i += 1) { contexts.set(node.destructuredContexts[i], `${node.context}[${i}]`); @@ -35,8 +32,7 @@ export default function visitEachBlock( } const childBlock = block.child({ - contexts, - indexes + contexts }); node.children.forEach((child: Node) => {