2
2
/** @import { AST } from '#compiler' */
3
3
/** @import { ComponentContext } from '../types' */
4
4
import * as b from '#compiler/builders' ;
5
- import { block_close , block_open , block_open_else , build_attribute_value } from './shared/utils.js' ;
5
+ import {
6
+ block_close ,
7
+ block_open ,
8
+ block_open_else ,
9
+ build_attribute_value ,
10
+ build_template
11
+ } from './shared/utils.js' ;
6
12
7
13
/**
8
14
* @param {AST.SvelteBoundary } node
@@ -13,6 +19,11 @@ export function SvelteBoundary(node, context) {
13
19
const pending_attribute = /** @type {AST.Attribute } */ (
14
20
node . attributes . find ( ( node ) => node . type === 'Attribute' && node . name === 'pending' )
15
21
) ;
22
+ const is_pending_attr_nullish =
23
+ pending_attribute &&
24
+ typeof pending_attribute . value === 'object' &&
25
+ ! Array . isArray ( pending_attribute . value ) &&
26
+ ! context . state . scope . evaluate ( pending_attribute . value . expression ) . is_defined ;
16
27
17
28
const pending_snippet = /** @type {AST.SnippetBlock } */ (
18
29
node . fragment . nodes . find (
@@ -21,20 +32,38 @@ export function SvelteBoundary(node, context) {
21
32
) ;
22
33
23
34
if ( pending_attribute || pending_snippet ) {
24
- const pending = pending_attribute
25
- ? b . call (
26
- build_attribute_value (
27
- pending_attribute . value ,
28
- context ,
29
- ( expression ) => expression ,
30
- false ,
31
- true
32
- ) ,
33
- b . id ( '$$renderer' )
35
+ if ( pending_attribute && is_pending_attr_nullish && ! pending_snippet ) {
36
+ const callee = build_attribute_value (
37
+ pending_attribute . value ,
38
+ context ,
39
+ ( expression ) => expression ,
40
+ false ,
41
+ true
42
+ ) ;
43
+ const pending = b . call ( callee , b . id ( '$$renderer' ) ) ;
44
+ const block = /** @type {BlockStatement } */ ( context . visit ( node . fragment ) ) ;
45
+ context . state . template . push (
46
+ b . if (
47
+ callee ,
48
+ b . block ( build_template ( [ block_open_else , pending , block_close ] ) ) ,
49
+ b . block ( build_template ( [ block_open , block , block_close ] ) )
34
50
)
35
- : /** @type {BlockStatement } */ ( context . visit ( pending_snippet . body ) ) ;
36
-
37
- context . state . template . push ( block_open_else , pending , block_close ) ;
51
+ ) ;
52
+ } else {
53
+ const pending = pending_attribute
54
+ ? b . call (
55
+ build_attribute_value (
56
+ pending_attribute . value ,
57
+ context ,
58
+ ( expression ) => expression ,
59
+ false ,
60
+ true
61
+ ) ,
62
+ b . id ( '$$renderer' )
63
+ )
64
+ : /** @type {BlockStatement } */ ( context . visit ( pending_snippet . body ) ) ;
65
+ context . state . template . push ( block_open_else , pending , block_close ) ;
66
+ }
38
67
} else {
39
68
const block = /** @type {BlockStatement } */ ( context . visit ( node . fragment ) ) ;
40
69
context . state . template . push ( block_open , block , block_close ) ;
0 commit comments