Skip to content

Commit

Permalink
make react-key rule configurable, update docs
Browse files Browse the repository at this point in the history
  • Loading branch information
roman01la committed Jul 19, 2022
1 parent 1ee076b commit 6f4873a
Show file tree
Hide file tree
Showing 2 changed files with 32 additions and 19 deletions.
36 changes: 22 additions & 14 deletions core/src/uix/linter.clj
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,16 @@
(def ^:dynamic *in-branch?* false)
(def ^:dynamic *in-loop?* false)

(defn- read-config [path]
(let [file (io/file ".uix/config.edn")
config (try
(if (.isFile file)
(clojure.edn/read-string (slurp file))
{})
(catch Exception e
{}))]
(get-in config path)))

(defn hook? [sym]
(and (symbol? sym)
(some? (re-find #"^use-|use[A-Z]" (name sym)))))
Expand Down Expand Up @@ -79,8 +89,14 @@
(uix-element? expr) (missing-key? expr)
(list? expr) (recur (last expr))))

(def react-key-rule-enabled?
(if-some [v (read-config [:linters :react-key :enabled?])]
v
true))

(defn- lint-missing-key! [kv sym body]
(when (contains? (get mapping-forms kv) sym)
(when (and react-key-rule-enabled?
(contains? (get mapping-forms kv) sym))
(lint-missing-key!* (last body))))

(declare lint-body!*)
Expand Down Expand Up @@ -230,28 +246,20 @@
(symbol? (first form))
(= "subscribe" (name (first form)))))

(defn- read-config [path]
(let [file (io/file ".uix/config.edn")
config (try
(if (.isFile file)
(clojure.edn/read-string (slurp file))
{})
(catch Exception e
{}))]
(get-in config path)))

(defn- read-re-frame-config []
(-> (reduce-kv (fn [ret k v]
(update ret v (fnil conj #{}) k))
'{re-frame.core/subscribe #{re-frame.core/subscribe}}
(read-config [:linters :re-frame :resolve-as]))
(get 're-frame.core/subscribe)))

(def re-frame-config
(read-re-frame-config))

(defn lint-re-frame! [form env]
(let [config (read-re-frame-config)
sources (->> (uix.lib/find-form rf-subscribe-call? form)
(let [sources (->> (uix.lib/find-form rf-subscribe-call? form)
(keep #(let [v (ana/resolve-var env (first %))]
(when (contains? config (:name v))
(when (contains? re-frame-config (:name v))
(assoc v :source %)))))]
(run! #(ana/warning ::non-reactive-re-frame-subscribe env %)
sources)))
Expand Down
15 changes: 10 additions & 5 deletions docs/code-linting.md
Original file line number Diff line number Diff line change
Expand Up @@ -163,6 +163,15 @@ UIx will check for missing `:key` attribute when UIx element is rendered as a li
($ item {:title "hello" :x x :key x})) ;; no error
```

## Config

UIx's linter can be provided with external configuration that should live in `.uix/config.edn` file at the root of your project.

```clojure
{:linters {:react-key {:enabled? false}}}
;; the rule is enabled by default
```

# Reagent interop linter

When migrating from Reagent + re-frame to UIx you might want to keep using re-frame or at least stick with it for some time, because migrating data management is not as simple as rewriting UI components.
Expand All @@ -184,14 +193,10 @@ re-frame subscription (rf/subscribe [:user/id])) is non-reactive in UIx componen
Read https://github.com/pitch-io/uix/blob/master/docs/interop-with-reagent.md#syncing-with-ratoms-and-re-frame for more context
```

# Configuring the linter
## Config

UIx's linter can be provided with external configuration that should live in `.uix/config.edn` file at the root of your project.

Currently the only onfiguration option available is for re-frame `subscribe` checks for cases when you are wrapping the function in application code.

Example

```clojure
{:linters
{:re-frame
Expand Down

0 comments on commit 6f4873a

Please sign in to comment.