Skip to content

Commit

Permalink
Fix wrong call to planner and improvements on linting
Browse files Browse the repository at this point in the history
  • Loading branch information
wilkerlucio committed Apr 20, 2022
1 parent bad83ee commit 9dfa583
Show file tree
Hide file tree
Showing 9 changed files with 103 additions and 9 deletions.
1 change: 1 addition & 0 deletions .clj-kondo/babashka/fs/config.edn
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
{:lint-as {babashka.fs/with-temp-dir clojure.core/let}}
Original file line number Diff line number Diff line change
@@ -0,0 +1,75 @@
(ns com.fulcrologic.guardrails.clj-kondo-hooks
(:require
[clj-kondo.hooks-api :as api]))

(def =>? #{'=> :ret})
(def |? #{'| :st})
(def known-sym? #{'=> '| '<-})

(defn args+gspec+body [nodes]
(let [argv (first nodes)
gspec (second nodes)
body (nnext nodes)
gspec' (->> gspec
(:children)
(filterv #(-> % :value known-sym? not))
(api/vector-node))
new-nodes (list* argv gspec' body)]
;; gspec: [arg-specs* (| arg-preds+)? => ret-spec (| fn-preds+)? (<- generator-fn)?]
(if (not= 1 (count (filter =>? (api/sexpr gspec))))
(api/reg-finding! (merge (meta gspec)
{:message (str "Gspec requires exactly one `=>` or `:ret`")
:type :clj-kondo.fulcro.>defn/invalid-gspec}))
(let [p (partition-by (comp not =>? api/sexpr) (:children gspec))
[arg [=>] [ret-spec & _output]] (if (-> p ffirst api/sexpr =>?)
(cons [] p) ; arg-specs might be empty
p)
[arg-specs [| & arg-preds]] (split-with (comp not |? api/sexpr) arg)]

(when-not ret-spec
(println =>)
(api/reg-finding! (merge (meta =>)
{:message "Missing return spec."
:type :clj-kondo.fulcro.>defn/invalid-gspec})))

;; (| arg-preds+)?
(when (and | (empty? arg-preds))
(api/reg-finding! (merge (meta |)
{:message "Missing argument predicates after |."
:type :clj-kondo.fulcro.>defn/invalid-gspec})))


(let [len-argv (count (remove #{'&} (api/sexpr argv))) ; [a & more] => 2 arguments
arg-difference (- (count arg-specs) len-argv)]
(when (not (zero? arg-difference))
(let [too-many-specs? (pos? arg-difference)]
(api/reg-finding! (merge
(meta (if too-many-specs?
(nth arg-specs (+ len-argv arg-difference -1)) ; first excess spec
gspec)) ; The gspec is wrong, not the surplus argument.
{:message (str "Guardrail spec does not match function signature. "
"Too " (if too-many-specs? "many" "few") " specs.")
:type :clj-kondo.fulcro.>defn/invalid-gspec})))))))
new-nodes))

(defn >defn
[{:keys [node]}]
(let [args (rest (:children node))
fn-name (first args)
?docstring (when (some-> (second args) api/sexpr string?)
(second args))
args (if ?docstring
(nnext args)
(next args))
post-docs (if (every? #(-> % api/sexpr list?) args)
(mapv #(-> % :children args+gspec+body api/list-node) args)
(args+gspec+body args))
post-name (if ?docstring
(list* ?docstring post-docs)
post-docs)
new-node (api/list-node
(list*
(api/token-node 'defn)
fn-name
post-name))]
{:node new-node}))
6 changes: 6 additions & 0 deletions .clj-kondo/com.fulcrologic/guardrails/config.edn
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
{:hooks {:analyze-call {com.fulcrologic.guardrails.core/>defn
com.fulcrologic.guardrails.clj-kondo-hooks/>defn
com.fulcrologic.guardrails.core/>defn-
com.fulcrologic.guardrails.clj-kondo-hooks/>defn}}
:linters {:clj-kondo.fulcro.>defn/invalid-gspec {:level :error}}
:lint-as {com.fulcrologic.guardrails.core/>def clojure.spec.alpha/def}}
4 changes: 3 additions & 1 deletion .clj-kondo/com.wsscode/pathom3/config.edn
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
{:lint-as {com.wsscode.pathom3.connect.operation/defmutation clojure.core/defn
com.wsscode.pathom3.connect.operation/defresolver clojure.core/defn
com.wsscode.pathom3.plugin/defplugin clojure.core/def}}
com.wsscode.pathom3.plugin/defplugin clojure.core/def
com.wsscode.promesa.macros/clet clojure.core/let
com.wsscode.promesa.macros/ctry clojure.core/try}}
6 changes: 1 addition & 5 deletions .clj-kondo/config.edn
Original file line number Diff line number Diff line change
@@ -1,10 +1,6 @@
{:config-paths ["com.wsscode/pathom3"]
:lint-as {clojure.test.check.clojure-test/defspec clojure.core/def
clojure.test.check.properties/for-all clojure.core/let
promesa.core/let clojure.core/let
promesa.core/loop clojure.core/loop
com.fulcrologic.guardrails.core/>def clojure.spec.alpha/def
com.fulcrologic.guardrails.core/>defn clojure.core/defn}
clojure.test.check.properties/for-all clojure.core/let}
:linters {:invalid-arity {:level :error}
:missing-else-branch {:level :off}
:unresolved-symbol {:exclude [match? thrown-match?]}
Expand Down
8 changes: 8 additions & 0 deletions .clj-kondo/funcool/promesa/config.edn
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
{:lint-as {promesa.core/-> clojure.core/->
promesa.core/->> clojure.core/->>
promesa.core/as-> clojure.core/as->
promesa.core/let clojure.core/let
promesa.core/plet clojure.core/let
promesa.core/loop clojure.core/loop
promesa.core/recur clojure.core/recur
promesa.core/with-redefs clojure.core/with-redefs}}
5 changes: 5 additions & 0 deletions .clj-kondo/rewrite-clj/rewrite-clj/config.edn
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
{:lint-as
{rewrite-clj.zip/subedit-> clojure.core/->
rewrite-clj.zip/subedit->> clojure.core/->>
rewrite-clj.zip/edit-> clojure.core/->
rewrite-clj.zip/edit->> clojure.core/->>}}
2 changes: 1 addition & 1 deletion deps.edn
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
["src/main"]

:deps
{com.wsscode/pathom3 {:git/url "https://github.com/wilkerlucio/pathom3" :sha "00813aa7fb2d2d31c6734f2d26310000d55cde48"}
{com.wsscode/pathom3 {:mvn/version "2022.04.20-alpha"}
juxt/crux-core {:mvn/version "21.06-1.17.1-beta"}}

:aliases
Expand Down
5 changes: 3 additions & 2 deletions src/main/com/wsscode/pathom3/connect/datomic.clj
Original file line number Diff line number Diff line change
Expand Up @@ -125,7 +125,8 @@
{::keys [db]
:as env}
input]
(let [id (pick-ident-key config input)
(let [input (or (get input (datomic-entity-attribute config)) input)
id (pick-ident-key config input)
foreign-ast (-> env ::pcp/node ::pcp/foreign-ast)]
(cond
(nil? id) nil
Expand Down Expand Up @@ -155,7 +156,7 @@
[{::keys [db] ::pcp/keys [node graph] :as env} {:keys [::pco/op-name] ::p.attr/keys [attribute]} query]
(let [attr (or attribute (-> node ::pcp/expects ffirst))
ast (get-in graph [::pcp/index-ast attr])
sub-ast (-> (pcp/compute-dynamic-resolver-nested-requirements
sub-ast (-> (pcp/compute-dynamic-nested-requirements
(assoc env :edn-query-language.ast/node ast
::pco/dynamic-name op-name
::p.attr/attribute attr
Expand Down

0 comments on commit 9dfa583

Please sign in to comment.