Skip to content

Commit

Permalink
Macroexpand (.foo bar) form
Browse files Browse the repository at this point in the history
  • Loading branch information
borkdude committed Oct 26, 2023
1 parent 8da8f44 commit 0f24b4b
Show file tree
Hide file tree
Showing 3 changed files with 18 additions and 3 deletions.
4 changes: 3 additions & 1 deletion src/sci/impl/analyzer.cljc
Original file line number Diff line number Diff line change
Expand Up @@ -86,7 +86,9 @@
(if needs-ctx?
(apply f original-expr (:bindings ctx) ctx (rest expr))
(apply f original-expr (:bindings ctx) (rest expr)))
expr)))))
(if (str/starts-with? (str op) ".")
(list* '. (second expr) (symbol (subs (str op) 1)) (nnext expr))
expr))))))
expr))
expr)))

Expand Down
9 changes: 8 additions & 1 deletion src/sci/impl/load.cljc
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,7 @@

(defn handle-require-libspec-env
[_ctx env current-ns the-loaded-ns lib-name
{:keys [:as :refer :rename :exclude :only :use] :as #?(:clj _opts :cljs opts)}]
{:keys [:as :refer #?(:cljs :refer-macros) :rename :exclude :only :use] :as #?(:clj _opts :cljs opts)}]
(or
#?(:cljs
(when (string? lib-name)
Expand All @@ -72,6 +72,13 @@
(fn [sym]
(not (contains? excludes sym))))
(constantly true))
#?@(:cljs [refer (if refer-macros
(if (or (nil? refer)
(coll? refer))
(into refer refer-macros)
;; assume :all
refer)
refer)])
the-current-ns
(cond refer
(cond (or (kw-identical? :all refer)
Expand Down
8 changes: 7 additions & 1 deletion test/sci/core_test.cljc
Original file line number Diff line number Diff line change
Expand Up @@ -1083,7 +1083,11 @@
(first (eval* "(macroexpand-1 '(for [x [1 2 3]] x))"))))
(is (= '(user/bar 1) (eval* "(defmacro foo [x] `(bar ~x)) (defmacro bar [x] x) (macroexpand-1 '(foo 1))")))
(is (= '(foobar) (eval* "(defmacro foo [] '(foobar)) (macroexpand '(foo))")))
(is (= '(clojure.core/defrecord Foo []) (eval* "(macroexpand '(defrecord Foo []))"))))
(is (= '(clojure.core/defrecord Foo []) (eval* "(macroexpand '(defrecord Foo []))")))
(is (= '(. nil log) (eval* "(macroexpand-1 '(.log))")))
(is (= '(. js/console log) (eval* "(macroexpand-1 '(.log js/console))")))
(is (= '(. js/console log 1 2 3) (eval* "(macroexpand-1 '(.log js/console 1 2 3))")))
)

(deftest macroexpand-call-test
(is (= [1 1] (eval* "(defmacro foo [x] `(bar ~x)) (defmacro bar [x] [x x]) (macroexpand '(foo 1))")))
Expand All @@ -1104,6 +1108,8 @@
(do (is (= 1 (tu/eval* "(ns foo (:require-macros [foo :refer [my-macro]]))
(defmacro my-macro [x] x) (my-macro 1)" {})))
(is (= 1 (tu/eval* "(ns foo (:require-macros [bar :refer [my-macro]])) (my-macro 1)"
{:load-fn (constantly {:source "(ns bar) (defmacro my-macro [x] x)"})})))
(is (= 1 (tu/eval* "(ns foo (:require [bar :refer-macros [my-macro]])) (my-macro 1)"
{:load-fn (constantly {:source "(ns bar) (defmacro my-macro [x] x)"})})))))))

(deftest reload-test
Expand Down

0 comments on commit 0f24b4b

Please sign in to comment.