diff --git a/packages/translator/src/__tests__/fixtures/basic-effect-no-deps/__snapshots__/dom.expected.js b/packages/translator/src/__tests__/fixtures/basic-effect-no-deps/__snapshots__/dom.expected.js index c8a2042cb..7af667d9a 100644 --- a/packages/translator/src/__tests__/fixtures/basic-effect-no-deps/__snapshots__/dom.expected.js +++ b/packages/translator/src/__tests__/fixtures/basic-effect-no-deps/__snapshots__/dom.expected.js @@ -1,11 +1,7 @@ -import { userEffect as _userEffect, register as _register, bind as _bind, queueHydrate as _queueHydrate, createRenderFn as _createRenderFn } from "@marko/runtime-fluurt/src/dom"; - -const _temp = function (_scope) { - document.body.className = "no-deps"; -}; +import { register as _register, queueHydrate as _queueHydrate, createRenderFn as _createRenderFn } from "@marko/runtime-fluurt/src/dom"; function _hydrate(_scope) { - _userEffect(_scope, 0, _bind(_scope, _temp)); + document.body.className = "no-deps"; } _register("packages/translator/src/__tests__/fixtures/basic-effect-no-deps/template.marko_0", _hydrate); diff --git a/packages/translator/src/__tests__/fixtures/effect-counter/__snapshots__/dom.expected.js b/packages/translator/src/__tests__/fixtures/effect-counter/__snapshots__/dom.expected.js index 9cc844bd3..a967a4b4e 100644 --- a/packages/translator/src/__tests__/fixtures/effect-counter/__snapshots__/dom.expected.js +++ b/packages/translator/src/__tests__/fixtures/effect-counter/__snapshots__/dom.expected.js @@ -1,9 +1,4 @@ -import { queue as _queue, userEffect as _userEffect, on as _on, register as _register, bind as _bind, queueHydrate as _queueHydrate, write as _write, createRenderFn as _createRenderFn } from "@marko/runtime-fluurt/src/dom"; - -const _temp = function (_scope) { - const clickCount = _scope[1]; - document.getElementById("button").textContent = clickCount; -}; +import { queue as _queue, on as _on, register as _register, bind as _bind, queueHydrate as _queueHydrate, write as _write, createRenderFn as _createRenderFn } from "@marko/runtime-fluurt/src/dom"; const _onclick = function (_scope) { const clickCount = _scope[1]; @@ -12,7 +7,7 @@ const _onclick = function (_scope) { }; function _hydrate_clickCount(_scope, clickCount = _scope[1]) { - _userEffect(_scope, 2, _bind(_scope, _temp)); + document.getElementById("button").textContent = clickCount; _on(_scope[0], "click", _bind(_scope, _onclick)); } diff --git a/packages/translator/src/__tests__/fixtures/effect-tag/__snapshots__/dom.expected.js b/packages/translator/src/__tests__/fixtures/effect-tag/__snapshots__/dom.expected.js index 91c0c6830..944a28f8b 100644 --- a/packages/translator/src/__tests__/fixtures/effect-tag/__snapshots__/dom.expected.js +++ b/packages/translator/src/__tests__/fixtures/effect-tag/__snapshots__/dom.expected.js @@ -1,12 +1,7 @@ -import { userEffect as _userEffect, register as _register, bind as _bind, queueHydrate as _queueHydrate, write as _write, createRenderFn as _createRenderFn } from "@marko/runtime-fluurt/src/dom"; - -const _temp = function (_scope) { - const x = _scope[0]; - document.getElementById("ref").textContent = x; -}; +import { register as _register, queueHydrate as _queueHydrate, write as _write, createRenderFn as _createRenderFn } from "@marko/runtime-fluurt/src/dom"; function _hydrate_x(_scope, x = _scope[0]) { - _userEffect(_scope, 1, _bind(_scope, _temp)); + document.getElementById("ref").textContent = x; } _register("packages/translator/src/__tests__/fixtures/effect-tag/template.marko_0_x", _hydrate_x); diff --git a/packages/translator/src/core/effect.ts b/packages/translator/src/core/effect.ts index f9f34ed0c..74eecb708 100644 --- a/packages/translator/src/core/effect.ts +++ b/packages/translator/src/core/effect.ts @@ -41,18 +41,32 @@ export default { const sectionId = getSectionId(tag); if (isOutputDOM()) { const cleanupIndex = tag.node.extra!.reserve!.id; + const { value } = defaultAttr; + let inlineStatements = null; + if ( + t.isFunctionExpression(value) || + (t.isArrowFunctionExpression(value) && t.isBlockStatement(value.body)) + ) { + inlineStatements = (value.body as t.BlockStatement).body; + t.traverse(value.body, (node) => { + if (t.isReturnStatement(node)) { + inlineStatements = null; + } + }); + } addStatement( "hydrate", sectionId, defaultAttr.extra?.valueReferences, - t.expressionStatement( - callRuntime( - "userEffect", - scopeIdentifier, - t.numericLiteral(cleanupIndex), - defaultAttr.value + inlineStatements || + t.expressionStatement( + callRuntime( + "userEffect", + scopeIdentifier, + t.numericLiteral(cleanupIndex), + defaultAttr.value + ) ) - ) ); } else { addHTMLHydrateCall(sectionId, defaultAttr.extra?.valueReferences);