DRY out get_slot_data

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

@ -6,39 +6,10 @@ import FragmentWrapper from './Fragment';
import { b, p, x } from 'code-red'; import { b, p, x } from 'code-red';
import { sanitize } from '../../../utils/names'; import { sanitize } from '../../../utils/names';
import add_to_set from '../../utils/add_to_set'; 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 Expression from '../../nodes/shared/Expression';
import is_dynamic from './shared/is_dynamic'; import is_dynamic from './shared/is_dynamic';
import { Identifier, ObjectExpression } from 'estree'; import { Identifier, ObjectExpression } from 'estree';
import Attribute from '../../nodes/Attribute';
import { string_literal } from '../../utils/stringify';
function get_slot_data(block: Block, values: Map<string, Attribute>) {
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 { export default class SlotWrapper extends Wrapper {
node: Slot; node: Slot;

Loading…
Cancel
Save