Skip to content

Commit

Permalink
Add support for trackSignals comments on object property assignment
Browse files Browse the repository at this point in the history
  • Loading branch information
andrewiggins committed Jul 18, 2023
1 parent f01b413 commit 033224a
Show file tree
Hide file tree
Showing 2 changed files with 81 additions and 0 deletions.
4 changes: 4 additions & 0 deletions packages/react-transform/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -97,13 +97,15 @@ function isOptedIntoSignalTracking(path: NodePath | null): boolean {
case "FunctionDeclaration":
case "VariableDeclarator":
case "VariableDeclaration":
case "AssignmentExpression":
return (
hasLeadingOptInComment(path) ||
isOptedIntoSignalTracking(path.parentPath)
);
case "ExportDefaultDeclaration":
case "ExportNamedDeclaration":
case "ObjectProperty":
case "ExpressionStatement":
return hasLeadingOptInComment(path);
default:
return false;
Expand All @@ -119,13 +121,15 @@ function isOptedOutOfSignalTracking(path: NodePath | null): boolean {
case "FunctionDeclaration":
case "VariableDeclarator":
case "VariableDeclaration":
case "AssignmentExpression":
return (
hasLeadingOptOutComment(path) ||
isOptedOutOfSignalTracking(path.parentPath)
);
case "ExportDefaultDeclaration":
case "ExportNamedDeclaration":
case "ObjectProperty":
case "ExpressionStatement":
return hasLeadingOptOutComment(path);
default:
return false;
Expand Down
77 changes: 77 additions & 0 deletions packages/react-transform/test/node/index.test.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -511,6 +511,46 @@ describe("React Signals Babel Transform", () => {

runTest(inputCode, expectedOutput, { mode: "manual" });
});

it("transforms functions assigned to object properties with leading opt-in JSDoc comments", () => {
const inputCode = `
var obj = {};
/** @trackSignals */
obj.a = () => {};
/** @trackSignals */
obj.b = function () {};
/** @trackSignals */
obj["c"] = function () {};
`;

const expectedOutput = `
import { useSignals as _useSignals } from "@preact/signals-react/runtime";
var obj = {};
/** @trackSignals */
obj.a = () => {
var _effect = _useSignals();
try {} finally {
_effect.f();
}
};
/** @trackSignals */
obj.b = function () {
var _effect2 = _useSignals();
try {} finally {
_effect2.f();
}
};
/** @trackSignals */
obj["c"] = function () {
var _effect3 = _useSignals();
try {} finally {
_effect3.f();
}
};
`;

runTest(inputCode, expectedOutput, { mode: "manual" });
});
});

describe("auto mode opt-out transformations", () => {
Expand Down Expand Up @@ -673,6 +713,43 @@ describe("React Signals Babel Transform", () => {

runTest(inputCode, expectedOutput, { mode: "auto" });
});

it("skips transforming functions declared as object properties with leading opt-out JSDoc comments", () => {
const inputCode = `
var obj = {
/** @noTrackSignals */
a: () => {},
/** @noTrackSignals */
b: function () {},
/** @noTrackSignals */
c: function c() {}
};
`;

const expectedOutput = inputCode;

runTest(inputCode, expectedOutput, { mode: "auto" });
});

it("skips transforming functions assigned to object properties with leading opt-out JSDoc comments", () => {
const inputCode = `
var obj = {};
/** @noTrackSignals */
obj.a = () => <div />;
/** @noTrackSignals */
obj.b = function () {
return <div />;
};
/** @noTrackSignals */
obj["c"] = function () {
return <div />;
};
`;

const expectedOutput = inputCode;

runTest(inputCode, expectedOutput, { mode: "auto" });
});
});

describe("auto mode no transformations", () => {
Expand Down

0 comments on commit 033224a

Please sign in to comment.