Skip to content

Commit 529571d

Browse files
committed
simplify
1 parent 37d02af commit 529571d

File tree

6 files changed

+21
-34
lines changed

6 files changed

+21
-34
lines changed

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

Lines changed: 7 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
/** @import { ArrowFunctionExpression, AssignmentExpression, Expression, FunctionDeclaration, FunctionExpression, Identifier, Node, Pattern, UpdateExpression } from 'estree' */
1+
/** @import { ArrowFunctionExpression, AssignmentExpression, BlockStatement, Expression, FunctionDeclaration, FunctionExpression, Identifier, Node, Pattern, UpdateExpression } from 'estree' */
22
/** @import { Binding } from '#compiler' */
33
/** @import { ClientTransformState, ComponentClientTransformState, ComponentContext } from './types.js' */
44
/** @import { Analysis } from '../../types.js' */
@@ -289,20 +289,15 @@ export function should_proxy(node, scope) {
289289
/**
290290
* Svelte legacy mode should use safe equals in most places, runes mode shouldn't
291291
* @param {ComponentClientTransformState} state
292-
* @param {Expression} arg
292+
* @param {Expression | BlockStatement} expression
293293
* @param {boolean} [async]
294294
*/
295-
export function create_derived(state, arg, async = false) {
295+
export function create_derived(state, expression, async = false) {
296+
const thunk = b.thunk(expression, async);
297+
296298
if (async) {
297-
return b.call(
298-
b.await(
299-
b.call(
300-
'$.save',
301-
b.call('$.async_derived', arg.type === 'ArrowFunctionExpression' ? b.async(arg) : arg)
302-
)
303-
)
304-
);
299+
return b.call(b.await(b.call('$.save', b.call('$.async_derived', thunk))));
305300
} else {
306-
return b.call(state.analysis.runes ? '$.derived' : '$.derived_safe_equal', arg);
301+
return b.call(state.analysis.runes ? '$.derived' : '$.derived_safe_equal', thunk);
307302
}
308303
}

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

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -96,13 +96,13 @@ function create_derived_block_argument(node, context) {
9696
b.return(b.object(identifiers.map((identifier) => b.prop('init', identifier, identifier))))
9797
]);
9898

99-
const declarations = [b.var(value, create_derived(context.state, b.thunk(block)))];
99+
const declarations = [b.var(value, create_derived(context.state, block))];
100100

101101
for (const id of identifiers) {
102102
context.state.transform[id.name] = { read: get_value };
103103

104104
declarations.push(
105-
b.var(id, create_derived(context.state, b.thunk(b.member(b.call('$.get', value), id))))
105+
b.var(id, create_derived(context.state, b.member(b.call('$.get', value), id)))
106106
);
107107
}
108108

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

Lines changed: 8 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -21,11 +21,8 @@ export function ConstTag(node, context) {
2121
declaration.init,
2222
node.metadata.expression
2323
);
24-
let expression = create_derived(
25-
context.state,
26-
b.thunk(init),
27-
node.metadata.expression.has_await
28-
);
24+
25+
let expression = create_derived(context.state, init, node.metadata.expression.has_await);
2926

3027
if (dev) {
3128
expression = b.call('$.tag', expression, b.literal(declaration.id.name));
@@ -65,15 +62,13 @@ export function ConstTag(node, context) {
6562
declaration.init,
6663
node.metadata.expression
6764
);
68-
const fn = b.arrow(
69-
[],
70-
b.block([
71-
b.const(/** @type {Pattern} */ (context.visit(declaration.id, child_state)), init),
72-
b.return(b.object(identifiers.map((node) => b.prop('init', node, node))))
73-
])
74-
);
7565

76-
let expression = create_derived(context.state, fn, node.metadata.expression.has_await);
66+
const block = b.block([
67+
b.const(/** @type {Pattern} */ (context.visit(declaration.id, child_state)), init),
68+
b.return(b.object(identifiers.map((node) => b.prop('init', node, node))))
69+
]);
70+
71+
let expression = create_derived(context.state, block, node.metadata.expression.has_await);
7772

7873
if (dev) {
7974
expression = b.call('$.tag', expression, b.literal('[@const]'));

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

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -46,9 +46,6 @@ export function LetDirective(node, context) {
4646
read: (node) => b.call('$.get', node)
4747
};
4848

49-
return b.const(
50-
name,
51-
create_derived(context.state, b.thunk(b.member(b.id('$$slotProps'), node.name)))
52-
);
49+
return b.const(name, create_derived(context.state, b.member(b.id('$$slotProps'), node.name)));
5350
}
5451
}

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -81,7 +81,7 @@ export function SnippetBlock(node, context) {
8181
? b.call(
8282
'$.wrap_snippet',
8383
b.id(context.state.analysis.name),
84-
has_await ? b.async(b.function(null, args, body)) : b.function(null, args, body)
84+
b.function(null, args, body, has_await)
8585
)
8686
: b.arrow(args, body, has_await);
8787

packages/svelte/src/compiler/utils/builders.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -588,14 +588,14 @@ export function method(kind, key, params, body, computed = false, is_static = fa
588588
* @param {ESTree.BlockStatement} body
589589
* @returns {ESTree.FunctionExpression}
590590
*/
591-
function function_builder(id, params, body) {
591+
function function_builder(id, params, body, async = false) {
592592
return {
593593
type: 'FunctionExpression',
594594
id,
595595
params,
596596
body,
597597
generator: false,
598-
async: false,
598+
async,
599599
metadata: /** @type {any} */ (null) // should not be used by codegen
600600
};
601601
}

0 commit comments

Comments
 (0)