Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Browse files

fix transform-defn-bodies for 1.2

  • Loading branch information...
commit 3e989b5c8a4eab45e95e82f4b0d1ab9711478063 1 parent 1f35eb5
@ztellman authored
View
1  src/potemkin.clj
@@ -18,6 +18,7 @@
(import-macro map/def-custom-map)
(import-fn macros/unify-gensyms)
+(import-fn macros/transform-defn-bodies)
View
21 src/potemkin/macros.clj
@@ -29,3 +29,24 @@
(symbol (str (gensym* (str (un-gensym %) "__")) "__auto__"))
%)
body)))
+
+(defn transform-defn-bodies
+ "Takes a (defn ...) form, and transform the bodies. The transform function is
+ passed the arglist, the function metadata, and the function body."
+ [f form]
+ (let [form (macroexpand form)
+ fn-form (->> form (drop 2) first)
+ fn-form (if (= ".withMeta" (str (first fn-form)))
+ (second fn-form)
+ fn-form)
+ fn-form (macroexpand fn-form)
+ fn-name (second form)
+ arity-forms (->> fn-form (drop-while symbol?))
+ arity-forms (map
+ (fn [arity-form]
+ (let [args (first arity-form)]
+ `(~args ~@(f args (rest arity-form)))))
+ arity-forms)]
+ `(~(first form)
+ ~(second form)
+ (fn* ~@arity-forms))))
View
36 test/potemkin/test/macros.clj
@@ -0,0 +1,36 @@
+;; Copyright (c) Zachary Tellman. All rights reserved.
+;; The use and distribution terms for this software are covered by the
+;; Eclipse Public License 1.0 (http://opensource.org/licenses/eclipse-1.0.php)
+;; which can be found in the file epl-v10.html at the root of this distribution.
+;; By using this software in any fashion, you are agreeing to be bound by
+;; the terms of this license.
+;; You must not remove this notice, or any other, from this software.
+
+(ns potemkin.test.macros
+ (:use
+ [clojure test]
+ [potemkin macros]))
+
+(defn simple-unify-form []
+ (unify-gensyms
+ `(let [cnt## (atom 0)]
+ ~@(map
+ (fn [_] `(swap! cnt## inc))
+ (range 100))
+ cnt##)))
+
+(deftest test-unify-form
+ (is (= 100 @(eval (simple-unify-form)))))
+
+(declare foo)
+
+(defn test-transform [form & arities]
+ (in-ns 'potemkin.test.macros)
+ (eval (transform-defn-bodies (constantly [1]) form))
+ (doseq [a arities]
+ (is (= 1 (apply foo (repeat a nil))))))
+
+(deftest test-transform-defn-bodies
+ (test-transform `(defn foo [x#]) 1)
+ (test-transform `(defn foo []))
+ (test-transform `(defn foo "doc-string" {:abc :def} ([]) ([x#]) ([x# y#]))))
Please sign in to comment.
Something went wrong with that request. Please try again.