-
-
Notifications
You must be signed in to change notification settings - Fork 6
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Fix wrong call to planner and improvements on linting
- Loading branch information
1 parent
bad83ee
commit 9dfa583
Showing
9 changed files
with
103 additions
and
9 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
{:lint-as {babashka.fs/with-temp-dir clojure.core/let}} |
75 changes: 75 additions & 0 deletions
75
.clj-kondo/com.fulcrologic/guardrails/com/fulcrologic/guardrails/clj_kondo_hooks.clj
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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})) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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}} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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}} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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}} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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/->>}} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters