(defn resolve-macro-var
"Given env, an analysis environment, and sym, a symbol, resolve a macro."
[env sym]
(let [ns (-> env :ns :name)
namespaces (get @env/*compiler* ::namespaces)]
(cond
(some? (namespace sym))
(let [ns (namespace sym)
ns (if (= "clojure.core" ns) "cljs.core" ns)
full-ns (resolve-macro-ns-alias env ns)
#?@(:cljs [full-ns (if-not (string/ends-with? (str full-ns) "$macros")
(symbol (str full-ns "$macros"))
full-ns)])]
#?(:clj (get-in namespaces [full-ns :macros (symbol (name sym))])
:cljs (get-in namespaces [full-ns :defs (symbol (name sym))])))
(some? (get-in namespaces [ns :use-macros sym]))
(let [full-ns (get-in namespaces [ns :use-macros sym])]
(get-in namespaces [full-ns :macros sym]))
(some? (get-in namespaces [ns :rename-macros sym]))
(let [qualified-symbol (get-in namespaces [ns :rename-macros sym])
full-ns (symbol (namespace qualified-symbol))
sym (symbol (name qualified-symbol))]
(get-in namespaces [full-ns :macros sym]))
:else
(let [ns (cond
(some? (get-in namespaces [ns :macros sym])) ns
(core-name? env sym) #?(:clj 'cljs.core
:cljs CLJS_CORE_MACROS_SYM))]
(when (some? ns)
#?(:clj (get-in namespaces [ns :macros sym])
:cljs (get-in namespaces [ns :defs sym])))))))