Skip to content

Commit

Permalink
Add support for trackSignals comments on object properties that are f…
Browse files Browse the repository at this point in the history
…unctions
  • Loading branch information
andrewiggins committed Jul 18, 2023
1 parent b90a938 commit f01b413
Show file tree
Hide file tree
Showing 2 changed files with 44 additions and 0 deletions.
2 changes: 2 additions & 0 deletions packages/react-transform/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -103,6 +103,7 @@ function isOptedIntoSignalTracking(path: NodePath | null): boolean {
);
case "ExportDefaultDeclaration":
case "ExportNamedDeclaration":
case "ObjectProperty":
return hasLeadingOptInComment(path);
default:
return false;
Expand All @@ -124,6 +125,7 @@ function isOptedOutOfSignalTracking(path: NodePath | null): boolean {
);
case "ExportDefaultDeclaration":
case "ExportNamedDeclaration":
case "ObjectProperty":
return hasLeadingOptOutComment(path);
default:
return false;
Expand Down
42 changes: 42 additions & 0 deletions packages/react-transform/test/node/index.test.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -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", () => {
Expand Down

0 comments on commit f01b413

Please sign in to comment.