(defn parse-require-spec [env macros? deps aliases spec]
(if (or (symbol? spec) (string? spec))
(recur env macros? deps aliases [spec])
(do
(basic-validate-ns-spec env macros? spec)
(let [[lib & opts] spec
;; We need to load JS modules by the name that has been created by the
;; Google Closure compiler, e.g. module$resources$libs$calculator.
;; This means that we need to create an alias from the module name
;; given with :provides to the new name.
[lib js-module-provides] (if-some [js-module-name (gets @env/*compiler* :js-module-index (str lib) :name)]
[(symbol js-module-name) lib]
[lib nil])
{alias :as referred :refer renamed :rename
:or {alias (if (string? lib)
(symbol (munge lib))
lib)}}
(apply hash-map opts)
referred-without-renamed (seq (remove (set (keys renamed)) referred))
[rk uk renk] (if macros? [:require-macros :use-macros :rename-macros] [:require :use :rename])]
(when-not (or (symbol? alias) (nil? alias))
(throw
(error env
(parse-ns-error-msg spec
":as must be followed by a symbol in :require / :require-macros"))))
(when (some? alias)
(let [alias-type (if macros? :macros :fns)
lib' ((alias-type @aliases) alias)]
(when (and (some? lib') (not= lib lib'))
(throw (error env (parse-ns-error-msg spec ":as alias must be unique"))))
(swap! aliases
update-in [alias-type]
conj [alias lib] (when js-module-provides [js-module-provides lib]))))
(when-not (or (and (sequential? referred)
(every? symbol? referred))
(nil? referred))
(throw
(error env
(parse-ns-error-msg spec
":refer must be followed by a sequence of symbols in :require / :require-macros"))))
(when-not macros?
(swap! deps conj lib))
(merge
(when (some? alias)
{rk (merge {alias lib} {lib lib}
(when js-module-provides {js-module-provides lib}))})
(when (some? referred-without-renamed)
{uk (apply hash-map (interleave referred-without-renamed (repeat lib)))})
(when (some? renamed)
{renk (reduce (fn [m [original renamed]]
(when-not (some #{original} referred)
(throw (error env
(str "Renamed symbol " original " not referred"))))
(assoc m renamed (symbol (str lib) (str original))))
{} renamed)}))))))