Skip to content
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

Add clj-kondo support for cljd.flutter.alpha/widget #29

Merged
merged 2 commits into from
Apr 21, 2022
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
3 changes: 2 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
.cpcache
.lsp/.cache
.clj-kondo/.cache
.clj-kondo/*
!.clj-kondo/config.edn
.clojuredart
.dart_tool
.packages
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
{:hooks {:analyze-call {cljd.flutter.alpha/widget hooks.flutter/widget}}}
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
(ns hooks.flutter
(:require [clj-kondo.hooks-api :as api]
[clojure.set :as set]
[clojure.string :as str]))

(defn widget
"Hook for cljd.flutter.alpha/widget macro"
[{:keys [:node]}]
(let [args (rest (:children node))
opts-nodes (take-while (comp api/keyword-node? first) (partition 2 args))
pairs->value (fn [k] (first (filter #(-> % first (api/sexpr) (= k)) opts-nodes)))
[_ state] (pairs->value :state)
[_ context] (pairs->value :context)
[_ watch] (pairs->value :watch)
[_ key] (pairs->value :key)

bindings-count (* 2 (count opts-nodes))
body (drop bindings-count args)
error (fn [msg] (throw (ex-info msg {})))
unknown-keys (set/difference
(into #{} (map (comp api/sexpr first) opts-nodes))
#{:state :key :watch :context})]

(cond
(and (seq (filter api/keyword-node? args)) ; has top keywords
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

could raise some false positive but it's so unlikely and useless that's ok

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

What examples do you think may become a false positive?
Only when there is a key-word pair, it raises an exception
image

(not (api/keyword-node? (first args)))) ; but first form is not a keyword
(error (str "Unknown first form " (api/sexpr (first args))))

(and state watch)
(error ":state and :watch option keys are mutually exclusive")

(and state (or (not (api/vector-node? state))
(not= (count (:children state)) 2)
(not (-> state :children first api/token-node?))))
(error ":state should be a vector [name initial-value]")

(seq unknown-keys)
(error (str "Unsupported option keys " (str/join " " unknown-keys)))

(= 0 (- (count args) bindings-count))
(error (str "No body provided"))

:else
{:node (api/list-node
(list
(api/token-node 'let)
(api/vector-node (concat (if context
[context (api/token-node 'identity)]
[])
(:children state)))
watch ;; to lint 'watch and 'key
key
body))})))