Skip to content

Commit 9fcdf9e

Browse files
committed
A map qualifier is now sexpr-able
Returns the symbol that you'd expect from the auto-resolve fn. Logically: - :: symbol of current ns - ::my-alias symbol of namespaced found by looking up my-alias - :prefix 'prefix Closes #119
1 parent 2f9aad0 commit 9fcdf9e

File tree

2 files changed

+25
-3
lines changed

2 files changed

+25
-3
lines changed

src/rewrite_clj/node/namespaced_map.cljc

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3,14 +3,16 @@
33

44
#?(:clj (set! *warn-on-reflection* true))
55

6-
;; a map qualifier is not sexpressable on its own
76
(defrecord MapQualifierNode [auto-resolved? prefix]
87
node/Node
98
(tag [_node] :map-qualifier)
109
(node-type [_node] :map-qualifier)
1110
(printable-only? [_node] true)
12-
(sexpr* [_node _opts]
13-
(throw (ex-info "unsupported operation" {})))
11+
(sexpr* [_node opts]
12+
(if auto-resolved?
13+
((or (:auto-resolve opts) node/default-auto-resolve)
14+
(if prefix (symbol prefix) :current))
15+
(symbol prefix)))
1416
(length [_node]
1517
(+ 1 ;; for first :
1618
(if auto-resolved? 1 0) ;; for extra :

test/rewrite_clj/node_test.cljc

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -175,3 +175,23 @@
175175
(-> (n/keyword-node :foo/my-kw false)
176176
(assoc :map-qualifier {:auto-resolved? true :prefix "nsmap-alias"})
177177
(n/sexpr opts)))) ))))
178+
179+
(deftest t-sexpr-on-map-qualifier-node
180+
(testing "with default auto-resolve"
181+
(let [default-mqn-sexpr (fn [s] (-> s p/parse-string n/children first n/sexpr))]
182+
(is (= 'prefix (default-mqn-sexpr "#:prefix {:a 1 :b 2}")))
183+
(is (= '?_current-ns_? (default-mqn-sexpr "#:: {:a 1 :b 2}")))
184+
(is (= '??_my-ns-alias_?? (default-mqn-sexpr "#::my-ns-alias {:a 1 :b 2}")))))
185+
(testing "with custom auto-resolve"
186+
(let [opts {:auto-resolve (fn [alias]
187+
(if (= :current alias)
188+
'my.current.ns
189+
(get {'my-alias 'my.aliased.ns
190+
'nsmap-alias 'nsmap.aliased.ns}
191+
alias
192+
(symbol (str alias "-unresolved")))))}
193+
custom-mqn-sexpr (fn [s] (-> s p/parse-string n/children first (n/sexpr opts)))]
194+
(is (= 'prefix (custom-mqn-sexpr "#:prefix {:a 1 :b 2}")))
195+
(is (= 'my.current.ns (custom-mqn-sexpr "#:: {:a 1 :b 2}")))
196+
(is (= 'my.aliased.ns (custom-mqn-sexpr "#::my-alias {:a 1 :b 2}")))
197+
(is (= 'my-alias-nope-unresolved (custom-mqn-sexpr "#::my-alias-nope {:a 1 :b 2}"))))))

0 commit comments

Comments
 (0)