diff --git a/dev/refresh_example.cljs b/dev/refresh_example.cljs index c3720c3..bd40ad0 100644 --- a/dev/refresh_example.cljs +++ b/dev/refresh_example.cljs @@ -9,7 +9,8 @@ (defnc app [] {:helix/features {:fast-refresh true}} - (let [[o set-o] (hooks/use-state + (let [_ (hooks/use-callback :auto-deps #(apply str %1 %&)) + [o set-o] (hooks/use-state #js {:name "Lisa"})] (d/div {:style {:text-align "center" diff --git a/src/helix/impl/analyzer.clj b/src/helix/impl/analyzer.clj index 11e1591..a146d44 100644 --- a/src/helix/impl/analyzer.clj +++ b/src/helix/impl/analyzer.clj @@ -73,14 +73,23 @@ Example: ($ %s %s ...)" (map :name) vec))) +(defn- normalise-jsval + [x] + (if (= JSValue (type x)) + (.-val x) + x)) + +(defn- normalise-sym + [x] + (if (symbol? x) + (symbol (string/replace (str x) #"^((p\d+|rest))(__\d+)#$" "$1__#")) + x)) -(defn- unwrap-js-vals +(defn- normalise-vals [form] (clojure.walk/prewalk (fn unwrap [x] - (if (= JSValue (type x)) - (.-val x) - x)) + (-> x normalise-jsval normalise-sym)) form)) @@ -105,7 +114,7 @@ Example: ($ %s %s ...)" (defn find-hooks [body] (let [f (fn f [matches form] - (let [form (unwrap-js-vals form)] + (let [form (normalise-vals form)] (if (and (seqable? form) (not ;; Ignore quoted forms, e.g. '(use-foo) diff --git a/test/helix/impl/analyzer_test.clj b/test/helix/impl/analyzer_test.clj index 6039286..ed4b7ba 100644 --- a/test/helix/impl/analyzer_test.clj +++ b/test/helix/impl/analyzer_test.clj @@ -189,4 +189,19 @@ '[(use-foo)] '(use-foo) '[(use-foo)] '{:foo (use-foo)} '[(use-foo)] '{:foo #{use-bar (use-foo)}} - '[] '('use-foo bar))) + '[] '('use-foo bar) + '[(use-callback (fn* [p1__#] (identity p1__#)))] '(use-callback (fn* [p1__20354#] (identity p1__20354#)))) + (t/testing "normalises bindings" + (t/are [form1 form2] + (= (hana/find-hooks (list 'use-callback form1)) + (hana/find-hooks (list 'use-callback form2))) + '(fn* [p1__20354#] (identity p1__20354#)) + '(fn* [p1__20364#] (identity p1__20364#)) + + '(fn* [p1__20356# & rest__20357#] (identity p1__20356# rest__20357#)) + '(fn* [p1__20366# & rest__20367#] (identity p1__20366# rest__20367#)) + + '(fn* [p1__20359# p2__20360# p3__20361# & rest__20362#] + (identity p1__20359# p2__20360# p3__20361# rest__20362#)) + '(fn* [p1__20369# p2__20370# p3__20371# & rest__20372#] + (identity p1__20369# p2__20370# p3__20371# rest__20372#)))))