diff --git a/src/refactor_nrepl/ns/clean_ns.clj b/src/refactor_nrepl/ns/clean_ns.clj index 6a62fa19..129d724a 100644 --- a/src/refactor_nrepl/ns/clean_ns.clj +++ b/src/refactor_nrepl/ns/clean_ns.clj @@ -20,7 +20,8 @@ [ns-parser :as ns-parser] [prune-dependencies :refer [prune-dependencies]] [rebuild :refer [rebuild-ns-form]]] - [clojure.string :as str])) + [clojure.string :as str] + [clojure.java.io :as io])) (defn- assert-no-exclude-clause [use-form] @@ -35,20 +36,23 @@ (assert-no-exclude-clause (core/get-ns-component ns-form :use)) ns-form) -(defn clean-ns [{:keys [path]}] - {:pre [(seq path) (string? path) (core/source-file? path)]} - ;; Prefix notation not supported in cljs. - ;; We also turn it off for cljc for reasons of symmetry - (config/with-config {:prefix-rewriting (if (or (core/cljs-file? path) - (core/cljc-file? path)) - false - (:prefix-rewriting config/*config*))} - (let [ns-form (validate (core/read-ns-form-with-meta path)) - deps-preprocessor (if (get config/*config* :prune-ns-form) - #(prune-dependencies % path) - identity) - new-ns-form (-> (ns-parser/parse-ns path) - deps-preprocessor - (rebuild-ns-form ns-form))] - (when-not (= ns-form new-ns-form) - new-ns-form)))) +(defn clean-ns [{:keys [path relative-path]}] + {:pre [(seq path) (string? path)]} + ;; Try first the absolute, then the relative path + (let [path (first (filter #(some-> % io/file .exists) [path relative-path]))] + (assert (core/source-file? path)) + ;; Prefix notation not supported in cljs. + ;; We also turn it off for cljc for reasons of symmetry + (config/with-config {:prefix-rewriting (if (or (core/cljs-file? path) + (core/cljc-file? path)) + false + (:prefix-rewriting config/*config*))} + (let [ns-form (validate (core/read-ns-form-with-meta path)) + deps-preprocessor (if (get config/*config* :prune-ns-form) + #(prune-dependencies % path) + identity) + new-ns-form (-> (ns-parser/parse-ns path) + deps-preprocessor + (rebuild-ns-form ns-form))] + (when-not (= ns-form new-ns-form) + new-ns-form))))) diff --git a/src/refactor_nrepl/ns/pprint.clj b/src/refactor_nrepl/ns/pprint.clj index 1bcdce0e..14f47188 100644 --- a/src/refactor_nrepl/ns/pprint.clj +++ b/src/refactor_nrepl/ns/pprint.clj @@ -156,6 +156,6 @@ (form-is? form :import) (pprint-import-form form) :else (pprint form)))) forms))) - (.replaceAll "\r" "") + (str/replace "\r" "") fmt/reformat-string - (.replaceAll (Pattern/quote "#? @") "#?@")))) + (str/replace (Pattern/quote "#? @") "#?@")))) diff --git a/test/refactor_nrepl/ns/clean_ns_test.clj b/test/refactor_nrepl/ns/clean_ns_test.clj index dd7d4e33..b16b4e17 100644 --- a/test/refactor_nrepl/ns/clean_ns_test.clj +++ b/test/refactor_nrepl/ns/clean_ns_test.clj @@ -3,14 +3,16 @@ [refactor-nrepl.config :as config] [refactor-nrepl.core :as core] [refactor-nrepl.ns.clean-ns :refer [clean-ns]] - [refactor-nrepl.ns.pprint :refer [pprint-ns]]) + [refactor-nrepl.ns.pprint :refer [pprint-ns]] + [clojure.string :as str]) (:import java.io.File)) (defn- absolute-path [^String path] (.getAbsolutePath (File. path))) (defn- clean-msg [path] - {:path (absolute-path path)}) + {:path (absolute-path path) + :relative-path path}) (def ns1 (clean-msg "test/resources/ns1.clj")) (def ns1-cleaned (core/read-ns-form-with-meta (absolute-path "test/resources/ns1_cleaned.clj"))) @@ -48,6 +50,9 @@ (def ns-using-dollar (clean-msg "test/resources/ns_using_dollar.clj")) +(def ns1-relative-path {:path "I do not exist.clj" + :relative-path "test/resources/ns1.clj"}) + (deftest combines-requires (let [requires (core/get-ns-component (clean-ns ns2) :require) combined-requires (core/get-ns-component ns2-cleaned :require)] @@ -55,7 +60,7 @@ (deftest meta-preserved (let [cleaned (pprint-ns (clean-ns ns2-meta))] - (is (.contains cleaned "^{:author \"Trurl and Klapaucius\" + (is (str/includes? cleaned "^{:author \"Trurl and Klapaucius\" :doc \"test ns with meta\"}")))) (deftest rewrites-use-to-require @@ -224,3 +229,7 @@ (deftest does-not-break-import-for-inner-class (let [cleaned (pprint-ns (clean-ns ns-with-inner-classes))] (is (re-find #":import.*Line2D\$Double" cleaned)))) + +(deftest fallback-to-relative-path + (is (= (pprint-ns (clean-ns ns1)) + (pprint-ns (clean-ns ns1-relative-path)))))