@@ -1260,12 +1260,25 @@ function get_template_function(namespace, state) {
1260
1260
: '$.template' ;
1261
1261
}
1262
1262
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
+
1263
1274
/**
1264
1275
*
1265
1276
* @param {import('../types.js').ComponentClientTransformState } state
1266
1277
*/
1267
1278
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 ) ) ) ) ;
1269
1282
}
1270
1283
1271
1284
/**
@@ -1490,26 +1503,18 @@ function process_children(nodes, expression, is_element, { visit, state }) {
1490
1503
1491
1504
const text_id = get_node_id ( b . call ( '$.space' , expression ( true ) ) , state , 'text' ) ;
1492
1505
1493
- const singular = b . stmt (
1506
+ const update = b . stmt (
1494
1507
b . call (
1495
- '$.text_effect ' ,
1508
+ '$.text ' ,
1496
1509
text_id ,
1497
- b . thunk ( /** @type {import('estree').Expression } */ ( visit ( node . expression ) ) )
1510
+ /** @type {import('estree').Expression } */ ( visit ( node . expression ) )
1498
1511
)
1499
1512
) ;
1500
1513
1501
1514
if ( node . metadata . contains_call_expression && ! within_bound_contenteditable ) {
1502
- state . init . push ( singular ) ;
1515
+ state . init . push ( serialize_update ( update ) ) ;
1503
1516
} 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 ) ;
1513
1518
} else {
1514
1519
state . init . push (
1515
1520
b . stmt (
@@ -1532,22 +1537,19 @@ function process_children(nodes, expression, is_element, { visit, state }) {
1532
1537
1533
1538
state . template . push ( ' ' ) ;
1534
1539
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 ) ) ;
1541
1543
1542
1544
if ( contains_call_expression && ! within_bound_contenteditable ) {
1543
- state . init . push ( singular ) ;
1545
+ state . init . push ( serialize_update ( update ) ) ;
1544
1546
} else if (
1545
1547
sequence . some ( ( node ) => node . type === 'ExpressionTag' && node . metadata . dynamic ) &&
1546
1548
! within_bound_contenteditable
1547
1549
) {
1548
- state . update . push ( b . stmt ( b . call ( '$.text' , text_id , assignment ) ) ) ;
1550
+ state . update . push ( update ) ;
1549
1551
} else {
1550
- state . init . push ( init ) ;
1552
+ state . init . push ( b . stmt ( b . assignment ( '=' , b . member ( text_id , b . id ( 'nodeValue' ) ) , value ) ) ) ;
1551
1553
}
1552
1554
1553
1555
expression = ( is_text ) =>
@@ -1650,22 +1652,20 @@ function serialize_attribute_value(attribute_value, context) {
1650
1652
}
1651
1653
}
1652
1654
1653
- return serialize_template_literal ( attribute_value , context . visit , context . state ) ;
1655
+ return serialize_template_literal ( attribute_value , context . visit ) ;
1654
1656
}
1655
1657
1656
1658
/**
1657
1659
* @param {Array<import('#compiler').Text | import('#compiler').ExpressionTag> } values
1658
1660
* @param {(node: import('#compiler').SvelteNode) => any } visit
1659
- * @param {import('../types.js').ComponentClientTransformState } state
1660
1661
* @returns {[boolean, import('estree').TemplateLiteral] }
1661
1662
*/
1662
- function serialize_template_literal ( values , visit , state ) {
1663
+ function serialize_template_literal ( values , visit ) {
1663
1664
/** @type {import('estree').TemplateElement[] } */
1664
1665
const quasis = [ ] ;
1665
1666
1666
1667
/** @type {import('estree').Expression[] } */
1667
1668
const expressions = [ ] ;
1668
- const scope = state . scope ;
1669
1669
let contains_call_expression = false ;
1670
1670
quasis . push ( b . quasi ( '' ) ) ;
1671
1671
@@ -1689,6 +1689,7 @@ function serialize_template_literal(values, visit, state) {
1689
1689
}
1690
1690
}
1691
1691
1692
+ // TODO instead of this tuple, return a `{ dynamic, complex, value }` object. will DRY stuff out
1692
1693
return [ contains_call_expression , b . template ( quasis , expressions ) ] ;
1693
1694
}
1694
1695
@@ -3111,7 +3112,7 @@ export const template_visitors = {
3111
3112
b . assignment (
3112
3113
'=' ,
3113
3114
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 ]
3115
3116
)
3116
3117
)
3117
3118
) ;
0 commit comments