Skip to content

Commit 3902291

Browse files
committed
get rid of text_effect
1 parent 3f68750 commit 3902291

File tree

4 files changed

+31
-45
lines changed

4 files changed

+31
-45
lines changed

packages/svelte/src/compiler/phases/3-transform/client/visitors/template.js

Lines changed: 29 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -1260,12 +1260,25 @@ function get_template_function(namespace, state) {
12601260
: '$.template';
12611261
}
12621262

1263+
/**
1264+
*
1265+
* @param {import('estree').Statement} statement
1266+
*/
1267+
function serialize_update(statement) {
1268+
const body =
1269+
statement.type === 'ExpressionStatement' ? statement.expression : b.block([statement]);
1270+
1271+
return b.stmt(b.call('$.render_effect', b.thunk(body)));
1272+
}
1273+
12631274
/**
12641275
*
12651276
* @param {import('../types.js').ComponentClientTransformState} state
12661277
*/
12671278
function serialize_render_stmt(state) {
1268-
return b.stmt(b.call('$.render_effect', b.thunk(b.block(state.update))));
1279+
return state.update.length === 1
1280+
? serialize_update(state.update[0])
1281+
: b.stmt(b.call('$.render_effect', b.thunk(b.block(state.update))));
12691282
}
12701283

12711284
/**
@@ -1490,26 +1503,18 @@ function process_children(nodes, expression, is_element, { visit, state }) {
14901503

14911504
const text_id = get_node_id(b.call('$.space', expression(true)), state, 'text');
14921505

1493-
const singular = b.stmt(
1506+
const update = b.stmt(
14941507
b.call(
1495-
'$.text_effect',
1508+
'$.text',
14961509
text_id,
1497-
b.thunk(/** @type {import('estree').Expression} */ (visit(node.expression)))
1510+
/** @type {import('estree').Expression} */ (visit(node.expression))
14981511
)
14991512
);
15001513

15011514
if (node.metadata.contains_call_expression && !within_bound_contenteditable) {
1502-
state.init.push(singular);
1515+
state.init.push(serialize_update(update));
15031516
} else if (node.metadata.dynamic && !within_bound_contenteditable) {
1504-
state.update.push(
1505-
b.stmt(
1506-
b.call(
1507-
'$.text',
1508-
text_id,
1509-
/** @type {import('estree').Expression} */ (visit(node.expression))
1510-
)
1511-
)
1512-
);
1517+
state.update.push(update);
15131518
} else {
15141519
state.init.push(
15151520
b.stmt(
@@ -1532,22 +1537,19 @@ function process_children(nodes, expression, is_element, { visit, state }) {
15321537

15331538
state.template.push(' ');
15341539

1535-
const contains_call_expression = sequence.some(
1536-
(n) => n.type === 'ExpressionTag' && n.metadata.contains_call_expression
1537-
);
1538-
const assignment = serialize_template_literal(sequence, visit, state)[1];
1539-
const init = b.stmt(b.assignment('=', b.member(text_id, b.id('nodeValue')), assignment));
1540-
const singular = b.stmt(b.call('$.text_effect', text_id, b.thunk(assignment)));
1540+
const [contains_call_expression, value] = serialize_template_literal(sequence, visit);
1541+
1542+
const update = b.stmt(b.call('$.text', text_id, value));
15411543

15421544
if (contains_call_expression && !within_bound_contenteditable) {
1543-
state.init.push(singular);
1545+
state.init.push(serialize_update(update));
15441546
} else if (
15451547
sequence.some((node) => node.type === 'ExpressionTag' && node.metadata.dynamic) &&
15461548
!within_bound_contenteditable
15471549
) {
1548-
state.update.push(b.stmt(b.call('$.text', text_id, assignment)));
1550+
state.update.push(update);
15491551
} else {
1550-
state.init.push(init);
1552+
state.init.push(b.stmt(b.assignment('=', b.member(text_id, b.id('nodeValue')), value)));
15511553
}
15521554

15531555
expression = (is_text) =>
@@ -1650,22 +1652,20 @@ function serialize_attribute_value(attribute_value, context) {
16501652
}
16511653
}
16521654

1653-
return serialize_template_literal(attribute_value, context.visit, context.state);
1655+
return serialize_template_literal(attribute_value, context.visit);
16541656
}
16551657

16561658
/**
16571659
* @param {Array<import('#compiler').Text | import('#compiler').ExpressionTag>} values
16581660
* @param {(node: import('#compiler').SvelteNode) => any} visit
1659-
* @param {import('../types.js').ComponentClientTransformState} state
16601661
* @returns {[boolean, import('estree').TemplateLiteral]}
16611662
*/
1662-
function serialize_template_literal(values, visit, state) {
1663+
function serialize_template_literal(values, visit) {
16631664
/** @type {import('estree').TemplateElement[]} */
16641665
const quasis = [];
16651666

16661667
/** @type {import('estree').Expression[]} */
16671668
const expressions = [];
1668-
const scope = state.scope;
16691669
let contains_call_expression = false;
16701670
quasis.push(b.quasi(''));
16711671

@@ -1689,6 +1689,7 @@ function serialize_template_literal(values, visit, state) {
16891689
}
16901690
}
16911691

1692+
// TODO instead of this tuple, return a `{ dynamic, complex, value }` object. will DRY stuff out
16921693
return [contains_call_expression, b.template(quasis, expressions)];
16931694
}
16941695

@@ -3111,7 +3112,7 @@ export const template_visitors = {
31113112
b.assignment(
31123113
'=',
31133114
b.member(b.id('$.document'), b.id('title')),
3114-
serialize_template_literal(/** @type {any} */ (node.fragment.nodes), visit, state)[1]
3115+
serialize_template_literal(/** @type {any} */ (node.fragment.nodes), visit)[1]
31153116
)
31163117
)
31173118
);

packages/svelte/src/internal/client/render.js

Lines changed: 0 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -39,15 +39,6 @@ export function set_should_intro(value) {
3939
should_intro = value;
4040
}
4141

42-
/**
43-
* @param {Element} dom
44-
* @param {() => string} value
45-
* @returns {void}
46-
*/
47-
export function text_effect(dom, value) {
48-
render_effect(() => text(dom, value()));
49-
}
50-
5142
/**
5243
* @param {Element} dom
5344
* @param {string} value

packages/svelte/tests/snapshot/samples/each-string-template/_expected/client/index.svelte.js

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -17,10 +17,7 @@ export default function Each_string_template($$anchor, $$props) {
1717
($$anchor, thing, $$index) => {
1818
var text = $.space_frag($$anchor);
1919

20-
$.render_effect(() => {
21-
$.text(text, `${$.stringify($.unwrap(thing))}, `);
22-
});
23-
20+
$.render_effect(() => $.text(text, `${$.stringify($.unwrap(thing))}, `));
2421
return $.close($$anchor, text);
2522
},
2623
null

packages/svelte/tests/snapshot/samples/function-prop-no-getter/_expected/client/index.svelte.js

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -23,10 +23,7 @@ export default function Function_prop_no_getter($$anchor, $$props) {
2323
children: ($$anchor, $$slotProps) => {
2424
var text = $.space_frag($$anchor);
2525

26-
$.render_effect(() => {
27-
$.text(text, `clicks: ${$.stringify($.get(count))}`);
28-
});
29-
26+
$.render_effect(() => $.text(text, `clicks: ${$.stringify($.get(count))}`));
3027
return $.close($$anchor, text);
3128
}
3229
});

0 commit comments

Comments
 (0)