Skip to content

Commit

Permalink
propagate rules of hooks check to account for nesting
Browse files Browse the repository at this point in the history
  • Loading branch information
roman01la committed Nov 4, 2022
1 parent 4d09766 commit f7276de
Show file tree
Hide file tree
Showing 3 changed files with 14 additions and 2 deletions.
4 changes: 2 additions & 2 deletions core/src/uix/linter.clj
Original file line number Diff line number Diff line change
Expand Up @@ -200,8 +200,8 @@
[expr & {:keys [in-branch? in-loop?]
:or {in-branch? *in-branch?*
in-loop? *in-loop?*}}]
(binding [*in-branch?* in-branch?
*in-loop?* in-loop?]
(binding [*in-branch?* (or *in-branch?* in-branch?)
*in-loop?* (or *in-loop?* in-loop?)]
(clojure.walk/prewalk
(fn [form]
(cond
Expand Down
6 changes: 6 additions & 0 deletions core/test/uix/linter_test.clj
Original file line number Diff line number Diff line change
Expand Up @@ -254,6 +254,12 @@
(is (str/includes? out-str (str :uix.linter/hook-in-loop)))
(is (str/includes? out-str "React Hook (uix.core/use-effect (fn [])) may be executed more than once.")))

(testing "should fail on hook call in nested branch or loop"
(is (str/includes? out-str (str :uix.linter/hook-in-branch)))
(is (str/includes? out-str (str :uix.linter/hook-in-loop)))
(is (str/includes? out-str "React Hook (uix.core/use-effect (fn [] \"nested condition\")) is called conditionally."))
(is (str/includes? out-str "React Hook (uix.core/use-effect (fn [] \"nested loop\")) may be executed more than once.")))

(testing "should fail on missing key in a loop"
(is (str/includes? out-str (str :uix.linter/missing-key)))
(is (str/includes? out-str "UIx element is missing :key attribute, which is required"))
Expand Down
6 changes: 6 additions & 0 deletions core/test/uix/linter_test.cljs
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,12 @@
(for [x []]
(uix.core/use-effect (fn []))))

(defui test-nested []
(when false
(or (uix.core/use-effect (fn [] "nested condition")) 1))
(loop []
(loop [x (uix.core/use-effect (fn [] "nested loop"))])))

(defui test-missing-key []
(for [x []]
($ :div.test-missing-key {} x))
Expand Down

0 comments on commit f7276de

Please sign in to comment.