Skip to content

Commit eba3d6c

Browse files
committed
infer more
1 parent 62a12a3 commit eba3d6c

File tree

2 files changed

+57
-8
lines changed

2 files changed

+57
-8
lines changed

src/compojure/api/meta.clj

+57-4
Original file line numberDiff line numberDiff line change
@@ -672,6 +672,9 @@
672672

673673
(declare static-body?)
674674

675+
(defn- static-form? [&env form]
676+
(static-body? &env [form]))
677+
675678
(defn- static-endpoint? [&env form]
676679
(and (seq? form)
677680
(boolean
@@ -785,21 +788,71 @@
785788
(when (symbol? sym)
786789
(let [v (resolve &env sym)]
787790
(when (or (= #'when v)
791+
(= #'cond v)
788792
(= #'= v)
789793
(= #'not= v)
794+
(= #'boolean v)
790795
(= sym 'if))
791796
(static-body? &env (next form)))))))))
792797

798+
(defn- var-form? [&env form]
799+
(boolean
800+
(or (and (seq? form)
801+
(= 2 (count form))
802+
(= 'var (first form)))
803+
(when (symbol? form)
804+
(var? (resolve &env form))))))
805+
806+
(defn- static-expansion? [&env form]
807+
(boolean
808+
(when (and (seq? form)
809+
(symbol? (first form))
810+
(not (contains? &env (first form))))
811+
(let [form' (macroexpand-1 form)]
812+
(when-not (identical? form' form)
813+
(static-form? &env form'))))))
814+
815+
(defn- constant-form? [&env form]
816+
(or ((some-fn nil? keyword? number? boolean?) form)
817+
(and (seq? form)
818+
(= 2 (count form))
819+
(= 'quote (first form)))))
820+
821+
(defn- static-binder? [&env bv]
822+
(and (vector? bv)
823+
(even? (count bv))
824+
(every? (fn [[_ init]]
825+
(static-body? &env init))
826+
(partition 2 bv))))
827+
828+
(defn- static-let? [&env body]
829+
(and (seq? body)
830+
(symbol? (first body))
831+
(or (= 'let* (first body))
832+
(let [v (resolve &env (first body))]
833+
(when (var? v)
834+
(contains?
835+
'#{clojure.core/let compojure.api.sweet/let-routes compojure.api.core/let-routes}
836+
(symbol v)))))
837+
(let [[_ bv & body] body]
838+
(and (static-binder? &env bv)
839+
(static-body? &env body)))))
840+
841+
(defn- static-vector? [&env body]
842+
(and (vector? body)
843+
(every? #(static-body? &env %) body)))
844+
793845
(defn- static-body? [&env body]
794846
(every? #(or (static-endpoint? &env %)
795847
(contains? &env %) ;;local
796-
(when (symbol? %)
797-
(var? (resolve &env %))) ;;var deref
798-
((some-fn keyword? number? boolean?) %)
848+
(var-form? &env %)
849+
(constant-form? &env %)
850+
(static-let? &env %)
799851
(static-cond? &env %)
800852
(static-context? &env %)
801853
(static-middleware? &env %)
802-
(static-route-middleware? &env %))
854+
(static-route-middleware? &env %)
855+
(static-expansion? &env %))
803856
body))
804857

805858
(defn restructure [method [path route-arg & args] {:keys [context? &form &env]}]

test/compojure/api/swagger_test.clj

-4
Original file line numberDiff line numberDiff line change
@@ -13,10 +13,8 @@
1313

1414
(fact "all compojure.api.core macros are interpreted"
1515
(let [app (context "/a" []
16-
:static true ;;FIXME nested static/dynamic?
1716
(routes
1817
(context "/b" []
19-
:dynamic true
2018
(let-routes []
2119
(GET "/c" [] identity)
2220
(POST "/d" [] identity)
@@ -50,14 +48,12 @@
5048
(fact "route-macros are expanded"
5149
(extract-paths
5250
(context "/api" []
53-
:static true
5451
(optional-routes true (GET "/true" [] identity))
5552
(optional-routes false (PUT "/false" [] identity)))) => {"/api/true" {:get {}}})
5653

5754
(fact "endpoint-macros are expanded"
5855
(extract-paths
5956
(context "/api" []
60-
:static true
6157
(GET+ "/true" [] identity))) => {"/api/xxx/true" {:get {}}})
6258

6359
(fact "Vanilla Compojure defroutes are NOT followed"

0 commit comments

Comments
 (0)