diff --git a/src/generators/dom/Block.ts b/src/generators/dom/Block.ts index 5e50cbcb37..30545d0a70 100644 --- a/src/generators/dom/Block.ts +++ b/src/generators/dom/Block.ts @@ -10,7 +10,6 @@ export interface BlockOptions { generator?: DomGenerator; comment?: string; key?: string; - contexts?: Map; indexNames?: Map; listNames?: Map; dependencies?: Set; @@ -24,7 +23,6 @@ export default class Block { key: string; first: string; - contexts: Map; dependencies: Set; indexNames: Map; listNames: Map; @@ -63,7 +61,6 @@ export default class Block { this.key = options.key; this.first = null; - this.contexts = options.contexts; this.dependencies = new Set(); this.indexNames = options.indexNames; diff --git a/src/generators/nodes/AwaitBlock.ts b/src/generators/nodes/AwaitBlock.ts index a512375bf6..7cc7e8035a 100644 --- a/src/generators/nodes/AwaitBlock.ts +++ b/src/generators/nodes/AwaitBlock.ts @@ -52,14 +52,9 @@ export default class AwaitBlock extends Node { child.block = block.child({ comment: createDebuggingComment(child, this.compiler), - name: this.compiler.getUniqueName(`create_${status}_block`), - contexts: new Map(block.contexts) + name: this.compiler.getUniqueName(`create_${status}_block`) }); - if (arg) { - child.block.contexts.set(arg, arg); // TODO should be using getUniqueName - } - child.initChildren(child.block, stripWhitespace, nextSibling); this.compiler.blocks.push(child.block); diff --git a/src/generators/nodes/Binding.ts b/src/generators/nodes/Binding.ts index 8b74a14f3b..8b20f98e35 100644 --- a/src/generators/nodes/Binding.ts +++ b/src/generators/nodes/Binding.ts @@ -17,6 +17,7 @@ const readOnlyMediaAttributes = new Set([ export default class Binding extends Node { name: string; value: Expression; + isContextual: boolean; usesContext: boolean; obj: string; prop: string; @@ -30,6 +31,9 @@ export default class Binding extends Node { let obj; let prop; + const { name } = getObject(this.value.node); + this.isContextual = scope.names.has(name); + if (this.value.node.type === 'MemberExpression') { prop = `[✂${this.value.node.property.start}-${this.value.node.property.end}✂]`; if (!this.value.node.computed) prop = `'${prop}'`; @@ -37,7 +41,6 @@ export default class Binding extends Node { this.usesContext = true; } else { - const { name } = getObject(this.value.node); obj = 'ctx'; prop = `'${name}'`; @@ -182,11 +185,12 @@ function getEventHandler( snippet: string, dependencies: string[], value: string, + isContextual: boolean ) { const storeDependencies = [...dependencies].filter(prop => prop[0] === '$').map(prop => prop.slice(1)); dependencies = [...dependencies].filter(prop => prop[0] !== '$'); - if (block.contexts.has(name)) { + if (binding.isContextual) { const tail = binding.value.node.type === 'MemberExpression' ? getTailSnippet(binding.value.node) : ''; diff --git a/src/generators/nodes/Component.ts b/src/generators/nodes/Component.ts index 264b52d482..c4a401f897 100644 --- a/src/generators/nodes/Component.ts +++ b/src/generators/nodes/Component.ts @@ -233,7 +233,7 @@ export default class Component extends Node { let setFromChild; - if (block.contexts.has(key)) { // TODO remove block.contexts + if (binding.isContextual) { const computed = isComputed(binding.value.node); const tail = binding.value.node.type === 'MemberExpression' ? getTailSnippet(binding.value.node) : ''; diff --git a/src/generators/nodes/EachBlock.ts b/src/generators/nodes/EachBlock.ts index bcd5c15f28..394b82369b 100644 --- a/src/generators/nodes/EachBlock.ts +++ b/src/generators/nodes/EachBlock.ts @@ -71,11 +71,8 @@ export default class EachBlock extends Node { this.block = block.child({ comment: createDebuggingComment(this, this.compiler), name: this.compiler.getUniqueName('create_each_block'), - context: this.context, key: this.key, - contexts: new Map(block.contexts), - indexNames: new Map(block.indexNames), listNames: new Map(block.listNames) }); @@ -90,16 +87,6 @@ export default class EachBlock extends Node { this.block.getUniqueName(this.index); // this prevents name collisions (#1254) } - const context = this.block.getUniqueName(this.context); - this.block.contexts.set(this.context, context); // TODO this is now redundant? - - if (this.destructuredContexts) { - for (let i = 0; i < this.destructuredContexts.length; i += 1) { - const context = this.block.getUniqueName(this.destructuredContexts[i]); - this.block.contexts.set(this.destructuredContexts[i], context); - } - } - this.contextProps = [ `${listName}: ${listName}`, `${this.context}: ${listName}[#i]`, diff --git a/src/generators/nodes/Fragment.ts b/src/generators/nodes/Fragment.ts index a5e89c31c4..4b2025f8d6 100644 --- a/src/generators/nodes/Fragment.ts +++ b/src/generators/nodes/Fragment.ts @@ -24,8 +24,6 @@ export default class Fragment extends Node { name: '@create_main_fragment', key: null, - contexts: new Map(), - indexNames: new Map(), listNames: new Map(), diff --git a/src/generators/nodes/Title.ts b/src/generators/nodes/Title.ts index fbc85c8380..79ca67ec01 100644 --- a/src/generators/nodes/Title.ts +++ b/src/generators/nodes/Title.ts @@ -6,10 +6,18 @@ import mapChildren from './shared/mapChildren'; export default class Title extends Node { type: 'Title'; children: any[]; // TODO + shouldCache: boolean; constructor(compiler, parent, scope, info) { super(compiler, parent, scope, info); this.children = mapChildren(compiler, parent, scope, info.children); + + this.shouldCache = info.children.length === 1 + ? ( + info.children[0].type !== 'Identifier' || + scope.names.has(info.children[0].name) + ) + : true; } build( @@ -23,7 +31,6 @@ export default class Title extends Node { let value; const allDependencies = new Set(); - let shouldCache; // TODO some of this code is repeated in Tag.ts — would be good to // DRY it out if that's possible without introducing crazy indirection @@ -36,11 +43,6 @@ export default class Title extends Node { dependencies.forEach(d => { allDependencies.add(d); }); - - shouldCache = ( - expression.type !== 'Identifier' || - block.contexts.has(expression.name) - ); } else { // '{foo} {bar}' — treat as string concatenation value = @@ -60,23 +62,21 @@ export default class Title extends Node { } }) .join(' + '); - - shouldCache = true; } - const last = shouldCache && block.getUniqueName( + const last = this.shouldCache && block.getUniqueName( `title_value` ); - if (shouldCache) block.addVariable(last); + if (this.shouldCache) block.addVariable(last); let updater; - const init = shouldCache ? `${last} = ${value}` : value; + const init = this.shouldCache ? `${last} = ${value}` : value; block.builders.init.addLine( `document.title = ${init};` ); - updater = `document.title = ${shouldCache ? last : value};`; + updater = `document.title = ${this.shouldCache ? last : value};`; if (allDependencies.size) { const dependencies = Array.from(allDependencies); @@ -87,7 +87,7 @@ export default class Title extends Node { const updateCachedValue = `${last} !== (${last} = ${value})`; - const condition = shouldCache ? + const condition = this.shouldCache ? ( dependencies.length ? `(${changedCheck}) && ${updateCachedValue}` : updateCachedValue ) : changedCheck; diff --git a/src/generators/server-side-rendering/Block.ts b/src/generators/server-side-rendering/Block.ts index 112c812656..3743597a88 100644 --- a/src/generators/server-side-rendering/Block.ts +++ b/src/generators/server-side-rendering/Block.ts @@ -12,8 +12,6 @@ export default class Block { generator: SsrGenerator; conditions: string[]; - contexts: Map; - constructor(options: BlockOptions) { Object.assign(this, options); } diff --git a/src/generators/server-side-rendering/index.ts b/src/generators/server-side-rendering/index.ts index a5882df18f..08e13715a1 100644 --- a/src/generators/server-side-rendering/index.ts +++ b/src/generators/server-side-rendering/index.ts @@ -59,7 +59,6 @@ export default function ssr( // create main render() function const mainBlock = new Block({ generator, - contexts: new Map(), conditions: [], }); diff --git a/src/generators/server-side-rendering/visitors/AwaitBlock.ts b/src/generators/server-side-rendering/visitors/AwaitBlock.ts index 9e811ffcf1..8236f23f4f 100644 --- a/src/generators/server-side-rendering/visitors/AwaitBlock.ts +++ b/src/generators/server-side-rendering/visitors/AwaitBlock.ts @@ -10,14 +10,7 @@ export default function visitAwaitBlock( ) { const { snippet } = node.expression; - // TODO should this be the generator's job? It's duplicated between - // here and the equivalent DOM compiler visitor - const contexts = new Map(block.contexts); - contexts.set(node.value, '__value'); - - const childBlock = block.child({ - contexts - }); + const childBlock = block.child({}); generator.append('${(function(__value) { if(__isPromise(__value)) return `'); diff --git a/src/generators/server-side-rendering/visitors/EachBlock.ts b/src/generators/server-side-rendering/visitors/EachBlock.ts index 76b89d64a4..e859950c76 100644 --- a/src/generators/server-side-rendering/visitors/EachBlock.ts +++ b/src/generators/server-side-rendering/visitors/EachBlock.ts @@ -20,20 +20,7 @@ export default function visitEachBlock( const open = `\${ ${node.else ? `${snippet}.length ? ` : ''}__each(${snippet}, ${getContext}, ctx => \``; generator.append(open); - // TODO should this be the generator's job? It's duplicated between - // here and the equivalent DOM compiler visitor - const contexts = new Map(block.contexts); - contexts.set(node.context, node.context); - - if (node.destructuredContexts) { - for (let i = 0; i < node.destructuredContexts.length; i += 1) { - contexts.set(node.destructuredContexts[i], `${node.context}[${i}]`); - } - } - - const childBlock = block.child({ - contexts - }); + const childBlock = block.child({}); node.children.forEach((child: Node) => { visit(generator, childBlock, child);