From 11fdb086f960a3d3a64a2679936f7011ad351b96 Mon Sep 17 00:00:00 2001 From: Rich Harris Date: Sat, 28 Apr 2018 16:35:27 -0400 Subject: [PATCH] more tidying up --- src/generators/nodes/Attribute.ts | 24 +++++----- src/generators/nodes/Binding.ts | 9 +--- src/generators/nodes/Component.ts | 55 ----------------------- src/generators/nodes/Element.ts | 12 +++-- src/generators/nodes/shared/Expression.ts | 3 -- 5 files changed, 17 insertions(+), 86 deletions(-) diff --git a/src/generators/nodes/Attribute.ts b/src/generators/nodes/Attribute.ts index 354323c53e..5ddbaa4f85 100644 --- a/src/generators/nodes/Attribute.ts +++ b/src/generators/nodes/Attribute.ts @@ -26,6 +26,7 @@ export default class Attribute extends Node { isTrue: boolean; isDynamic: boolean; isSynthetic: boolean; + shouldCache: boolean; expression?: Expression; chunks: (Text | Expression)[]; dependencies: Set; @@ -44,6 +45,7 @@ export default class Attribute extends Node { this.chunks = null; this.isDynamic = true; // TODO not necessarily + this.shouldCache = false; // TODO does this mean anything here? } else { @@ -69,6 +71,12 @@ export default class Attribute extends Node { this.isDynamic = this.chunks.length === 1 ? this.chunks[0].type !== 'Text' : this.chunks.length > 1; + + this.shouldCache = this.isDynamic + ? this.chunks.length === 1 + ? this.chunks[0].node.type !== 'Identifier' || scope.names.has(this.chunks[0].node.name) + : true + : false; } } @@ -140,21 +148,11 @@ export default class Attribute extends Node { if (this.isDynamic) { let value; - 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 if (this.chunks.length === 1) { // single {tag} — may be a non-string - const expression = this.chunks[0]; - const { snippet } = expression; - - value = snippet; - - shouldCache = ( - expression.node.type !== 'Identifier' || - block.contexts.has(expression.node.name) - ); + value = this.chunks[0].snippet; } else { // '{foo} {bar}' — treat as string concatenation value = @@ -170,14 +168,12 @@ export default class Attribute extends Node { } }) .join(' + '); - - shouldCache = true; } const isSelectValueAttribute = name === 'value' && node.name === 'select'; - if (isSelectValueAttribute) shouldCache = true; + const shouldCache = this.shouldCache || isSelectValueAttribute; const last = shouldCache && block.getUniqueName( `${node.var}_${name.replace(/[^a-zA-Z_$]/g, '_')}_value` diff --git a/src/generators/nodes/Binding.ts b/src/generators/nodes/Binding.ts index dcd763120c..8b74a14f3b 100644 --- a/src/generators/nodes/Binding.ts +++ b/src/generators/nodes/Binding.ts @@ -49,8 +49,7 @@ export default class Binding extends Node { } munge( - block: Block, - allUsedContexts: Set + block: Block ) { const node: Element = this.parent; @@ -60,7 +59,7 @@ export default class Binding extends Node { let updateCondition: string; const { name } = getObject(this.value.node); - const { contexts, snippet } = this.value; + const { snippet } = this.value; // special case: if you have e.g. `` // and `selected` is an object chosen with a