Skip to content

Commit bab74be

Browse files
Fix anonymous functions in hooks (#158)
Fix #117
1 parent 6b3d4d3 commit bab74be

File tree

3 files changed

+32
-7
lines changed

3 files changed

+32
-7
lines changed

dev/refresh_example.cljs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,8 @@
99
(defnc app
1010
[]
1111
{:helix/features {:fast-refresh true}}
12-
(let [[o set-o] (hooks/use-state
12+
(let [_ (hooks/use-callback :auto-deps #(apply str %1 %&))
13+
[o set-o] (hooks/use-state
1314
#js {:name "Lisa"})]
1415
(d/div
1516
{:style {:text-align "center"

src/helix/impl/analyzer.clj

Lines changed: 14 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -73,14 +73,23 @@ Example: ($ %s %s ...)"
7373
(map :name)
7474
vec)))
7575

76+
(defn- normalise-jsval
77+
[x]
78+
(if (= JSValue (type x))
79+
(.-val x)
80+
x))
81+
82+
(defn- normalise-sym
83+
[x]
84+
(if (symbol? x)
85+
(symbol (string/replace (str x) #"^((p\d+|rest))(__\d+)#$" "$1__#"))
86+
x))
7687

77-
(defn- unwrap-js-vals
88+
(defn- normalise-vals
7889
[form]
7990
(clojure.walk/prewalk
8091
(fn unwrap [x]
81-
(if (= JSValue (type x))
82-
(.-val x)
83-
x))
92+
(-> x normalise-jsval normalise-sym))
8493
form))
8594

8695

@@ -105,7 +114,7 @@ Example: ($ %s %s ...)"
105114
(defn find-hooks
106115
[body]
107116
(let [f (fn f [matches form]
108-
(let [form (unwrap-js-vals form)]
117+
(let [form (normalise-vals form)]
109118
(if (and (seqable? form)
110119
(not
111120
;; Ignore quoted forms, e.g. '(use-foo)

test/helix/impl/analyzer_test.clj

Lines changed: 16 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -189,4 +189,19 @@
189189
'[(use-foo)] '(use-foo)
190190
'[(use-foo)] '{:foo (use-foo)}
191191
'[(use-foo)] '{:foo #{use-bar (use-foo)}}
192-
'[] '('use-foo bar)))
192+
'[] '('use-foo bar)
193+
'[(use-callback (fn* [p1__#] (identity p1__#)))] '(use-callback (fn* [p1__20354#] (identity p1__20354#))))
194+
(t/testing "normalises bindings"
195+
(t/are [form1 form2]
196+
(= (hana/find-hooks (list 'use-callback form1))
197+
(hana/find-hooks (list 'use-callback form2)))
198+
'(fn* [p1__20354#] (identity p1__20354#))
199+
'(fn* [p1__20364#] (identity p1__20364#))
200+
201+
'(fn* [p1__20356# & rest__20357#] (identity p1__20356# rest__20357#))
202+
'(fn* [p1__20366# & rest__20367#] (identity p1__20366# rest__20367#))
203+
204+
'(fn* [p1__20359# p2__20360# p3__20361# & rest__20362#]
205+
(identity p1__20359# p2__20360# p3__20361# rest__20362#))
206+
'(fn* [p1__20369# p2__20370# p3__20371# & rest__20372#]
207+
(identity p1__20369# p2__20370# p3__20371# rest__20372#)))))

0 commit comments

Comments
 (0)