Skip to content

Commit

Permalink
CLJS-1692: Autoalias clojure.* to exisiting cljs.* namespaces if poss…
Browse files Browse the repository at this point in the history
…ible
  • Loading branch information
dnolen committed Jun 24, 2016
1 parent 4e50980 commit 23632ba
Show file tree
Hide file tree
Showing 2 changed files with 50 additions and 1 deletion.
37 changes: 36 additions & 1 deletion src/main/clojure/cljs/analyzer.cljc
Expand Up @@ -1854,6 +1854,39 @@
(or (some #{ns} (vals use-macros))
(some #{ns} (vals require-macros))))))

(defn clj-ns->cljs-ns
"Given a symbol that starts with clojure as the first segment return the
same symbol with the first segment replaced with cljs"
[sym]
(let [segs (string/split (clojure.core/name sym) #"\.")]
(if (= "clojure" (first segs))
(symbol (string/join "." (cons "cljs" (next segs))))
sym)))

(defn aliasable-clj-ns?
"Predicate for testing with a symbol represents an aliasable clojure namespace."
[sym]
(when-not (util/ns->source sym)
(let [[seg1 :as segs] (string/split (clojure.core/name sym) #"\.")]
(when (= "clojure" seg1)
(let [sym' (clj-ns->cljs-ns sym)]
(util/ns->source sym'))))))

(defn rewrite-cljs-aliases
"Alias non-existing clojure.* namespaces to existing cljs.* namespaces if
possible."
[args]
(letfn [(process-spec [maybe-spec]
(if (sequential? maybe-spec)
(let [[lib & xs] maybe-spec]
(cons (cond-> lib (aliasable-clj-ns? lib) clj-ns->cljs-ns) xs))
maybe-spec))
(process-form [[k & specs :as form]]
(if (#{:use :require} k)
(cons k (map process-spec specs))
form))]
(map process-form args)))

(defn desugar-ns-specs
"Given an original set of ns specs desugar :include-macros and :refer-macros
usage into only primitive spec forms - :use, :require, :use-macros,
Expand Down Expand Up @@ -1942,7 +1975,9 @@
args (if docstring (next args) args)
metadata (if (map? (first args)) (first args))
form-meta (meta form)
args (desugar-ns-specs (if metadata (next args) args))
args (desugar-ns-specs
(rewrite-cljs-aliases
(if metadata (next args) args)))
name (vary-meta name merge metadata)
excludes (parse-ns-excludes env args)
deps (atom #{})
Expand Down
14 changes: 14 additions & 0 deletions src/test/clojure/cljs/analyzer_tests.clj
Expand Up @@ -272,6 +272,20 @@
(set '((:require-macros (bar :refer [quux]) :reload)
(:require (bar :refer [baz]) :reload)))))))

(deftest test-rewrite-cljs-aliases
(is (= (cljs.analyzer/rewrite-cljs-aliases
'((:require-macros (bar :refer [quux]) :reload)
(:require (clojure.spec :as [s]) :reload)))
'((:require-macros (bar :refer [quux]) :reload)
(:require (cljs.spec :as [s]) :reload))))
(is (= (cljs.analyzer/rewrite-cljs-aliases
'((:refer-clojure :exclude [first])
(:require-macros (bar :refer [quux]) :reload)
(:require (clojure.spec :as [s]) :reload)))
'((:refer-clojure :exclude [first])
(:require-macros (bar :refer [quux]) :reload)
(:require (cljs.spec :as [s]) :reload)))))

;; =============================================================================
;; Namespace metadata

Expand Down

0 comments on commit 23632ba

Please sign in to comment.