Skip to content

Commit

Permalink
Fix #505: support :rename in require
Browse files Browse the repository at this point in the history
  • Loading branch information
borkdude committed Apr 3, 2024
1 parent 05f3692 commit 966c38a
Show file tree
Hide file tree
Showing 3 changed files with 24 additions and 10 deletions.
4 changes: 4 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,10 @@

[Squint](https://github.com/squint-cljs/squint): Light-weight ClojureScript dialect

## v0.7.102 (2024-04-03)

- Fix [#505](https://github.com/squint-cljs/squint/issues/505): Support `:rename` in `:require`

## v0.7.101 (2024-04-02)

- Fix [#490](https://github.com/squint-cljs/squint/issues/490): render css maps in html mode
Expand Down
26 changes: 17 additions & 9 deletions src/squint/compiler_common.cljc
Original file line number Diff line number Diff line change
Expand Up @@ -270,18 +270,20 @@
(:bool (meta renamed)) (bool-expr))
(let [ns-state @(:ns-state env)
current (:current ns-state)
current-ns (get ns-state current)
m (munged-name expr)]
current-ns (get ns-state current)]
(or
(when (contains? current-ns expr)
(str (when *repl*
(str "globalThis." (munge *cljs-ns*) ".")) m))
(str "globalThis." (munge *cljs-ns*) ".")) (munged-name expr)))
(some-> (maybe-core-var expr env) munge)
(when (or (contains? (:refers current-ns) expr)
(let [alias (get (:aliases current-ns) expr)]
alias))
(str (when *repl*
(str "globalThis." (munge *cljs-ns*) ".")) m))
(let [renamed (:rename current-ns)
expr (get renamed expr expr)]
(when (or (contains? (:refers current-ns) expr)
(let [alias (get (:aliases current-ns) expr)]
alias))
(str (when *repl*
(str "globalThis." (munge *cljs-ns*) "."))
(munged-name expr))))
(let [m (munged-name expr)]
m)))))]
(emit-return (escape-jsx expr env)
Expand Down Expand Up @@ -502,7 +504,7 @@
alias)
alias))

(defn process-require-clause [env current-ns-name [libname & {:keys [refer as]}]]
(defn process-require-clause [env current-ns-name [libname & {:keys [rename refer as]}]]
(when-not (or (= 'squint.core libname)
(= 'cherry.core libname))
(let [libname (resolve-ns env libname)
Expand Down Expand Up @@ -532,6 +534,12 @@
(update-in ns-state [current :refers]
(fn [refers]
(merge refers (zipmap refer (repeat libname))))))))
(when rename
(swap! (:ns-state env)
(fn [ns-state]
(let [current (:current ns-state)]
(update-in ns-state [current :rename]
merge (zipmap (vals rename) (keys rename)))))))
(let [munged-refers (map munge refer)]
(if *repl*
(str (statement (format "var { %s } = await import('%s')" (str/join ", " munged-refers) libname))
Expand Down
4 changes: 3 additions & 1 deletion test/squint/compiler_test.cljs
Original file line number Diff line number Diff line change
Expand Up @@ -1657,7 +1657,9 @@
(doseq [repl [false true]]
(let [js (compiler/compile-string "(require '[clojure.string :as str-ing]) (str-ing/join [1 2 3])" {:repl repl})]
(is (not (str/includes? js "str-ing")))
(is (str/includes? js "str_ing")))))
(is (str/includes? js "str_ing"))))
(let [s (compiler/compile-string "(ns test-namespace (:require [\"some-js-library\" :refer [existsSync] :rename {existsSync exists}])) (exists \"README.md\")")]
(is (str/includes? s "existsSync(\"README.md\")"))))

(deftest dissoc!-test
(is (eq #js {"1" 2 "3" 4} (jsv! '(dissoc! {"1" 2 "3" 4}))))
Expand Down

0 comments on commit 966c38a

Please sign in to comment.