File tree Expand file tree Collapse file tree 2 files changed +25
-3
lines changed Expand file tree Collapse file tree 2 files changed +25
-3
lines changed Original file line number Diff line number Diff line change 3
3
4
4
#?(:clj (set! *warn-on-reflection* true ))
5
5
6
- ; ; a map qualifier is not sexpressable on its own
7
6
(defrecord MapQualifierNode [auto-resolved? prefix]
8
7
node /Node
9
8
(tag [_node] :map-qualifier )
10
9
(node-type [_node] :map-qualifier )
11
10
(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)))
14
16
(length [_node]
15
17
(+ 1 ; ; for first :
16
18
(if auto-resolved? 1 0 ) ; ; for extra :
Original file line number Diff line number Diff line change 175
175
(-> (n/keyword-node :foo/my-kw false )
176
176
(assoc :map-qualifier {:auto-resolved? true :prefix " nsmap-alias" })
177
177
(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}" ))))))
You can’t perform that action at this time.
0 commit comments