Skip to content

Commit 97b78b3

Browse files
committed
wrap in try-catch
1 parent 56a396e commit 97b78b3

File tree

3 files changed

+50
-13
lines changed

3 files changed

+50
-13
lines changed

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

Lines changed: 11 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -350,7 +350,7 @@ export function client_component(analysis, options) {
350350
const push_args = [b.id('$$props'), b.literal(analysis.runes)];
351351
if (dev) push_args.push(b.id(analysis.name));
352352

353-
const component_block = b.block([
353+
let component_block = b.block([
354354
...store_setup,
355355
...legacy_reactive_declarations,
356356
...group_binding_declarations,
@@ -362,10 +362,6 @@ export function client_component(analysis, options) {
362362
.../** @type {ESTree.Statement[]} */ (template.body)
363363
]);
364364

365-
if (analysis.disposable.length > 0) {
366-
component_block.body.push(b.stmt(b.call('$.dispose', ...analysis.disposable)));
367-
}
368-
369365
if (!analysis.runes) {
370366
// Bind static exports to props so that people can access them with bind:x
371367
for (const { name, alias } of analysis.exports) {
@@ -495,6 +491,16 @@ export function client_component(analysis, options) {
495491

496492
body = [...imports, ...state.module_level_snippets, ...body];
497493

494+
if (analysis.disposable.length > 0) {
495+
component_block = b.block([
496+
b.declaration(
497+
'var',
498+
analysis.disposable.map((id) => b.declarator(id))
499+
),
500+
b.try(component_block.body, null, [b.stmt(b.call('$.dispose', ...analysis.disposable))])
501+
]);
502+
}
503+
498504
const component = b.function_declaration(
499505
b.id(analysis.name),
500506
should_inject_props ? [b.id('$$anchor'), b.id('$$props')] : [b.id('$$anchor')],

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

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -351,14 +351,14 @@ export function VariableDeclaration(node, context) {
351351
...node.declarations.map((declarator) => /** @type {Identifier} */ (declarator.id))
352352
);
353353

354-
if (dev) {
355-
declarations = declarations.map((declarator) => ({
356-
...declarator,
357-
init: b.call('$.disposable', /** @type {Expression} */ (declarator.init))
358-
}));
359-
}
354+
const assignments = declarations.map((declarator) => {
355+
let init = /** @type {Expression} */ (declarator.init);
356+
if (dev) init = b.call('$.disposable', init);
357+
358+
return b.assignment('=', declarator.id, init);
359+
});
360360

361-
kind = 'const';
361+
return assignments.length === 1 ? assignments[0] : b.sequence(assignments);
362362
}
363363

364364
return {

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

Lines changed: 32 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -635,6 +635,35 @@ export function throw_error(str) {
635635
};
636636
}
637637

638+
/**
639+
* @param {ESTree.Statement[]} body
640+
* @param {ESTree.CatchClause | null} handler
641+
* @param {ESTree.Statement[] | null} finalizer
642+
* @returns {ESTree.TryStatement}
643+
*/
644+
function try_builder(body, handler, finalizer) {
645+
return {
646+
type: 'TryStatement',
647+
block: block(body),
648+
handler,
649+
finalizer: finalizer && block(finalizer)
650+
};
651+
}
652+
653+
/**
654+
*
655+
* @param {ESTree.Pattern | null} param
656+
* @param {ESTree.Statement[]} body
657+
* @returns {ESTree.CatchClause}
658+
*/
659+
function catch_clause(param, body) {
660+
return {
661+
type: 'CatchClause',
662+
param,
663+
body: block(body)
664+
};
665+
}
666+
638667
export {
639668
await_builder as await,
640669
let_builder as let,
@@ -648,7 +677,9 @@ export {
648677
if_builder as if,
649678
this_instance as this,
650679
null_instance as null,
651-
debugger_builder as debugger
680+
debugger_builder as debugger,
681+
try_builder as try,
682+
catch_clause as catch
652683
};
653684

654685
/**

0 commit comments

Comments
 (0)