pull/16542/head
Rich Harris 1 month ago
parent 37d02af888
commit 529571d184

@ -1,4 +1,4 @@
/** @import { ArrowFunctionExpression, AssignmentExpression, Expression, FunctionDeclaration, FunctionExpression, Identifier, Node, Pattern, UpdateExpression } from 'estree' */ /** @import { ArrowFunctionExpression, AssignmentExpression, BlockStatement, Expression, FunctionDeclaration, FunctionExpression, Identifier, Node, Pattern, UpdateExpression } from 'estree' */
/** @import { Binding } from '#compiler' */ /** @import { Binding } from '#compiler' */
/** @import { ClientTransformState, ComponentClientTransformState, ComponentContext } from './types.js' */ /** @import { ClientTransformState, ComponentClientTransformState, ComponentContext } from './types.js' */
/** @import { Analysis } from '../../types.js' */ /** @import { Analysis } from '../../types.js' */
@ -289,20 +289,15 @@ export function should_proxy(node, scope) {
/** /**
* Svelte legacy mode should use safe equals in most places, runes mode shouldn't * Svelte legacy mode should use safe equals in most places, runes mode shouldn't
* @param {ComponentClientTransformState} state * @param {ComponentClientTransformState} state
* @param {Expression} arg * @param {Expression | BlockStatement} expression
* @param {boolean} [async] * @param {boolean} [async]
*/ */
export function create_derived(state, arg, async = false) { export function create_derived(state, expression, async = false) {
const thunk = b.thunk(expression, async);
if (async) { if (async) {
return b.call( return b.call(b.await(b.call('$.save', b.call('$.async_derived', thunk))));
b.await(
b.call(
'$.save',
b.call('$.async_derived', arg.type === 'ArrowFunctionExpression' ? b.async(arg) : arg)
)
)
);
} else { } else {
return b.call(state.analysis.runes ? '$.derived' : '$.derived_safe_equal', arg); return b.call(state.analysis.runes ? '$.derived' : '$.derived_safe_equal', thunk);
} }
} }

@ -96,13 +96,13 @@ function create_derived_block_argument(node, context) {
b.return(b.object(identifiers.map((identifier) => b.prop('init', identifier, identifier)))) b.return(b.object(identifiers.map((identifier) => b.prop('init', identifier, identifier))))
]); ]);
const declarations = [b.var(value, create_derived(context.state, b.thunk(block)))]; const declarations = [b.var(value, create_derived(context.state, block))];
for (const id of identifiers) { for (const id of identifiers) {
context.state.transform[id.name] = { read: get_value }; context.state.transform[id.name] = { read: get_value };
declarations.push( declarations.push(
b.var(id, create_derived(context.state, b.thunk(b.member(b.call('$.get', value), id)))) b.var(id, create_derived(context.state, b.member(b.call('$.get', value), id)))
); );
} }

@ -21,11 +21,8 @@ export function ConstTag(node, context) {
declaration.init, declaration.init,
node.metadata.expression node.metadata.expression
); );
let expression = create_derived(
context.state, let expression = create_derived(context.state, init, node.metadata.expression.has_await);
b.thunk(init),
node.metadata.expression.has_await
);
if (dev) { if (dev) {
expression = b.call('$.tag', expression, b.literal(declaration.id.name)); expression = b.call('$.tag', expression, b.literal(declaration.id.name));
@ -65,15 +62,13 @@ export function ConstTag(node, context) {
declaration.init, declaration.init,
node.metadata.expression node.metadata.expression
); );
const fn = b.arrow(
[],
b.block([
b.const(/** @type {Pattern} */ (context.visit(declaration.id, child_state)), init),
b.return(b.object(identifiers.map((node) => b.prop('init', node, node))))
])
);
let expression = create_derived(context.state, fn, node.metadata.expression.has_await); const block = b.block([
b.const(/** @type {Pattern} */ (context.visit(declaration.id, child_state)), init),
b.return(b.object(identifiers.map((node) => b.prop('init', node, node))))
]);
let expression = create_derived(context.state, block, node.metadata.expression.has_await);
if (dev) { if (dev) {
expression = b.call('$.tag', expression, b.literal('[@const]')); expression = b.call('$.tag', expression, b.literal('[@const]'));

@ -46,9 +46,6 @@ export function LetDirective(node, context) {
read: (node) => b.call('$.get', node) read: (node) => b.call('$.get', node)
}; };
return b.const( return b.const(name, create_derived(context.state, b.member(b.id('$$slotProps'), node.name)));
name,
create_derived(context.state, b.thunk(b.member(b.id('$$slotProps'), node.name)))
);
} }
} }

@ -81,7 +81,7 @@ export function SnippetBlock(node, context) {
? b.call( ? b.call(
'$.wrap_snippet', '$.wrap_snippet',
b.id(context.state.analysis.name), b.id(context.state.analysis.name),
has_await ? b.async(b.function(null, args, body)) : b.function(null, args, body) b.function(null, args, body, has_await)
) )
: b.arrow(args, body, has_await); : b.arrow(args, body, has_await);

@ -588,14 +588,14 @@ export function method(kind, key, params, body, computed = false, is_static = fa
* @param {ESTree.BlockStatement} body * @param {ESTree.BlockStatement} body
* @returns {ESTree.FunctionExpression} * @returns {ESTree.FunctionExpression}
*/ */
function function_builder(id, params, body) { function function_builder(id, params, body, async = false) {
return { return {
type: 'FunctionExpression', type: 'FunctionExpression',
id, id,
params, params,
body, body,
generator: false, generator: false,
async: false, async,
metadata: /** @type {any} */ (null) // should not be used by codegen metadata: /** @type {any} */ (null) // should not be used by codegen
}; };
} }

Loading…
Cancel
Save