From 26c6d6f95d21de8cb524896b8433045ba0c7170d Mon Sep 17 00:00:00 2001 From: Rich Harris Date: Thu, 7 Dec 2023 10:45:04 -0500 Subject: [PATCH] chore: avoid creating unnecessary thunks (#9841) Co-authored-by: Rich Harris --- .changeset/poor-seahorses-flash.md | 5 +++++ packages/svelte/src/compiler/utils/builders.js | 11 ++++++++++- .../_expected/client/main.svelte.js | 8 ++++---- .../svelte-element/_expected/client/index.svelte.js | 2 +- 4 files changed, 20 insertions(+), 6 deletions(-) create mode 100644 .changeset/poor-seahorses-flash.md diff --git a/.changeset/poor-seahorses-flash.md b/.changeset/poor-seahorses-flash.md new file mode 100644 index 000000000000..4f6f146fe69a --- /dev/null +++ b/.changeset/poor-seahorses-flash.md @@ -0,0 +1,5 @@ +--- +'svelte': patch +--- + +chore: avoid creating thunk for call expressions when appropriate diff --git a/packages/svelte/src/compiler/utils/builders.js b/packages/svelte/src/compiler/utils/builders.js index dd6cff6f9153..d98b840a195e 100644 --- a/packages/svelte/src/compiler/utils/builders.js +++ b/packages/svelte/src/compiler/utils/builders.js @@ -370,9 +370,18 @@ export function template(elements, expressions) { /** * @param {import('estree').Expression | import('estree').BlockStatement} expression - * @returns {import('estree').ArrowFunctionExpression} + * @returns {import('estree').Expression} */ export function thunk(expression) { + if ( + expression.type === 'CallExpression' && + expression.callee.type !== 'Super' && + expression.callee.type !== 'MemberExpression' && + expression.arguments.length === 0 + ) { + return expression.callee; + } + return arrow([], expression); } diff --git a/packages/svelte/tests/snapshot/samples/dynamic-attributes-casing/_expected/client/main.svelte.js b/packages/svelte/tests/snapshot/samples/dynamic-attributes-casing/_expected/client/main.svelte.js index 75d495c0a7fd..e233a9d51e0a 100644 --- a/packages/svelte/tests/snapshot/samples/dynamic-attributes-casing/_expected/client/main.svelte.js +++ b/packages/svelte/tests/snapshot/samples/dynamic-attributes-casing/_expected/client/main.svelte.js @@ -21,9 +21,9 @@ export default function Main($$anchor, $$props) { var custom_element_1 = $.sibling($.sibling(svg_1)); /* Update */ - $.attr_effect(div, "foobar", () => y()); - $.attr_effect(svg_1, "viewBox", () => y()); - $.set_custom_element_data_effect(custom_element_1, "fooBar", () => y()); + $.attr_effect(div, "foobar", y); + $.attr_effect(svg_1, "viewBox", y); + $.set_custom_element_data_effect(custom_element_1, "fooBar", y); var node_foobar; var svg_viewBox; @@ -45,4 +45,4 @@ export default function Main($$anchor, $$props) { $.close_frag($$anchor, fragment); $.pop(); -} \ No newline at end of file +} diff --git a/packages/svelte/tests/snapshot/samples/svelte-element/_expected/client/index.svelte.js b/packages/svelte/tests/snapshot/samples/svelte-element/_expected/client/index.svelte.js index ac61fc9e6d31..937e22410f67 100644 --- a/packages/svelte/tests/snapshot/samples/svelte-element/_expected/client/index.svelte.js +++ b/packages/svelte/tests/snapshot/samples/svelte-element/_expected/client/index.svelte.js @@ -11,7 +11,7 @@ export default function Svelte_element($$anchor, $$props) { var fragment = $.comment($$anchor); var node = $.child_frag(fragment); - $.element(node, () => tag()); + $.element(node, tag); $.close_frag($$anchor, fragment); $.pop(); }