Skip to content

Commit a7ee520

Browse files
committed
simplify
1 parent 1ec18a3 commit a7ee520

File tree

1 file changed

+17
-31
lines changed

1 file changed

+17
-31
lines changed

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

Lines changed: 17 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -1,46 +1,40 @@
1-
/** @import { BlockStatement, Statement, Property, Expression } from 'estree' */
1+
/** @import { BlockStatement, Statement, Expression } from 'estree' */
22
/** @import { AST } from '#compiler' */
33
/** @import { ComponentContext } from '../types' */
4-
54
import * as b from '../../../../utils/builders.js';
5+
66
/**
77
* @param {AST.SvelteBoundary} node
88
* @param {ComponentContext} context
99
*/
1010
export function SvelteBoundary(node, context) {
11-
const nodes = [];
12-
1311
const props = b.object([]);
1412

1513
/** @type {Statement[]} */
1614
const snippet_statements = [];
1715

1816
for (const attribute of node.attributes) {
19-
// Skip non-attributes with a single value
20-
if (
21-
attribute.type !== 'Attribute' ||
22-
attribute.value === true ||
23-
Array.isArray(attribute.value)
24-
) {
17+
if (attribute.type !== 'Attribute' || attribute.value === true) {
18+
// these can't exist, because they would have caused validation
19+
// to fail, but typescript doesn't know that
2520
continue;
2621
}
2722

28-
// Currently we only support `onerror` and `failed` props
29-
if (attribute.name === 'onerror' || attribute.name === 'failed') {
30-
const value = /** @type {Expression} */ (
31-
context.visit(attribute.value.expression, context.state)
32-
);
23+
const chunk = Array.isArray(attribute.value)
24+
? /** @type {AST.ExpressionTag} */ (attribute.value[0])
25+
: attribute.value;
26+
27+
const expression = /** @type {Expression} */ (context.visit(chunk.expression, context.state));
3328

34-
if (attribute.metadata.expression.has_state) {
35-
props.properties.push(
36-
b.prop('get', b.id(attribute.name), b.function(null, [], b.block([b.return(value)])))
37-
);
38-
} else {
39-
props.properties.push(b.prop('init', b.id(attribute.name), value));
40-
}
29+
if (attribute.metadata.expression.has_state) {
30+
props.properties.push(b.get(attribute.name, [b.return(expression)]));
31+
} else {
32+
props.properties.push(b.init(attribute.name, expression));
4133
}
4234
}
4335

36+
const nodes = [];
37+
4438
// Capture the `failed` implicit snippet prop
4539
for (const child of node.fragment.nodes) {
4640
if (child.type === 'SnippetBlock' && child.expression.name === 'failed') {
@@ -54,15 +48,7 @@ export function SvelteBoundary(node, context) {
5448
}
5549
}
5650

57-
const block = /** @type {BlockStatement} */ (
58-
context.visit(
59-
{
60-
...node.fragment,
61-
nodes
62-
},
63-
{ ...context.state }
64-
)
65-
);
51+
const block = /** @type {BlockStatement} */ (context.visit({ ...node.fragment, nodes }));
6652

6753
const boundary = b.stmt(
6854
b.call('$.boundary', context.state.node, props, b.arrow([b.id('$$anchor')], block))

0 commit comments

Comments
 (0)