Skip to content

Commit fbb8839

Browse files
chore: simplify accessor return (#10520)
Return `pop` directly by returning the component from it; gets rid of extraneous variable declaration --------- Co-authored-by: Rich Harris <[email protected]> Co-authored-by: Simon H <[email protected]>
1 parent 8feb865 commit fbb8839

File tree

2 files changed

+15
-16
lines changed

2 files changed

+15
-16
lines changed

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

Lines changed: 6 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -288,17 +288,12 @@ export function client_component(source, analysis, options) {
288288
)
289289
: () => {};
290290

291-
if (properties.length > 0) {
292-
component_block.body.push(
293-
b.var('$$accessors', b.object(properties)),
294-
b.stmt(b.call('$.pop', b.id('$$accessors')))
295-
);
296-
append_styles();
297-
component_block.body.push(b.return(b.id('$$accessors')));
298-
} else {
299-
component_block.body.push(b.stmt(b.call('$.pop')));
300-
append_styles();
301-
}
291+
append_styles();
292+
component_block.body.push(
293+
properties.length > 0
294+
? b.return(b.call('$.pop', b.object(properties)))
295+
: b.stmt(b.call('$.pop'))
296+
);
302297

303298
if (analysis.uses_rest_props) {
304299
/** @type {string[]} */

packages/svelte/src/internal/client/runtime.js

Lines changed: 9 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1906,14 +1906,15 @@ export function push(props, runes = false) {
19061906
}
19071907

19081908
/**
1909-
* @param {Record<string, any>} [accessors]
1910-
* @returns {void}
1909+
* @template {Record<string, any>} T
1910+
* @param {T} [component]
1911+
* @returns {T}
19111912
*/
1912-
export function pop(accessors) {
1913+
export function pop(component) {
19131914
const context_stack_item = current_component_context;
19141915
if (context_stack_item !== null) {
1915-
if (accessors !== undefined) {
1916-
context_stack_item.a = accessors;
1916+
if (component !== undefined) {
1917+
context_stack_item.a = component;
19171918
}
19181919
const effects = context_stack_item.e;
19191920
if (effects !== null) {
@@ -1925,6 +1926,9 @@ export function pop(accessors) {
19251926
current_component_context = context_stack_item.p;
19261927
context_stack_item.m = true;
19271928
}
1929+
// Micro-optimization: Don't set .a above to the empty object
1930+
// so it can be garbage-collected when the return here is unused
1931+
return component || /** @type {T} */ ({});
19281932
}
19291933

19301934
/**

0 commit comments

Comments
 (0)