Skip to content

Defeat clj-kondo's static type analysis #32

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 3 commits into from
Jan 27, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions CHANGES.md
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,10 @@ The `abort` function assumes an exit status of 1 if the first message passed is
It also now prints the tool and command name in bold green (to be conistent with
the rest of the library).

The clj-kondo hook for the `defcommand` now produces slightly different code, that defeats
the clj-kondo static type analysis, preventing spurious warnings about vectors passed
to particular clojure.core functions.

Dependencies were not properly declared for Clojure applications that use cli-tools
(libraries that are bundled with Babashka were omitted).

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,23 @@
(next more-terms)
(update blocks :opts conj term (first more-terms)))))))

(defn xform-opts
"This is a lot of work to actually defeat clj-kondo's normal static analysis.

See https://github.com/hlship/cli-tools/issues/28 for more info."
[opts]
(when (seq opts)
(let [pairs (partition 2 opts)
collector (api/token-node (gensym "options"))
map-terms (mapcat (fn [[sym v]]
[(api/keyword-node (-> sym str keyword)) v])
pairs)]
[collector
(api/map-node (vec map-terms))

(api/map-node [(api/keyword-node :keys)
(api/vector-node (map first pairs))])
collector])))

(defn- parse-interface
[interface]
Expand All @@ -45,7 +62,7 @@
vector (api/vector-node
(concat
lets
opts
(xform-opts opts)
afters))]
vector))

Expand Down
Loading