further dry out get_slot_data

pull/4005/head
daszgfz 6 years ago
parent 6528baa35b
commit cea152fb59

@ -96,7 +96,7 @@ export default class SlotWrapper extends Wrapper {
renderer.blocks.push(b` renderer.blocks.push(b`
const ${get_slot_changes_fn} = #dirty => ${changes}; 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 { } else {
get_slot_changes_fn = 'null'; get_slot_changes_fn = 'null';

@ -1,37 +1,7 @@
import Renderer, { RenderOptions } from '../Renderer'; import Renderer, { RenderOptions } from '../Renderer';
import Slot from '../../nodes/Slot'; import Slot from '../../nodes/Slot';
import { x, p } from 'code-red'; import { x } from 'code-red';
import Attribute from '../../nodes/Attribute'; import get_slot_data from '../../utils/get_slot_data';
import { string_literal } from '../../utils/stringify';
// TODO this is *almost* but not quite duplicated with non-SSR
function get_slot_data(values: Map<string, Attribute>) {
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;
}
export default function(node: Slot, renderer: Renderer, options: RenderOptions) { export default function(node: Slot, renderer: Renderer, options: RenderOptions) {
const slot_data = get_slot_data(node.values); const slot_data = get_slot_data(node.values);

@ -3,25 +3,24 @@ import { p, x } from 'code-red';
import { string_literal } from './stringify'; import { string_literal } from './stringify';
import Block from '../render_dom/Block'; import Block from '../render_dom/Block';
export default function get_slot_data(block: Block, values: Map<string, Attribute>) { export default function get_slot_data(values: Map<string, Attribute>, block: Block = null) {
return { return {
type: 'ObjectExpression', type: 'ObjectExpression',
properties: Array.from(values.values()) properties: Array.from(values.values())
.filter(attribute => attribute.name !== 'name') .filter(attribute => attribute.name !== 'name')
.map(attribute => { .map(attribute => {
const value = get_value(block, attribute); const value = get_value(attribute, block);
return p`${attribute.name}: ${value}`; return p`${attribute.name}: ${value}`;
}) })
}; };
} }
// TODO fairly sure this is duplicated at least once function get_value(attribute: Attribute, block: Block = null) {
function get_value(block: Block, attribute: Attribute) {
if (attribute.is_true) return x`true`; if (attribute.is_true) return x`true`;
if (attribute.chunks.length === 0) return x`""`; if (attribute.chunks.length === 0) return x`""`;
let value = attribute.chunks 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}`); .reduce((lhs, rhs) => x`${lhs} + ${rhs}`);
if (attribute.chunks.length > 1 && attribute.chunks[0].type !== 'Text') { if (attribute.chunks.length > 1 && attribute.chunks[0].type !== 'Text') {

Loading…
Cancel
Save