From 6528baa35bf5840b9fe3ebfdbb67ec5ac4b76a28 Mon Sep 17 00:00:00 2001 From: daszgfz Date: Tue, 26 Nov 2019 23:11:22 -0500 Subject: [PATCH] DRY out get_slot_data --- .../compile/render_dom/wrappers/Slot.ts | 31 +------------------ 1 file changed, 1 insertion(+), 30 deletions(-) diff --git a/src/compiler/compile/render_dom/wrappers/Slot.ts b/src/compiler/compile/render_dom/wrappers/Slot.ts index 0443ecc2e5..b72d3f0d49 100644 --- a/src/compiler/compile/render_dom/wrappers/Slot.ts +++ b/src/compiler/compile/render_dom/wrappers/Slot.ts @@ -6,39 +6,10 @@ import FragmentWrapper from './Fragment'; import { b, p, x } from 'code-red'; import { sanitize } from '../../../utils/names'; import add_to_set from '../../utils/add_to_set'; +import get_slot_data from '../../utils/get_slot_data'; import Expression from '../../nodes/shared/Expression'; import is_dynamic from './shared/is_dynamic'; import { Identifier, ObjectExpression } from 'estree'; -import Attribute from '../../nodes/Attribute'; -import { string_literal } from '../../utils/stringify'; - -function get_slot_data(block: Block, values: Map) { - return { - type: 'ObjectExpression', - properties: Array.from(values.values()) - .filter(attribute => attribute.name !== 'name') - .map(attribute => { - const value = get_value(block, attribute); - return p`${attribute.name}: ${value}`; - }) - }; -} - -// TODO fairly sure this is duplicated at least once -function get_value(block: Block, 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.manipulate(block)) - .reduce((lhs, rhs) => x`${lhs} + ${rhs}`); - - if (attribute.chunks.length > 1 && attribute.chunks[0].type !== 'Text') { - value = x`"" + ${value}`; - } - - return value; -} export default class SlotWrapper extends Wrapper { node: Slot;