Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

clean-ns: Introduce ^:keep metadata on libspecs #189

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
16 changes: 10 additions & 6 deletions src/refactor_nrepl/core.clj
Original file line number Diff line number Diff line change
Expand Up @@ -245,12 +245,16 @@
metadata which shouldn't be printed back out."
[file-content]
(let [ns-string (sexp/get-first-sexp file-content)
meta? (-> ns-string (.replaceFirst "\\^\\{" "\\{")
(StringReader.)
(PushbackReader.)
parse/read-ns-decl
second)
shorthand-meta? (second (re-find #"\^:([^\s]+)\s" ns-string))]
ns-decl (-> ns-string
(.replaceFirst "\\^\\{" "\\{")
(StringReader.)
(PushbackReader.)
parse/read-ns-decl)
meta? (second ns-decl)
ns-name (if (symbol? meta?) meta? (first (nnext ns-decl)))
shorthand-meta? (-> (java.util.regex.Pattern/compile (str "\\^:([^\\s]+)\\s" ns-name))
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This code was too eager, it would transform

(ns user (:require ^:keep [,,,]))

into

(ns ^:keep user)

(re-find ns-string)
second)]
(cond
(map? meta?) meta?
shorthand-meta? {::shorthand-meta (keyword shorthand-meta?)}
Expand Down
9 changes: 5 additions & 4 deletions src/refactor_nrepl/ns/ns_parser.clj
Original file line number Diff line number Diff line change
Expand Up @@ -19,10 +19,11 @@

(defn- libspec-vector->map
[libspec]
(if (vector? libspec)
(let [[ns & specs] libspec]
(into {:ns ns} (->> specs (partition 2) (map vec))))
{:ns (symbol libspec)}))
(-> (if (vector? libspec)
(let [[ns & specs] libspec]
(into {:ns ns} (->> specs (partition 2) (map vec))))
{:ns (symbol libspec)})
(with-meta (meta libspec))))

(defn- expand-prefix-specs
"Eliminate prefix lists."
Expand Down
28 changes: 15 additions & 13 deletions src/refactor_nrepl/ns/pprint.clj
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,19 @@
(printf "%s " libspec))))))
ordered-libspecs))))

(defn pprint-meta
[m]
(if-let [shorthand-meta (::core/shorthand-meta m)]
(print (str "^" shorthand-meta " "))
(printf (.replaceAll (str "^" (into (sorted-map) m) "\n")
", " "\n"))))

(defn- pprint-libspec [libspec]
(when (seq (meta libspec)) (pprint-meta (meta libspec)))
(if (prefix-form? libspec)
(pprint-prefix-form libspec)
(pprint libspec)))

(defn pprint-require-form
[[_ & libspecs]]
(print "(:require ")
Expand All @@ -40,12 +53,8 @@
(fn [idx libspec]
(if (= idx (dec (count libspecs)))
(printf "%s)\n" (str/trim-newline
(with-out-str (if (prefix-form? libspec)
(pprint-prefix-form libspec)
(pprint libspec)))))
(if (prefix-form? libspec)
(pprint-prefix-form libspec)
(pprint libspec))))
(with-out-str (pprint-libspec libspec))))
(pprint-libspec libspec)))
libspecs)))

(defn- form-is? [form type]
Expand Down Expand Up @@ -76,13 +85,6 @@
(println import)))
imports)))

(defn pprint-meta
[m]
(if-let [shorthand-meta (::core/shorthand-meta m)]
(print (str "^" shorthand-meta " "))
(printf (.replaceAll (str "^" (into (sorted-map) m) "\n")
", " "\n"))))

(defn pprint-ns
[[_ name & more :as ns-form]]
(let [docstring? (when (string? (first more)) (first more))
Expand Down
16 changes: 9 additions & 7 deletions src/refactor_nrepl/ns/prune_dependencies.clj
Original file line number Diff line number Diff line change
Expand Up @@ -67,13 +67,15 @@

(defn- remove-unused-syms-and-specs
[used-syms current-ns libspec]
(some-> libspec
(libspec-in-use? used-syms current-ns)
(prune-key :refer used-syms)
(prune-key :refer-macros used-syms)
(util/dissoc-when (fn empty-and-not-kw [k]
(and (not (keyword k)) (empty? k)))
:refer :refer-macros)))
(if (:keep (meta libspec))
libspec
(some-> libspec
(libspec-in-use? used-syms current-ns)
(prune-key :refer used-syms)
(prune-key :refer-macros used-syms)
(util/dissoc-when (fn empty-and-not-kw [k]
(and (not (keyword k)) (empty? k)))
:refer :refer-macros))))

(defn- static-method-or-field-access->Classname
[symbol-in-file]
Expand Down
30 changes: 16 additions & 14 deletions src/refactor_nrepl/ns/rebuild.clj
Original file line number Diff line number Diff line change
Expand Up @@ -119,20 +119,22 @@
[{:keys [ns as refer rename refer-macros] :as libspec}]
(let [all-flags #{:reload :reload-all :verbose :include-macros}
flags (util/filter-map #(all-flags (first %)) libspec)]
(if (and (not as) (not refer)
(empty? flags) (empty? rename) (empty? refer-macros))
ns
(into [ns]
(concat (when as [:as as])
(when refer
[:refer (if (sequential? refer)
(vec (sort-referred-symbols refer))
refer)])
(when refer-macros
[:refer-macros (vec (sort-referred-symbols refer-macros))])
(when (not-empty rename)
[:rename (into (sorted-map) (:rename libspec))])
(flatten (seq flags)))))))
(cond->
(if (and (not as) (not refer)
(empty? flags) (empty? rename) (empty? refer-macros))
ns
(into [ns]
(concat (when as [:as as])
(when refer
[:refer (if (sequential? refer)
(vec (sort-referred-symbols refer))
refer)])
(when refer-macros
[:refer-macros (vec (sort-referred-symbols refer-macros))])
(when (not-empty rename)
[:rename (into (sorted-map) (:rename libspec))])
(flatten (seq flags)))))
(-> libspec meta :keep) (with-meta {:refactor-nrepl.core/shorthand-meta :keep}))))

(defn- create-libspec-vectors-without-prefix
[libspecs]
Expand Down
8 changes: 8 additions & 0 deletions test/refactor_nrepl/ns/clean_ns_test.clj
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,9 @@

(def ns-with-shorthand-meta (clean-msg "test/resources/ns_with_shorthand_meta.clj"))

(def ns-with-keep-metadata (clean-msg "test/resources/ns_with_keep_metadata.clj"))
(def ns-with-keep-metadata-rebuilt (core/read-ns-form-with-meta (absolute-path "test/resources/ns_with_keep_metadata_rebuilt.clj")))

(deftest combines-requires
(let [requires (core/get-ns-component (clean-ns ns2) :require)
combined-requires (core/get-ns-component ns2-cleaned :require)]
Expand Down Expand Up @@ -192,3 +195,8 @@
(deftest preserves-shorthand-meta
(let [cleaned (pprint-ns (clean-ns ns-with-shorthand-meta))]
(is (re-find #"\^:automation" cleaned))))

(deftest respects-keep-metadata
(let [new-require (core/get-ns-component (clean-ns ns-with-keep-metadata) :require)
expected-require (core/get-ns-component ns-with-keep-metadata-rebuilt :require)]
(is (= expected-require new-require))))
3 changes: 3 additions & 0 deletions test/resources/ns_with_keep_metadata.clj
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
(ns resources.ns-with-keep-metadata
(:require [clojure.set]
^:keep [clojure.string]))
2 changes: 2 additions & 0 deletions test/resources/ns_with_keep_metadata_rebuilt.clj
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
(ns resources.ns-with-keep-metadata-rebuilt
(:require ^:keep clojure.string))