Convert src/compiler/compile/utils/get_slot_data.ts to JavaScript

pull/8569/head
Simon Holthausen 3 years ago
parent 186b8ebf20
commit b43c9c3690

@ -1,49 +1,59 @@
import Attribute from '../nodes/Attribute';
import { p, x } from 'code-red'; import { p, x } from 'code-red';
import { string_literal } from './stringify'; import { string_literal } from './stringify.js';
import Block from '../render_dom/Block';
export default function get_slot_data(values: Map<string, Attribute>, block: Block = null) { /**
return { * @param {Map<string, Attribute>} values
type: 'ObjectExpression', * @param {Block} block
properties: Array.from(values.values()) */
.filter((attribute) => attribute.name !== 'name') export default function get_slot_data(values, block = null) {
.map((attribute) => { return {
if (attribute.is_spread) { type: 'ObjectExpression',
const argument = get_spread_value(block, attribute); properties: Array.from(values.values())
return { .filter((attribute) => attribute.name !== 'name')
type: 'SpreadElement', .map((attribute) => {
argument if (attribute.is_spread) {
}; const argument = get_spread_value(block, attribute);
} return {
type: 'SpreadElement',
argument
};
}
const value = get_value(block, attribute);
return p `${attribute.name}: ${value}`;
})
};
}
const value = get_value(block, attribute); /**
return p`${attribute.name}: ${value}`; * @param {Block} block
}) * @param {Attribute} attribute
}; */
function get_value(block, 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)
: block
? chunk.manipulate(block)
: chunk.node)
.reduce((lhs, rhs) => x `${lhs} + ${rhs}`);
if (attribute.chunks.length > 1 && attribute.chunks[0].type !== 'Text') {
value = x `"" + ${value}`;
}
return value;
} }
function get_value(block: Block, attribute: Attribute) { /**
if (attribute.is_true) return x`true`; * @param {Block} block
if (attribute.chunks.length === 0) return x`""`; * @param {Attribute} attribute
*/
function get_spread_value(block, attribute) {
return block ? attribute.expression.manipulate(block) : attribute.expression.node;
}
let value = attribute.chunks
.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') {
value = x`"" + ${value}`;
}
return value;
}
function get_spread_value(block: Block, attribute: Attribute) {
return block ? attribute.expression.manipulate(block) : attribute.expression.node;
}

Loading…
Cancel
Save