@@ -11,28 +11,6 @@ import is_reference from 'is-reference';
11
11
import { locator } from '../../../../../state.js' ;
12
12
import { escape_html } from '../../../../../../escaping.js' ;
13
13
14
- /**
15
- * @param {Array<AST.Text | AST.ExpressionTag> } values
16
- */
17
- export function get_states_and_calls ( values ) {
18
- let states = 0 ;
19
- let calls = 0 ;
20
- for ( let i = 0 ; i < values . length ; i ++ ) {
21
- const node = values [ i ] ;
22
-
23
- if ( node . type === 'ExpressionTag' ) {
24
- if ( node . metadata . expression . has_call ) {
25
- calls ++ ;
26
- }
27
- if ( node . metadata . expression . has_state ) {
28
- states ++ ;
29
- }
30
- }
31
- }
32
-
33
- return { states, calls } ;
34
- }
35
-
36
14
/**
37
15
* @param {Expression } node
38
16
* @param {boolean } [is_attr]
@@ -70,55 +48,59 @@ export function build_template_chunk(values, visit, state) {
70
48
let quasi = b . quasi ( '' ) ;
71
49
const quasis = [ quasi ] ;
72
50
73
- const { states, calls } = get_states_and_calls ( values ) ;
74
-
75
- let has_call = calls > 0 ;
76
- let has_state = states > 0 ;
77
- let contains_multiple_call_expression = calls > 1 ;
51
+ let has_call = false ;
52
+ let has_state = false ;
78
53
let can_inline = true ;
54
+ let contains_multiple_call_expression = false ;
79
55
80
- for ( let i = 0 ; i < values . length ; i ++ ) {
81
- const node = values [ i ] ;
56
+ for ( const node of values ) {
57
+ if ( node . type === 'ExpressionTag' ) {
58
+ if ( node . metadata . expression . has_call ) {
59
+ if ( has_call ) contains_multiple_call_expression = true ;
60
+ has_call = true ;
61
+ }
82
62
83
- if ( node . type === 'Text' ) {
84
- quasi . value . cooked += node . data ;
85
- } else if ( node . type === 'ExpressionTag' && node . expression . type === 'Literal' ) {
86
- if ( node . expression . value != null ) {
87
- quasi . value . cooked += node . expression . value + '' ;
63
+ if ( node . metadata . expression . has_state ) {
64
+ has_state = true ;
88
65
}
89
- } else {
66
+
90
67
if ( ! node . metadata . expression . can_inline ) {
91
68
can_inline = false ;
92
69
}
70
+ }
71
+ }
93
72
94
- if ( contains_multiple_call_expression ) {
95
- const id = b . id ( state . scope . generate ( 'stringified_text' ) ) ;
96
- state . init . push (
97
- b . const (
98
- id ,
99
- create_derived (
100
- state ,
101
- b . thunk (
102
- b . logical (
103
- '??' ,
104
- /** @type {Expression } */ ( visit ( node . expression , state ) ) ,
105
- b . literal ( '' )
106
- )
107
- )
108
- )
109
- )
110
- ) ;
111
- expressions . push ( b . call ( '$.get' , id ) ) ;
112
- } else if ( values . length === 1 ) {
113
- // If we have a single expression, then pass that in directly to possibly avoid doing
114
- // extra work in the template_effect (instead we do the work in set_text).
115
- return { value : visit ( node . expression , state ) , has_state, has_call, can_inline } ;
73
+ for ( let i = 0 ; i < values . length ; i ++ ) {
74
+ const node = values [ i ] ;
75
+
76
+ if ( node . type === 'Text' ) {
77
+ quasi . value . cooked += node . data ;
78
+ } else {
79
+ if ( node . expression . type === 'Literal' ) {
80
+ if ( node . expression . value != null ) {
81
+ quasi . value . cooked += node . expression . value + '' ;
82
+ }
116
83
} else {
117
- expressions . push ( b . logical ( '??' , visit ( node . expression , state ) , b . literal ( '' ) ) ) ;
118
- }
84
+ if ( contains_multiple_call_expression ) {
85
+ const id = b . id ( state . scope . generate ( 'stringified_text' ) ) ;
86
+ const expression = /** @type {Expression } */ ( visit ( node . expression , state ) ) ;
87
+
88
+ state . init . push (
89
+ b . const ( id , create_derived ( state , b . thunk ( b . logical ( '??' , expression , b . literal ( '' ) ) ) ) )
90
+ ) ;
91
+
92
+ expressions . push ( b . call ( '$.get' , id ) ) ;
93
+ } else if ( values . length === 1 ) {
94
+ // If we have a single expression, then pass that in directly to possibly avoid doing
95
+ // extra work in the template_effect (instead we do the work in set_text).
96
+ return { value : visit ( node . expression , state ) , has_state, has_call, can_inline } ;
97
+ } else {
98
+ expressions . push ( b . logical ( '??' , visit ( node . expression , state ) , b . literal ( '' ) ) ) ;
99
+ }
119
100
120
- quasi = b . quasi ( '' , i + 1 === values . length ) ;
121
- quasis . push ( quasi ) ;
101
+ quasi = b . quasi ( '' , i + 1 === values . length ) ;
102
+ quasis . push ( quasi ) ;
103
+ }
122
104
}
123
105
}
124
106
0 commit comments