Lint and format src/compiler/compile/render_ssr/handlers/InlineComponent.js

pull/8569/head
Simon Holthausen 3 years ago
parent 9f271e2c50
commit 0b7ab0672f

@ -5,14 +5,11 @@ import { namespaces } from '../../../utils/namespaces';
/** @param {any} attribute */ /** @param {any} attribute */
function get_prop_value(attribute) { function get_prop_value(attribute) {
if (attribute.is_true) if (attribute.is_true) return x`true`;
return x `true`; if (attribute.chunks.length === 0) return x`''`;
if (attribute.chunks.length === 0)
return x `''`;
return attribute.chunks return attribute.chunks
.map((chunk) => { .map((chunk) => {
if (chunk.type === 'Text') if (chunk.type === 'Text') return string_literal(chunk.data);
return string_literal(chunk.data);
return chunk.node; return chunk.node;
}) })
.reduce((lhs, rhs) => x`${lhs} + ${rhs}`); .reduce((lhs, rhs) => x`${lhs} + ${rhs}`);
@ -40,14 +37,12 @@ export default function (node, renderer, options) {
.map((attribute) => { .map((attribute) => {
if (attribute.is_spread) { if (attribute.is_spread) {
return attribute.expression.node; return attribute.expression.node;
} } else {
else {
return x`{ ${attribute.name}: ${get_prop_value(attribute)} }`; return x`{ ${attribute.name}: ${get_prop_value(attribute)} }`;
} }
}) })
.concat(binding_props.map((p) => x`{ ${p} }`))})`; .concat(binding_props.map((p) => x`{ ${p} }`))})`;
} } else {
else {
props = x`{ props = x`{
${node.attributes.map((attribute) => p`${attribute.name}: ${get_prop_value(attribute)}`)}, ${node.attributes.map((attribute) => p`${attribute.name}: ${get_prop_value(attribute)}`)},
${binding_props} ${binding_props}
@ -56,18 +51,22 @@ export default function (node, renderer, options) {
const bindings = x`{ const bindings = x`{
${binding_fns} ${binding_fns}
}`; }`;
const expression = node.name === 'svelte:self' const expression =
node.name === 'svelte:self'
? renderer.name ? renderer.name
: node.name === 'svelte:component' : node.name === 'svelte:component'
? x`(${node.expression.node}) || @missing_component` ? x`(${node.expression.node}) || @missing_component`
: node.name.split('.').reduce(/** @type {any} */ (((lhs, rhs) => x `${lhs}.${rhs}`))); : node.name.split('.').reduce(/** @type {any} */ ((lhs, rhs) => x`${lhs}.${rhs}`));
const slot_fns = []; const slot_fns = [];
const children = node.children; const children = node.children;
if (children.length) { if (children.length) {
const slot_scopes = new Map(); const slot_scopes = new Map();
renderer.render(children, Object.assign({}, options, { renderer.render(
children,
Object.assign({}, options, {
slot_scopes slot_scopes
})); })
);
slot_scopes.forEach(({ input, output, statements }, name) => { slot_scopes.forEach(({ input, output, statements }, name) => {
slot_fns.push(p`${name}: (${input}) => { ${statements}; return ${output}; }`); slot_fns.push(p`${name}: (${input}) => { ${statements}; return ${output}; }`);
}); });
@ -78,30 +77,25 @@ export default function (node, renderer, options) {
if (node.css_custom_properties.length > 0) { if (node.css_custom_properties.length > 0) {
if (node.namespace === namespaces.svg) { if (node.namespace === namespaces.svg) {
renderer.add_string('<g style="'); renderer.add_string('<g style="');
} } else {
else {
renderer.add_string('<div style="display: contents; '); renderer.add_string('<div style="display: contents; ');
} }
node.css_custom_properties.forEach((attr, index) => { node.css_custom_properties.forEach((attr, index) => {
renderer.add_string(`${attr.name}:`); renderer.add_string(`${attr.name}:`);
renderer.add_expression(get_attribute_value(attr)); renderer.add_expression(get_attribute_value(attr));
renderer.add_string(';'); renderer.add_string(';');
if (index < node.css_custom_properties.length - 1) if (index < node.css_custom_properties.length - 1) renderer.add_string(' ');
renderer.add_string(' ');
}); });
renderer.add_string('">'); renderer.add_string('">');
} }
renderer.add_expression(x `@validate_component(${expression}, "${node.name}").$$render($$result, ${props}, ${bindings}, ${slots})`); renderer.add_expression(
x`@validate_component(${expression}, "${node.name}").$$render($$result, ${props}, ${bindings}, ${slots})`
);
if (node.css_custom_properties.length > 0) { if (node.css_custom_properties.length > 0) {
if (node.namespace === namespaces.svg) { if (node.namespace === namespaces.svg) {
renderer.add_string('</g>'); renderer.add_string('</g>');
} } else {
else {
renderer.add_string('</div>'); renderer.add_string('</div>');
} }
} }
} }

Loading…
Cancel
Save