Skip to content

Commit

Permalink
Fix *print-length* behaviour in formatters (#196)
Browse files Browse the repository at this point in the history
  • Loading branch information
thumbnail authored Oct 15, 2021
1 parent db6f295 commit 7824d61
Show file tree
Hide file tree
Showing 6 changed files with 51 additions and 7 deletions.
4 changes: 2 additions & 2 deletions src/formatting_stack/formatters/clean_ns.clj
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
[formatting-stack.formatters.how-to-ns]
[formatting-stack.protocols.formatter :as formatter]
[formatting-stack.protocols.linter :as linter]
[formatting-stack.util :refer [ensure-sequential process-in-parallel! try-require]]
[formatting-stack.util :refer [ensure-sequential process-in-parallel! try-require unlimited-pr-str]]
[formatting-stack.util.diff :as diff :refer [diff->line-numbers]]
[formatting-stack.util.ns :as util.ns :refer [write-ns-replacement!]]
[medley.core :refer [deep-merge]]
Expand All @@ -19,7 +19,7 @@
:original-ns-form original-ns-form
:namespaces-that-should-never-cleaned namespaces-that-should-never-cleaned
:libspec-whitelist libspec-whitelist})
pr-str)))
unlimited-pr-str)))

(def default-libspecs
["specs" "imports" "exports" "extensions" "side-effects" "init" "initialization" "load" "migration" "migrations"])
Expand Down
6 changes: 3 additions & 3 deletions src/formatting_stack/formatters/cljfmt/impl.clj
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
[clojure.tools.namespace.parse :as parse]
[formatting-stack.indent-specs]
[formatting-stack.project-parsing :refer [project-namespaces]]
[formatting-stack.util :refer [dissoc-by rcomp require-lock]]
[formatting-stack.util :refer [dissoc-by rcomp require-lock unlimited-pr-str]]
[nedap.speced.def :as speced]))

(def ^:dynamic *cache* nil)
Expand Down Expand Up @@ -99,8 +99,8 @@
(swap! result
#(-> %
(dissoc-by (fn [x] ;; regexes can't be compared, hence this contraption
(not= (pr-str x)
(pr-str #"^def"))))
(not= (unlimited-pr-str x)
(unlimited-pr-str #"^def"))))
(assoc #"^def(?!ault)(?!late)(?!er)" [[:inner 0]])))
;; :refer awareness:
(doseq [[sym var-ref] ns-mappings
Expand Down
4 changes: 2 additions & 2 deletions src/formatting_stack/formatters/trivial_ns_duplicates.clj
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
[formatting-stack.formatters.how-to-ns]
[formatting-stack.protocols.formatter :as formatter]
[formatting-stack.protocols.linter :as linter]
[formatting-stack.util :refer [ensure-coll ensure-sequential process-in-parallel! rcomp read-ns-decl]]
[formatting-stack.util :refer [ensure-coll ensure-sequential process-in-parallel! rcomp read-ns-decl unlimited-pr-str]]
[formatting-stack.util.diff :as diff :refer [diff->line-numbers]]
[formatting-stack.util.ns :as util.ns :refer [replace-ns-form! write-ns-replacement!]]
[medley.core :refer [deep-merge]]
Expand Down Expand Up @@ -136,7 +136,7 @@
replacement)))

(speced/defn ^{::speced/spec (complement #{"nil"})} duplicate-cleaner [ns-form]
(some-> ns-form remove-exact-duplicates pr-str))
(some-> ns-form remove-exact-duplicates unlimited-pr-str))

(defn replaceable-ns-form
[how-to-ns-opts filename]
Expand Down
7 changes: 7 additions & 0 deletions src/formatting_stack/util.clj
Original file line number Diff line number Diff line change
Expand Up @@ -167,3 +167,10 @@
[pred coll]
(let [switch (reductions not= true (map pred coll (rest coll)))]
(map (partial map first) (partition-by second (map list coll switch)))))

(defn unlimited-pr-str
"#'clojure.core/pr-str with no print limits"
[s]
(binding [*print-length* nil
*print-level* nil]
(pr-str s)))
21 changes: 21 additions & 0 deletions test/unit/formatting_stack/formatters/trivial_ns_duplicates.clj
Original file line number Diff line number Diff line change
Expand Up @@ -113,3 +113,24 @@

"(ns foo (:require #?(:clj foo :cljs bar) [#?(:clj foo :cljs bar)]))"
nil))

(deftest duplicate-cleaner
(let [input '(ns foo
(:require
[a :refer [a]]
[a :refer [a]]
[b :refer [b]]))
expected "(ns foo (:require [a :refer [a]] [b :refer [b]]))"]

;; override user settings
(binding [*print-length* nil
*print-level* nil]
(is (= expected (sut/duplicate-cleaner input)))

(testing "not affected by *print-length*"
(binding [*print-length* 1]
(is (= expected (sut/duplicate-cleaner input)))))

(testing "not affected by *print-level*"
(binding [*print-level* 1]
(is (= expected (sut/duplicate-cleaner input))))))))
16 changes: 16 additions & 0 deletions test/unit/formatting_stack/util.clj
Original file line number Diff line number Diff line change
Expand Up @@ -62,3 +62,19 @@
#(< 2 (- %2 %1))
'(1 2 3 8 9 10)
'((1 2 3) (8 9 10))))

(deftest unlimited-pr-str
(let [input [1 2 [3 4 [5 6]]]
expected "[1 2 [3 4 [5 6]]]"]
;; override user settings
(binding [*print-length* nil
*print-level* nil]
(is (= expected (sut/unlimited-pr-str input)))

(testing "not affected by *print-length*"
(binding [*print-length* 1]
(is (= expected (sut/unlimited-pr-str input)))))

(testing "not affected by *print-level*"
(binding [*print-level* 1]
(is (= expected (sut/unlimited-pr-str input))))))))

0 comments on commit 7824d61

Please sign in to comment.