Skip to content

Commit

Permalink
Add :auto-resolve for auto resolving keywords (#52)
Browse files Browse the repository at this point in the history
  • Loading branch information
djblue authored Aug 12, 2022
1 parent 6c93643 commit 4eb2675
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 3 deletions.
22 changes: 19 additions & 3 deletions src/cherry/compiler.cljc
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,7 @@
(def statement-separator ";\n")

;; TODO: move to context argument
(def ^:dynamic *aliases* (atom {}))
(def ^:dynamic *async* false)
(def ^:dynamic *imported-core-vars* (atom #{}))
(def ^:dynamic *public-vars* (atom #{}))
Expand Down Expand Up @@ -422,7 +423,19 @@
(when refer
(statement (format "import { %s } from '%s'" (str/join ", " refer) libname))))))

(defmethod emit-special 'ns [_type _env [_ns _name & clauses]]
(defmethod emit-special 'ns [_type _env [_ns name & clauses]]
(reset! *aliases*
(->> clauses
(some
(fn [[k & exprs]]
(when (= :require k) exprs)))
(reduce
(fn [aliases [full as alias]]
(case as
(:as :as-alias)
(assoc aliases alias full)
aliases))
{:current name})))
(reduce (fn [acc [k & exprs]]
(if (= :require k)
(str acc (str/join "" (map process-require-clause exprs)))
Expand Down Expand Up @@ -819,7 +832,8 @@ break;}" body)
(let [rdr (e/reader s)
opts cherry-parse-opts]
(loop [transpiled ""]
(let [next-form (e/parse-next rdr opts)]
(let [opts (assoc opts :auto-resolve @*aliases*)
next-form (e/parse-next rdr opts)]
(if (= ::e/eof next-form)
transpiled
(let [next-t (transpile-form next-form)
Expand All @@ -831,9 +845,11 @@ break;}" body)
([s {:keys [elide-exports
elide-imports]}]
(let [core-vars (atom #{})
public-vars (atom #{})]
public-vars (atom #{})
aliases (atom {})]
(binding [*imported-core-vars* core-vars
*public-vars* public-vars
*aliases* aliases
*jsx* false]
(let [transpiled (transpile-string* s)
imports (when-let [core-vars (and (not elide-imports)
Expand Down
3 changes: 3 additions & 0 deletions test/cherry/compiler_test.cljs
Original file line number Diff line number Diff line change
Expand Up @@ -372,5 +372,8 @@
(is (= -10 (jsv! '(- 10))))
(is (= -11 (jsv! '(- 10 21)))))

(deftest namespace-keywords
(is (= :hello/world (jsv! "(ns hello) ::world"))))

(defn init []
(cljs.test/run-tests 'cherry.compiler-test))

0 comments on commit 4eb2675

Please sign in to comment.