nha
nha

Reputation: 18005

ClojureScript advanced compilation of pre and post conditions

I have the following piece of code in a ClojureScript project :

(ns project.lib
  (:require [cljs.test :refer-macros [is]]))

(defn my-fn [p]
  {:pre  [(is (#{:allowed-key :another-allowed-key} p))]}
  ;;...
  )

I would like to know if I can control the behaviour of the :pre and :post assertions, and generally what is the way to make sure that some code related to parameter checking is not included.

Note : I am aware of the :closure-define compiler option, but not sure how to apply it to this specific case.

Upvotes: 0

Views: 127

Answers (1)

Mike Fikes
Mike Fikes

Reputation: 3527

You can set the compiler option :elide-asserts to true to eliminate all assertions, including :pre and :post assertions.

This flag is independent of :advanced and needs to be set even under that mode to eliminate the assertions from production code.

See https://github.com/clojure/clojurescript/wiki/Compiler-Options#elide-asserts

Also note that, generally, the cljs.test namespace would only be used in unit test namespaces, which would be placed in a separate directory (perhaps under "test" as opposed to to "src") and, if using lein, you would use :source-paths so as to not include the tests in your production builds.

Having said that, using :pre and :post is perfectly fine in production code—just use "regular" predicates instead of the cljs.test is macro. For your specific example, is could be eliminated as the precondition simply needs to evaluate to something truthy.

Upvotes: 1

Related Questions