Skip to content

Commit e35af1d

Browse files
aibaexpez
authored andcommitted
clean-ns: group together npm deps when sorting; avoid tracking cljs files with npm deps to workaround tools.namespace bug.
add tests use a known string avoid tracking cljs files with npm string requires, as a workaround to clojure.tools.namespace bug. fix linting issue
1 parent f6e03dd commit e35af1d

File tree

5 files changed

+50
-4
lines changed

5 files changed

+50
-4
lines changed

src/refactor_nrepl/ns/rebuild.clj

+1-1
Original file line numberDiff line numberDiff line change
@@ -60,7 +60,7 @@
6060
^String [dep]
6161
(str/lower-case
6262
(if (sequential? dep)
63-
(let [name (-> dep first name)
63+
(let [name (-> dep first pr-str)
6464
;; penalize prefix forms so [foo.bar :as bar]
6565
;; comes before [foo.bar [qux :as qux] [quux ..]]
6666
suffix (if (and (> (count dep) 1) (sequential? (second dep)))

src/refactor_nrepl/ns/tracker.clj

+17-3
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,30 @@
11
(ns refactor-nrepl.ns.tracker
2-
(:require [clojure.tools.namespace
2+
(:require [clojure.java.io :as io]
3+
[clojure.tools.namespace
34
[dependency :as dep]
45
[file :as file]
56
[track :as tracker]]
6-
[refactor-nrepl.core :as core]))
7+
[refactor-nrepl.core :as core]
8+
[refactor-nrepl.ns.ns-parser :as ns-parser]))
9+
10+
;; Exclude cljs files that use npm string requires until they fix this bug:
11+
;; https://clojure.atlassian.net/projects/TNS/issues/TNS-51
12+
(defn- safe-for-clojure-tools-namespace? [f]
13+
(->> (io/file f)
14+
(.getAbsolutePath)
15+
ns-parser/parse-ns
16+
:cljs
17+
:require
18+
(map :ns)
19+
(not-any? string?)))
720

821
(defn build-tracker
922
"Build a tracker for the project.
1023
1124
If file-predicate is provided, use that instead of `core/source-file?`"
1225
([]
13-
(build-tracker core/source-file?))
26+
(build-tracker #(and (core/source-file? %)
27+
(safe-for-clojure-tools-namespace? %))))
1428
([file-predicate]
1529
(file/add-files (tracker/tracker) (core/find-in-project file-predicate))))
1630

test/refactor_nrepl/ns/clean_ns_test.clj

+13
Original file line numberDiff line numberDiff line change
@@ -53,6 +53,9 @@
5353
(def ns1-relative-path {:path "I do not exist.clj"
5454
:relative-path "test/resources/ns1.clj"})
5555

56+
(def ns-with-npm-strs (clean-msg "test/resources/ns_with_npm_strs.cljs"))
57+
(def ns-with-npm-strs-clean (clean-msg "test/resources/ns_with_npm_strs_clean.cljs"))
58+
5659
(deftest combines-requires
5760
(let [requires (core/get-ns-component (clean-ns ns2) :require)
5861
combined-requires (core/get-ns-component ns2-cleaned :require)]
@@ -233,3 +236,13 @@
233236
(deftest fallback-to-relative-path
234237
(is (= (pprint-ns (clean-ns ns1))
235238
(pprint-ns (clean-ns ns1-relative-path)))))
239+
240+
;; keep quotes around string requires
241+
(deftest npm-string-preservation
242+
(let [cleaned (pprint-ns (clean-ns ns-with-npm-strs))]
243+
(is (re-find #"\[\"react-native\" :as rn\]" cleaned))))
244+
245+
;; group string requires together when sorting
246+
(deftest npm-string-sorting
247+
(is (= (pprint-ns (clean-ns ns-with-npm-strs))
248+
(pprint-ns (read-string (slurp (:path ns-with-npm-strs-clean)))))))

test/resources/ns_with_npm_strs.cljs

+14
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
(ns resources.ns-with-npm-strs
2+
(:require ["react-native" :as rn]
3+
[clojure.string :as str]
4+
["@react-native-community/async-storage" :as async-storage]
5+
["@fortawesome/react-native-fontawesome" :as fa]
6+
["@fortawesome/free-solid-svg-icons" :as fa-icons]))
7+
8+
(defmacro black-hole [& body])
9+
10+
(black-hole
11+
rn/View
12+
str/join
13+
fa/FontAwesomeIcon
14+
fa-icons/faCoffee)
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
(ns resources.ns-with-npm-strs
2+
(:require ["@fortawesome/free-solid-svg-icons" :as fa-icons]
3+
["@fortawesome/react-native-fontawesome" :as fa]
4+
["react-native" :as rn]
5+
[clojure.string :as str]))

0 commit comments

Comments
 (0)