From cea152fb59a8aa53251dd1170a1e97f469decd72 Mon Sep 17 00:00:00 2001 From: daszgfz Date: Tue, 26 Nov 2019 23:30:13 -0500 Subject: [PATCH] further dry out get_slot_data --- .../compile/render_dom/wrappers/Slot.ts | 2 +- .../compile/render_ssr/handlers/Slot.ts | 34 ++----------------- src/compiler/compile/utils/get_slot_data.ts | 9 +++-- 3 files changed, 7 insertions(+), 38 deletions(-) diff --git a/src/compiler/compile/render_dom/wrappers/Slot.ts b/src/compiler/compile/render_dom/wrappers/Slot.ts index b72d3f0d49..5f2d331839 100644 --- a/src/compiler/compile/render_dom/wrappers/Slot.ts +++ b/src/compiler/compile/render_dom/wrappers/Slot.ts @@ -96,7 +96,7 @@ export default class SlotWrapper extends Wrapper { renderer.blocks.push(b` const ${get_slot_changes_fn} = #dirty => ${changes}; - const ${get_slot_context_fn} = #ctx => ${get_slot_data(block, this.node.values)}; + const ${get_slot_context_fn} = #ctx => ${get_slot_data(this.node.values, block)}; `); } else { get_slot_changes_fn = 'null'; diff --git a/src/compiler/compile/render_ssr/handlers/Slot.ts b/src/compiler/compile/render_ssr/handlers/Slot.ts index 897efeb382..3b1e199c75 100644 --- a/src/compiler/compile/render_ssr/handlers/Slot.ts +++ b/src/compiler/compile/render_ssr/handlers/Slot.ts @@ -1,37 +1,7 @@ import Renderer, { RenderOptions } from '../Renderer'; import Slot from '../../nodes/Slot'; -import { x, p } from 'code-red'; -import Attribute from '../../nodes/Attribute'; -import { string_literal } from '../../utils/stringify'; - -// TODO this is *almost* but not quite duplicated with non-SSR -function get_slot_data(values: Map) { - return { - type: 'ObjectExpression', - properties: Array.from(values.values()) - .filter(attribute => attribute.name !== 'name') - .map(attribute => { - const value = get_value(attribute); - return p`${attribute.name}: ${value}`; - }) - }; -} - -// TODO fairly sure this is duplicated at least once -function get_value(attribute: Attribute) { - if (attribute.is_true) return x`true`; - if (attribute.chunks.length === 0) return x`""`; - - let value = attribute.chunks - .map(chunk => chunk.type === 'Text' ? string_literal(chunk.data) : chunk.node) - .reduce((lhs, rhs) => x`${lhs} + ${rhs}`); - - if (attribute.chunks.length > 1 && attribute.chunks[0].type !== 'Text') { - value = x`"" + ${value}`; - } - - return value; -} +import { x } from 'code-red'; +import get_slot_data from '../../utils/get_slot_data'; export default function(node: Slot, renderer: Renderer, options: RenderOptions) { const slot_data = get_slot_data(node.values); diff --git a/src/compiler/compile/utils/get_slot_data.ts b/src/compiler/compile/utils/get_slot_data.ts index 87811684a3..ae9f40a65f 100644 --- a/src/compiler/compile/utils/get_slot_data.ts +++ b/src/compiler/compile/utils/get_slot_data.ts @@ -3,25 +3,24 @@ import { p, x } from 'code-red'; import { string_literal } from './stringify'; import Block from '../render_dom/Block'; -export default function get_slot_data(block: Block, values: Map) { +export default function get_slot_data(values: Map, block: Block = null) { return { type: 'ObjectExpression', properties: Array.from(values.values()) .filter(attribute => attribute.name !== 'name') .map(attribute => { - const value = get_value(block, attribute); + const value = get_value(attribute, block); return p`${attribute.name}: ${value}`; }) }; } -// TODO fairly sure this is duplicated at least once -function get_value(block: Block, attribute: Attribute) { +function get_value(attribute: Attribute, block: Block = null) { if (attribute.is_true) return x`true`; if (attribute.chunks.length === 0) return x`""`; let value = attribute.chunks - .map(chunk => chunk.type === 'Text' ? string_literal(chunk.data) : chunk.manipulate(block)) + .map(chunk => chunk.type === 'Text' ? string_literal(chunk.data) : (block ? chunk.manipulate(block) : chunk.node)) .reduce((lhs, rhs) => x`${lhs} + ${rhs}`); if (attribute.chunks.length > 1 && attribute.chunks[0].type !== 'Text') {