From 7bc909859932ef4aefa31f0c83f1e71f05765026 Mon Sep 17 00:00:00 2001 From: Cam Saul Date: Fri, 22 May 2026 15:47:43 -0700 Subject: [PATCH] Fix parsing ns requires with reader conditionals --- cljfmt/src/cljfmt/core.cljc | 5 ++- cljfmt/test/cljfmt/core_test.cljc | 51 ++++++++++++++++++++++++++++++- 2 files changed, 54 insertions(+), 2 deletions(-) diff --git a/cljfmt/src/cljfmt/core.cljc b/cljfmt/src/cljfmt/core.cljc index 75151940..8cb32bbb 100644 --- a/cljfmt/src/cljfmt/core.cljc +++ b/cljfmt/src/cljfmt/core.cljc @@ -820,7 +820,10 @@ (when-not (ns-require-form? grandparent-node) (when (or (z/vector? grandparent-node) (z/list? grandparent-node)) - (first (z/child-sexprs grandparent-node)))))) + (some-> (z/find (-> grandparent-node z/down skip-meta) + (comp skip-meta z/right) + symbol-node?) + z/sexpr))))) #?(:clj (defn- join-ns-str [parent-namespace current-ns] (if parent-namespace diff --git a/cljfmt/test/cljfmt/core_test.cljc b/cljfmt/test/cljfmt/core_test.cljc index a3d70d56..91c52398 100644 --- a/cljfmt/test/cljfmt/core_test.cljc +++ b/cljfmt/test/cljfmt/core_test.cljc @@ -905,7 +905,56 @@ " (method [this x]" " \"This is a docstring for a protocol method.\"))"] opts) - "only :cljs; skip metadata in front of symbol")))) + "only :cljs; skip metadata in front of symbol"))) + (testing "reader conditionals in ns (:require ...) should be handled correctly" + (let [opts '{:remove-blank-lines-in-forms? true + :extra-blank-line-forms {potemkin/defprotocol+ :all} + :extra-indents {potemkin/defprotocol+ [[:block 1] [:inner 1]]} + #?@(:cljs [:alias-map {"p" "potemkin"}])}] + (is (reformats-to? + ["(ns x" + "(:require" + "#?@(:clj [[potemkin :as p]])))" + "" + "(#?(:clj p/defprotocol+ :cljs defprotocol) Protocol" + "(my-method [_this]" + "\"This is a great method.\")" + "" + "(another-method [_this]" + "\"Another nice method.\"))"] + ["(ns x" + " (:require" + " #?@(:clj [[potemkin :as p]])))" + "" + "(#?(:clj p/defprotocol+ :cljs defprotocol) Protocol" + " (my-method [_this]" + " \"This is a great method.\")" + "" + " (another-method [_this]" + " \"Another nice method.\"))"] + opts)) + (is (reformats-to? + ["(ns x" + "(:require" + "#?(:clj [potemkin :as p])))" + "" + "(#?(:clj p/defprotocol+ :cljs defprotocol) Protocol" + "(my-method [_this]" + "\"This is a great method.\")" + "" + "(another-method [_this]" + "\"Another nice method.\"))"] + ["(ns x" + " (:require" + " #?(:clj [potemkin :as p])))" + "" + "(#?(:clj p/defprotocol+ :cljs defprotocol) Protocol" + " (my-method [_this]" + " \"This is a great method.\")" + "" + " (another-method [_this]" + " \"Another nice method.\"))"] + opts))))) (testing "namespaced maps" (is (reformats-to?