Skip to content

Commit 7734fe2

Browse files
committed
Don't rewrite ~ @foo to ~@foo
The problem is ~ @foo is being reformatted to ~@foo, a different expression. We fix this by not treating spaces between ~ and @ as "surrounding spaces".
1 parent 8d8e26d commit 7734fe2

File tree

2 files changed

+52
-9
lines changed

2 files changed

+52
-9
lines changed

cljfmt/src/cljfmt/core.cljc

+15-5
Original file line numberDiff line numberDiff line change
@@ -33,10 +33,6 @@
3333
(defn- transform [form zf & args]
3434
(z/root (apply zf (z/of-node form) args)))
3535

36-
(defn- surrounding? [zloc p?]
37-
(and (p? zloc) (or (nil? (z/left* zloc))
38-
(nil? (z/skip z/right* p? zloc)))))
39-
4036
(defn root? [zloc]
4137
(nil? (z/up* zloc)))
4238

@@ -49,9 +45,23 @@
4945
(defn- clojure-whitespace? [zloc]
5046
(z/whitespace? zloc))
5147

48+
(defn- unquote? [zloc]
49+
(and zloc (= (n/tag (z/node zloc)) :unquote)))
50+
51+
(defn- deref? [zloc]
52+
(and zloc (= (n/tag (z/node zloc)) :deref)))
53+
54+
(defn- unquote-deref? [zloc]
55+
(and (deref? zloc)
56+
(unquote? (z/up* zloc))))
57+
5258
(defn- surrounding-whitespace? [zloc]
5359
(and (not (top? zloc))
54-
(surrounding? zloc clojure-whitespace?)))
60+
(clojure-whitespace? zloc)
61+
(or (and (nil? (z/left* zloc))
62+
;; don't convert ~ @ to ~@
63+
(not (unquote-deref? (z/right* zloc))))
64+
(nil? (z/skip z/right* clojure-whitespace? zloc)))))
5565

5666
(defn remove-surrounding-whitespace [form]
5767
(transform form edit-all surrounding-whitespace? z/remove*))

cljfmt/test/cljfmt/core_test.cljc

+37-4
Original file line numberDiff line numberDiff line change
@@ -692,7 +692,34 @@
692692
["#:clj {:a :b"
693693
":c :d}"]
694694
["#:clj {:a :b"
695-
" :c :d}"]))))
695+
" :c :d}"])))
696+
(testing "~ @ is not ~@"
697+
(is (reformats-to?
698+
["~ @foo"]
699+
["~ @foo"]))
700+
(is (reformats-to?
701+
["~(deref foo)"]
702+
["~(deref foo)"]))
703+
(is (reformats-to?
704+
["~(clojure.core/deref foo)"]
705+
["~(clojure.core/deref foo)"]))
706+
(is (reformats-to?
707+
["~ @foo"]
708+
["~ @foo"]))
709+
(is (reformats-to?
710+
["~ @foo"]
711+
["~ @foo"]))
712+
(is (reformats-to?
713+
["~\n@foo"]
714+
["~"
715+
" @foo"]))
716+
(is (reformats-to?
717+
["~;;comment\n@foo"]
718+
["~;;comment"
719+
" @foo"]))
720+
(is (reformats-to?
721+
["~#_a@foo"]
722+
["~#_a @foo"]))))
696723

697724
(deftest test-remove-multiple-non-indenting-spaces
698725
(let [opts {:remove-multiple-non-indenting-spaces? true}]
@@ -1568,7 +1595,9 @@
15681595
"(~@foo"
15691596
"bar)"
15701597
"(#:foo{:bar 1}"
1571-
"baz)"]]
1598+
"baz)"
1599+
"(~ @foo"
1600+
"bar)"]]
15721601
(testing ":cursive style uses 2 spaces unless starting with a collection"
15731602
(is (reformats-to?
15741603
input
@@ -1627,7 +1656,9 @@
16271656
"(~@foo"
16281657
" bar)"
16291658
"(#:foo{:bar 1}"
1630-
" baz)"]
1659+
" baz)"
1660+
"(~ @foo"
1661+
" bar)"]
16311662
{:function-arguments-indentation :cursive})))
16321663
(testing ":zprint uses 2 spaces if starting with a symbol, keyword, or list"
16331664
(is (reformats-to?
@@ -1687,5 +1718,7 @@
16871718
"(~@foo"
16881719
" bar)"
16891720
"(#:foo{:bar 1}"
1690-
" baz)"]
1721+
" baz)"
1722+
"(~ @foo"
1723+
" bar)"]
16911724
{:function-arguments-indentation :zprint})))))

0 commit comments

Comments
 (0)