Skip to content

Commit

Permalink
fix
Browse files Browse the repository at this point in the history
  • Loading branch information
XantreDev committed May 16, 2024
1 parent 068cdfd commit 1b339b4
Show file tree
Hide file tree
Showing 4 changed files with 39 additions and 6 deletions.
5 changes: 5 additions & 0 deletions .changeset/mean-jokes-crash.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"@preact-signals/utils": patch
---

Fixed incorrect babel transform of state macro identifier inside of `$$`
14 changes: 14 additions & 0 deletions packages/utils/src/__tests__/__snapshots__/babel.test.ts.snap
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,13 @@ const a = _$(() => 1);

exports[`@preact-signals/utils/macro > Must remove import event if not used 1`] = `""`;

exports[`@preact-signals/utils/macro > Ref macro inside of state macro 1`] = `
"import { $ as _$, deepSignal as _deepSignal } from "@preact-signals/utils";
let a = _deepSignal(10);
let b = _deepSignal(_$(() => a.value));
"
`;

exports[`@preact-signals/utils/macro > Replaces $useState references 1`] = `
"import {
useDeepSignal as _useDeepSignal,
Expand Down Expand Up @@ -87,6 +94,13 @@ const state = (0, _macroHelper.computed)(() => 10);
"
`;

exports[`@preact-signals/utils/macro > State macro inside of ref macro 1`] = `
"import { $ as _$, deepSignal as _deepSignal } from "@preact-signals/utils";
let a = _deepSignal(10);
_$(() => a.value);
"
`;

exports[`@preact-signals/utils/macro > Top level macro works 1`] = `
"import { deepSignal as _deepSignal } from "@preact-signals/utils";
let a = _deepSignal(0);
Expand Down
18 changes: 18 additions & 0 deletions packages/utils/src/__tests__/babel.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -220,6 +220,24 @@ describe.concurrent("@preact-signals/utils/macro", () => {
}
`
),
TestCase.makeSuccess(
"State macro inside of ref macro",
`
import { $$, $state } from '@preact-signals/utils/macro'
let a = $state(10)
$$(a)
`
),
TestCase.makeSuccess("Ref macro inside of state macro",
`
import { $$, $state } from '@preact-signals/utils/macro'
let a = $state(10)
let b = $state($$(a))
`
),
TestCase.makeConfigurable(
"Should transform by preset-env correctly",
`
Expand Down
8 changes: 2 additions & 6 deletions packages/utils/src/babel.ts
Original file line number Diff line number Diff line change
Expand Up @@ -471,12 +471,8 @@ const processRefMacros = (
const [res] = parent.replaceWith(
t.callExpression(callee, [t.arrowFunctionExpression([], arg)])
);
if (path.scope.hasBinding(callee.name)) {
path.scope.getBinding(callee.name)?.reference(res);
} else {
console.warn("expected binding to exist", path.node.loc);
}

// crawling newly created scope
res.scope.crawl()
binding.dereference();
}
if (binding.references !== 0) {
Expand Down

0 comments on commit 1b339b4

Please sign in to comment.