Skip to content

Commit

Permalink
defn: prevent type hints in wrong positions
Browse files Browse the repository at this point in the history
  • Loading branch information
vemv committed May 13, 2019
1 parent 6339f75 commit 9aad01e
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 1 deletion.
5 changes: 5 additions & 0 deletions src/nedap/utils/spec/impl/defn.cljc
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,11 @@
tail (if (-> tail first list?)
tail
(list tail))]
(assert (->> tail
(map meta)
(mapcat keys)
(not-any? #{:tag}))
":tag metadata placed in a wrong position.")
{:tails (fntails name tail)
:docstring-and-meta docstring-and-meta}))

Expand Down
17 changes: 16 additions & 1 deletion test/unit/nedap/utils/speced/defn.cljc
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
In practice, that is completely optional and you are free to use 2, 1, or 0 return value hints in any position of your choice."
(:require
#?(:clj [clojure.spec.alpha :as spec] :cljs [cljs.spec.alpha :as spec])
[clojure.string :as string]
#?(:clj [clojure.test :refer [deftest testing are is use-fixtures]] :cljs [cljs.test :refer-macros [deftest testing is are] :refer [use-fixtures]])
[nedap.utils.speced :as sut]
[unit.nedap.test-helpers :refer [every-and-at-least-one?]]))
Expand Down Expand Up @@ -489,4 +490,18 @@
#'type-hinted-metadata-n
#'type-hinted-metadata-n-alt
#'inline-function-n
#'inline-function-n-alt))))]))
#'inline-function-n-alt))

(testing ":tag metadata placed in wrong positions is guarded against"
(are [desc input] (testing desc
(testing input
(try
(eval input)
false
(catch Exception e
true))))

"Hinting an arity"
'(sut/defn faulty
^String ([x]
(-> x (* x) str)))))))]))

0 comments on commit 9aad01e

Please sign in to comment.