Skip to content

Commit 6cac9f7

Browse files
committed
Fix parsing ns :require vectors with #_ comments
1 parent 389e7ca commit 6cac9f7

2 files changed

Lines changed: 45 additions & 20 deletions

File tree

cljfmt/src/cljfmt/core.cljc

Lines changed: 24 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -219,8 +219,11 @@
219219
(count)
220220
(dec)))
221221

222+
(defn- meta? [zloc]
223+
(#{:meta :meta*} (z/tag zloc)))
224+
222225
(defn- skip-meta [zloc]
223-
(if (#{:meta :meta*} (z/tag zloc))
226+
(if (meta? zloc)
224227
(-> zloc z/down z/right)
225228
zloc))
226229

@@ -829,13 +832,18 @@
829832

830833
#?(:clj
831834
(defn- refer-zloc->refer-mapping [refer-zloc]
832-
(let [refer-vec (some-> refer-zloc z/right z/sexpr)
833-
current-ns (some-> refer-zloc leftmost-symbol z/sexpr)
834-
grandparent-node (some-> refer-zloc z/up z/up)
835-
parent-ns (ns-require-form-parent grandparent-node)]
836-
(when (and (vector? refer-vec) (symbol? current-ns))
835+
(let [refers (some-> refer-zloc
836+
(z/find-next (comp skip-meta z/right)
837+
(some-fn z/vector? z/list?))
838+
z/sexpr)
839+
current-ns (some-> refer-zloc leftmost-symbol z/sexpr)
840+
grandparent-node (some-> refer-zloc
841+
(z/find-next z/up (complement meta?))
842+
(z/find-next z/up (complement meta?)))
843+
parent-ns (ns-require-form-parent grandparent-node)]
844+
(when (and (sequential? refers) (symbol? current-ns))
837845
(let [ns-str (join-ns-str parent-ns current-ns)]
838-
(->> refer-vec (map (fn [sym] [(str sym) ns-str])) (into {})))))))
846+
(->> refers (map (fn [sym] [(str sym) ns-str])) (into {})))))))
839847

840848
#?(:clj (defn- refer-map-for-form [form]
841849
(when-let [req-zloc (-> form z/of-node (z/find z/next ns-require-form?))]
@@ -845,10 +853,15 @@
845853

846854
#?(:clj
847855
(defn- as-zloc->alias-mapping [as-zloc]
848-
(let [alias (some-> as-zloc z/right z/sexpr)
849-
current-ns (some-> as-zloc leftmost-symbol z/sexpr)
850-
grandparent-node (some-> as-zloc z/up z/up)
851-
parent-ns (ns-require-form-parent grandparent-node)]
856+
(let [alias (some-> as-zloc
857+
(z/find-next (comp skip-meta z/right)
858+
symbol-node?)
859+
z/sexpr)
860+
current-ns (some-> as-zloc leftmost-symbol z/sexpr)
861+
grandparent-node (some-> as-zloc
862+
(z/find-next z/up (complement meta?))
863+
(z/find-next z/up (complement meta?)))
864+
parent-ns (ns-require-form-parent grandparent-node)]
852865
(when (and (symbol? alias) (symbol? current-ns))
853866
{(str alias) (join-ns-str parent-ns current-ns)}))))
854867

cljfmt/test/cljfmt/core_test.cljc

Lines changed: 21 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77
replace-newlines wrap-normalize-newlines
88
realign-form unalign-form]]
99
[cljfmt.test-util.common]
10+
[clojure.string :as str]
1011
[rewrite-clj.node :as n]
1112
[rewrite-clj.parser :as p])
1213
#?(:cljs (:require-macros [cljfmt.test-util.cljs])))
@@ -323,24 +324,35 @@
323324
"#_:clj-kondo/ignore "
324325
"^tag "
325326
"#_old-thing "]
326-
ns-vec-str [(str ignore-str "[thing.core :as t]")
327-
(str ignore-str "[thing [core :as t]]")
328-
(str ignore-str "(thing [core :as t])")
329-
(str "[" ignore-str "thing.core :as t]")
330-
(str ignore-str " [" ignore-str "thing.core :as t]")]
331-
:let [ns-str (str "(ns example (:require " ns-vec-str "))")]]
332-
(testing ns-str
327+
require-str ["<>[<>thing.core :as <>t :refer <>[<>my-defn]]"
328+
"<>[<>thing <>[<>core :as <>t :refer <>[<>my-defn]]]"]
329+
:let [ns-str (str "(ns my-namespace (:require " require-str "))")]
330+
ns-str [ns-str
331+
(-> ns-str
332+
(str/replace #"\[" "(")
333+
(str/replace #"\]" ")"))]
334+
:let [ns-str (str/replace ns-str #"<>" ignore-str)]]
335+
(testing (str \newline (pr-str ns-str))
333336
(is (reformats-to?
334337
[ns-str
335338
""
336339
"(t/defn foo [x]"
340+
"(+ x 1))"
341+
""
342+
"(my-defn foo [x]"
337343
"(+ x 1))"]
338344
[ns-str
339345
""
340346
"(t/defn foo [x]"
347+
" (+ x 1))"
348+
""
349+
"(my-defn foo [x]"
341350
" (+ x 1))"]
342-
{:indents {'ns [[:block 1]], 'thing.core/defn [[:inner 0]]}
343-
#?@(:cljs [:alias-map {"t" "thing.core"}])})))))
351+
{:indents {'ns [[:block 1]]
352+
'thing.core/defn [[:inner 0]]
353+
'thing.core/my-defn [[:inner 0]]}
354+
#?@(:cljs [:alias-map {"t" "thing.core"}
355+
:refer-map {"my-defn" "thing.core"}])})))))
344356
(is (reformats-to?
345357
["(comment)"
346358
"(ns thing.core)"

0 commit comments

Comments
 (0)