diff --git a/packages/react-transform/src/index.ts b/packages/react-transform/src/index.ts index aea05ebc8..12ac23cc1 100644 --- a/packages/react-transform/src/index.ts +++ b/packages/react-transform/src/index.ts @@ -103,6 +103,7 @@ function isOptedIntoSignalTracking(path: NodePath | null): boolean { ); case "ExportDefaultDeclaration": case "ExportNamedDeclaration": + case "ObjectProperty": return hasLeadingOptInComment(path); default: return false; @@ -124,6 +125,7 @@ function isOptedOutOfSignalTracking(path: NodePath | null): boolean { ); case "ExportDefaultDeclaration": case "ExportNamedDeclaration": + case "ObjectProperty": return hasLeadingOptOutComment(path); default: return false; diff --git a/packages/react-transform/test/node/index.test.tsx b/packages/react-transform/test/node/index.test.tsx index fba75fea3..945e300aa 100644 --- a/packages/react-transform/test/node/index.test.tsx +++ b/packages/react-transform/test/node/index.test.tsx @@ -469,6 +469,48 @@ describe("React Signals Babel Transform", () => { runTest(inputCode, expectedOutput, { mode: "manual" }); }); + + it("transforms functions declared as object properties with leading opt-in JSDoc comments", () => { + const inputCode = ` + var obj = { + /** @trackSignals */ + a: () => {}, + /** @trackSignals */ + b: function () {}, + /** @trackSignals */ + c: function c() {}, + }; + `; + + const expectedOutput = ` + import { useSignals as _useSignals } from "@preact/signals-react/runtime"; + var obj = { + /** @trackSignals */ + a: () => { + var _effect = _useSignals(); + try {} finally { + _effect.f(); + } + }, + /** @trackSignals */ + b: function () { + var _effect2 = _useSignals(); + try {} finally { + _effect2.f(); + } + }, + /** @trackSignals */ + c: function c() { + var _effect3 = _useSignals(); + try {} finally { + _effect3.f(); + } + } + }; + `; + + runTest(inputCode, expectedOutput, { mode: "manual" }); + }); }); describe("auto mode opt-out transformations", () => {