@@ -14,39 +14,41 @@ import { locator } from '../../../../../state.js';
14
14
* @param {Array<Text | ExpressionTag> } values
15
15
* @param {(node: SvelteNode, state: any) => any } visit
16
16
* @param {ComponentClientTransformState } state
17
- * @returns {[boolean, TemplateLiteral] }
18
17
*/
19
18
export function build_template_literal ( values , visit , state ) {
20
- /** @type {TemplateElement[] } */
21
- const quasis = [ ] ;
22
-
23
19
/** @type {Expression[] } */
24
20
const expressions = [ ] ;
21
+
22
+ let quasi = b . quasi ( '' ) ;
23
+ const quasis = [ quasi ] ;
24
+
25
25
let has_call = false ;
26
+ let has_state = false ;
26
27
let contains_multiple_call_expression = false ;
27
- quasis . push ( b . quasi ( '' ) ) ;
28
28
29
29
for ( let i = 0 ; i < values . length ; i ++ ) {
30
30
const node = values [ i ] ;
31
31
32
- if ( node . type === 'ExpressionTag' && node . metadata . expression . has_call ) {
33
- if ( has_call ) {
34
- contains_multiple_call_expression = true ;
32
+ if ( node . type === 'ExpressionTag' ) {
33
+ if ( node . metadata . expression . has_call ) {
34
+ if ( has_call ) {
35
+ contains_multiple_call_expression = true ;
36
+ }
37
+ has_call = true ;
35
38
}
36
- has_call = true ;
39
+
40
+ has_state ||= node . metadata . expression . has_state ;
37
41
}
38
42
}
39
43
40
44
for ( let i = 0 ; i < values . length ; i ++ ) {
41
45
const node = values [ i ] ;
42
46
43
47
if ( node . type === 'Text' ) {
44
- const last = /** @type {TemplateElement } */ ( quasis . at ( - 1 ) ) ;
45
- last . value . raw += sanitize_template_string ( node . data ) ;
48
+ quasi . value . raw += sanitize_template_string ( node . data ) ;
46
49
} else if ( node . type === 'ExpressionTag' && node . expression . type === 'Literal' ) {
47
- const last = /** @type {TemplateElement } */ ( quasis . at ( - 1 ) ) ;
48
50
if ( node . expression . value != null ) {
49
- last . value . raw += sanitize_template_string ( node . expression . value + '' ) ;
51
+ quasi . value . raw += sanitize_template_string ( node . expression . value + '' ) ;
50
52
}
51
53
} else {
52
54
if ( contains_multiple_call_expression ) {
@@ -65,12 +67,15 @@ export function build_template_literal(values, visit, state) {
65
67
} else {
66
68
expressions . push ( b . logical ( '??' , visit ( node . expression , state ) , b . literal ( '' ) ) ) ;
67
69
}
68
- quasis . push ( b . quasi ( '' , i + 1 === values . length ) ) ;
70
+
71
+ quasi = b . quasi ( '' , i + 1 === values . length ) ;
72
+ quasis . push ( quasi ) ;
69
73
}
70
74
}
71
75
72
- // TODO instead of this tuple, return a `{ dynamic, complex, value }` object. will DRY stuff out
73
- return [ has_call , b . template ( quasis , expressions ) ] ;
76
+ const value = b . template ( quasis , expressions ) ;
77
+
78
+ return { value, has_state, has_call } ;
74
79
}
75
80
76
81
/**
0 commit comments