diff --git a/CHANGELOG.md b/CHANGELOG.md index a4eac92e..bd4f2656 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -3,9 +3,9 @@ [Squint](https://github.com/squint-cljs/squint): Light-weight ClojureScript dialect ## Unreleased - - [#779](https://github.com/squint-cljs/squint/pull/779): Added `compare-and-swap!`, `swap-vals!` and `reset-vals!` - Multiple `:require-macros` with `:refer` now accumulate instead of overwriting +- Fix qualified symbol resolution in macro expansions when namespace has different runtime alias ## v0.9.182 diff --git a/bb/tasks.clj b/bb/tasks.clj index de5eb217..18039205 100644 --- a/bb/tasks.clj +++ b/bb/tasks.clj @@ -60,7 +60,8 @@ (assert (str/includes? output "my-other-src 1 2")) (assert (str/includes? output "json!")) (assert (str/includes? output "{ a: 1 }")) - (assert (str/includes? output "\"emit!\""))) + (assert (str/includes? output "\"emit!\"")) + (assert (str/includes? output "qualified test: 142"))) (assert (fs/exists? "test-project/lib/foo.json")) (assert (fs/exists? "test-project/lib/baz.css")) (assert (not (fs/exists? "test-project/lib/bar.json"))))) diff --git a/src/squint/compiler/node.cljs b/src/squint/compiler/node.cljs index 55cd2280..f70797e5 100644 --- a/src/squint/compiler/node.cljs +++ b/src/squint/compiler/node.cljs @@ -42,9 +42,9 @@ reload (concat [:reload]))) (let [publics (eval-form `(ns-publics '~macro-ns)) - ks (keys publics) - vs (vals publics) - vs (map deref vs) + macro-vars (filter (fn [[_ v]] (:macro (meta v))) publics) + ks (map first macro-vars) + vs (map (comp deref second) macro-vars) publics (zipmap ks vs)] publics)))] (.then macros diff --git a/src/squint/compiler_common.cljc b/src/squint/compiler_common.cljc index 3236eac0..d31b9e17 100644 --- a/src/squint/compiler_common.cljc +++ b/src/squint/compiler_common.cljc @@ -331,6 +331,11 @@ (str "globalThis." (munge *cljs-ns*) ".")) (alias-munge sym-ns) "." (munged-name sn))) + (when-let [alias (get (:libname->alias current-ns) (str sym-ns))] + (str (when *repl* + (str "globalThis." (munge *cljs-ns*) ".")) + (alias-munge alias) "." + (munged-name sn))) (let [ns (namespace expr) munged (munge ns) nm (name expr)] @@ -592,6 +597,7 @@ (when-not (or (= 'squint.core libname) (= 'cherry.core libname)) (let [env (expr-env env) + original-libname libname libname (resolve-ns env libname) [libname suffix] (str/split (if (string? libname) libname (str libname)) #"\$" 2) default? (= "default" suffix) ;; we only support a default suffix for now anyway @@ -674,8 +680,11 @@ (swap! (:ns-state env) (fn [ns-state] (let [current (:current ns-state)] - (update-in ns-state [current :aliases] (fn [aliases] - ((fnil assoc {}) aliases as libname))))))) + (-> ns-state + (update-in [current :aliases] (fn [aliases] + ((fnil assoc {}) aliases as libname))) + (update-in [current :libname->alias] (fn [m] + ((fnil assoc {}) m (str original-libname) as)))))))) (when-not (:elide-imports env) expr) #_nil))) diff --git a/test-project/src/macros.cljc b/test-project/src/macros.cljc index 8c16a3bd..8c8517a0 100644 --- a/test-project/src/macros.cljc +++ b/test-project/src/macros.cljc @@ -1,6 +1,12 @@ (ns macros #?(:cljs (:require ["node:fs" :as fs]))) +(defn add-100 [x] + (+ x 100)) + +(defmacro with-add-100 [x] + `(add-100 ~x)) + (defmacro debug [_kwd body] `(println ::debug ~body)) diff --git a/test-project/src/main.cljs b/test-project/src/main.cljs index f3ee47fc..888c1c98 100644 --- a/test-project/src/main.cljs +++ b/test-project/src/main.cljs @@ -1,7 +1,8 @@ (ns main - (:require-macros [macros :as m :refer [debug]] + (:require-macros [macros :as m :refer [debug with-add-100]] [macros2 :refer [also]]) - (:require [other-ns] + (:require [macros :as mac] + [other-ns] [my-other-src :as src] ["fs" :as fs] ["path" :as path] @@ -28,3 +29,5 @@ println) (js/console.log json) + +(println "qualified test:" (m/with-add-100 42))