Skip to content

Commit 1112baa

Browse files
Fix use of private function from macro
This ends up expanding in the caller's namespace, which references the private `wrap-fx` and leads to a compiler warning under Figwheel. I believe Shadow is suppressing these so it wasn't caught.
1 parent 9a870be commit 1112baa

File tree

2 files changed

+20
-19
lines changed

2 files changed

+20
-19
lines changed

src/helix/hooks.cljc

Lines changed: 11 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -11,8 +11,10 @@
1111
:once Equivalent to using [] as the deps.
1212
:auto-deps Infer the dependencies automatically from the code by finding
1313
local vars. Not available for the function form of a hook."
14-
#?(:clj (:require [helix.impl.analyzer :as hana])
14+
#?(:clj (:require [helix.impl.analyzer :as hana]
15+
[helix.impl.hooks :as-alias impl.hooks])
1516
:cljs (:require
17+
[helix.impl.hooks :as impl.hooks]
1618
["react" :as react]
1719
[goog.object :as gobj]))
1820
#?(:cljs (:require-macros [helix.hooks])))
@@ -117,16 +119,6 @@
117119
"Just react/useContext"
118120
react/useContext))
119121

120-
121-
;; React `useEffect` expects either a function or undefined to be returned
122-
#?(:cljs
123-
(defn- wrap-fx [f]
124-
(fn wrap-fx-return []
125-
(let [x (f)]
126-
(if (fn? x)
127-
x
128-
js/undefined)))))
129-
130122
#?(:clj
131123
(defn deps-macro-body [env deps body simple-body-ok? deps->hook-body]
132124
(cond
@@ -182,9 +174,9 @@
182174
(deps-macro-body
183175
&env deps body false
184176
(fn
185-
([fn-body] `^clj-nil (raw-use-effect (wrap-fx (fn [] ~@fn-body))))
177+
([fn-body] `^clj-nil (raw-use-effect (impl.hooks/wrap-fx (fn [] ~@fn-body))))
186178
([deps fn-body]
187-
`^clj-nil (raw-use-effect (wrap-fx (fn [] ~@fn-body)) ~deps))))))
179+
`^clj-nil (raw-use-effect (impl.hooks/wrap-fx (fn [] ~@fn-body)) ~deps))))))
188180

189181

190182
#?(:cljs
@@ -193,12 +185,12 @@
193185
;; be harder to read
194186
(defn use-effect*
195187
"Like react/useEffect. See `use-effect` for details on what `f`'s return values. See namespace doc for `deps`."
196-
([f] (react/useEffect (wrap-fx f)))
188+
([f] (react/useEffect (impl.hooks/wrap-fx f)))
197189
([f deps]
198190
(when goog/DEBUG
199191
(when (= deps :auto-deps)
200192
(throw (js/Error. "Can't use `:auto-deps` with `use-effect*`; use `use-effect` macro for that"))))
201-
(react/useEffect (wrap-fx f) (to-array deps)))))
193+
(react/useEffect (impl.hooks/wrap-fx f) (to-array deps)))))
202194

203195

204196
(defmacro use-layout-effect
@@ -209,20 +201,20 @@
209201
(deps-macro-body
210202
&env deps body false
211203
(fn
212-
([fn-body] `^clj-nil (raw-use-layout-effect (wrap-fx (fn [] ~@fn-body))))
204+
([fn-body] `^clj-nil (raw-use-layout-effect (impl.hooks/wrap-fx (fn [] ~@fn-body))))
213205
([deps fn-body]
214-
`^clj-nil (raw-use-layout-effect (wrap-fx (fn [] ~@fn-body)) ~deps))))))
206+
`^clj-nil (raw-use-layout-effect (impl.hooks/wrap-fx (fn [] ~@fn-body)) ~deps))))))
215207

216208

217209
#?(:cljs
218210
(defn use-layout-effect*
219211
"Like `use-effect*` but instead calls react/useLayoutEffect."
220-
([f] (react/useLayoutEffect (wrap-fx f)))
212+
([f] (react/useLayoutEffect (impl.hooks/wrap-fx f)))
221213
([f deps]
222214
(when goog/DEBUG
223215
(when (= deps :auto-deps)
224216
(throw (js/Error. "Can't use `:auto-deps` with `use-layout-effect*`; use `use-layout-effect` macro for that"))))
225-
(react/useLayoutEffect (wrap-fx f) (to-array deps)))))
217+
(react/useLayoutEffect (impl.hooks/wrap-fx f) (to-array deps)))))
226218

227219

228220
(defmacro use-memo

src/helix/impl/hooks.cljs

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
(ns helix.impl.hooks)
2+
3+
;; React `useEffect` expects either a function or undefined to be returned
4+
(defn wrap-fx [f]
5+
(fn wrap-fx-return []
6+
(let [x (f)]
7+
(if (fn? x)
8+
x
9+
js/undefined))))

0 commit comments

Comments
 (0)