Skip to content

Commit

Permalink
remove cljs.analyzer.api current|empty-state - just a unpleasant api.…
Browse files Browse the repository at this point in the history
… Instead add

a new `current-state` helper to get dynamic state if needed for some reason.
Refactor build & compiler api to consistently use analyzer api for dealing with cljs.env
  • Loading branch information
swannodette committed Apr 3, 2020
1 parent 5b2d4b8 commit 45022fa
Show file tree
Hide file tree
Showing 3 changed files with 35 additions and 43 deletions.
18 changes: 6 additions & 12 deletions src/main/clojure/cljs/analyzer/api.cljc
Original file line number Diff line number Diff line change
Expand Up @@ -28,16 +28,12 @@
([opts]
(env/default-compiler-env opts)))

(defn current|empty-state
"Returns the currently bound compiler state or an empty one"
[]
(if-not (nil? env/*compiler*)
env/*compiler*
(empty-state)))
(defn current-state []
env/*compiler*)

(defmacro with-state
"Run the body with the given compilation state Atom<Map>."
[state body]
[state & body]
`(env/with-compiler-env ~state
~@body))

Expand Down Expand Up @@ -67,14 +63,12 @@

(defn get-options
"Return the compiler options from compiler state."
([] (get-options env/*compiler*))
([state]
(get @state :options)))

(defn get-js-index
"Return the currently computed Google Closure js dependency index from the
compiler state."
([] (get-options env/*compiler*))
([state]
(get @state :js-dependency-index)))

Expand All @@ -90,7 +84,7 @@
([env form] (analyze env form nil))
([env form name] (analyze env form name nil))
([env form name opts]
(analyze (current|empty-state) env form name opts))
(analyze (empty-state opts) env form name opts))
([state env form name opts]
(env/with-compiler-env state
(binding [ana/*cljs-warning-handlers* (:warning-handlers opts ana/*cljs-warning-handlers*)]
Expand All @@ -117,7 +111,7 @@
([src] (parse-ns src nil nil))
([src opts] (parse-ns src nil opts))
([src dest opts]
(parse-ns (current|empty-state) src dest opts))
(parse-ns (empty-state opts) src dest opts))
([state src dest opts]
(env/with-compiler-env state
(binding [ana/*cljs-warning-handlers* (:warning-handlers opts ana/*cljs-warning-handlers*)]
Expand All @@ -134,7 +128,7 @@
meaningful value."
([f] (analyze-file f nil))
([f opts]
(analyze-file (current|empty-state) f opts))
(analyze-file (empty-state opts) f opts))
([state f opts]
(env/with-compiler-env state
(binding [ana/*cljs-warning-handlers* (:warning-handlers opts ana/*cljs-warning-handlers*)]
Expand Down
33 changes: 16 additions & 17 deletions src/main/clojure/cljs/build/api.clj
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,6 @@
(:refer-clojure :exclude [compile])
(:require [clojure.java.io :as io]
[cljs.util :as util]
[cljs.env :as env]
[cljs.analyzer :as ana]
[cljs.analyzer.api :as ana-api]
[cljs.closure :as closure])
Expand Down Expand Up @@ -52,7 +51,7 @@
('example.core 'example.util)"
([namespaces]
(closure/cljs-dependents-for-macro-namespaces
(ana-api/current|empty-state) namespaces))
(ana-api/empty-state) namespaces))
([state namespaces]
(closure/cljs-dependents-for-macro-namespaces state namespaces)))

Expand All @@ -68,9 +67,9 @@
provide build options with :output-dir specified."
([src] (src-file->target-file src nil))
([src opts]
(src-file->target-file (ana-api/current|empty-state) src opts))
(src-file->target-file (ana-api/empty-state opts) src opts))
([state src opts]
(env/with-compiler-env state
(ana-api/with-state state
(binding [ana/*cljs-warning-handlers* (:warning-handlers opts ana/*cljs-warning-handlers*)]
(closure/src-file->target-file src opts)))))

Expand All @@ -79,9 +78,9 @@
the goog.require statement for it."
([src] (src-file->goog-require src nil))
([src opts]
(src-file->goog-require (ana-api/current|empty-state) src opts))
(src-file->goog-require (ana-api/empty-state opts) src opts))
([state src opts]
(env/with-compiler-env state
(ana-api/with-state state
(binding [ana/*cljs-warning-handlers* (:warning-handlers opts ana/*cljs-warning-handlers*)]
(closure/src-file->goog-require src opts)))))

Expand Down Expand Up @@ -121,7 +120,7 @@
.cljs, .cljc, .js. Returns a map containing :relative-path a string, and
:uri a URL."
([ns]
(ns->location ns (ana-api/current|empty-state)))
(ns->location ns (ana-api/empty-state)))
([ns compiler-env]
(closure/source-for-namespace ns compiler-env)))

Expand All @@ -139,9 +138,9 @@
([xs]
(add-dependency-sources xs {}))
([xs opts]
(add-dependency-sources (ana-api/current|empty-state) xs opts))
(add-dependency-sources (ana-api/empty-state opts) xs opts))
([state xs opts]
(env/with-compiler-env state
(ana-api/with-state state
(closure/add-dependency-sources xs opts))))

(defn add-dependencies
Expand Down Expand Up @@ -180,9 +179,9 @@
(defn compile
"Given a Compilable, compile it and return an IJavaScript."
([opts compilable]
(compile (ana-api/current|empty-state) opts compilable))
(compile (ana-api/empty-state opts) opts compilable))
([state opts compilable]
(env/with-compiler-env state
(ana-api/with-state state
(closure/compile compilable opts))))

(defn output-unoptimized
Expand Down Expand Up @@ -245,8 +244,8 @@
(if (compiler-opts? dependencies)
(install-node-deps! (:npm-deps dependencies) dependencies)
(install-node-deps! dependencies
(when-not (nil? env/*compiler*)
(:options @env/*compiler*)))))
(when-let [state (ana-api/current-state)]
(:options @state)))))
([dependencies opts]
{:pre [(map? dependencies)]}
(closure/check-npm-deps opts)
Expand All @@ -264,8 +263,8 @@
(if (compiler-opts? dependencies)
(get-node-deps (keys (:npm-deps dependencies)) dependencies)
(get-node-deps dependencies
(when-not (nil? env/*compiler*)
(:options @env/*compiler*)))))
(when-let [state (ana-api/current-state)]
(:options @state)))))
([dependencies opts]
{:pre [(sequential? dependencies)]}
(closure/index-node-modules
Expand All @@ -279,12 +278,12 @@
installed."
([entries]
(node-inputs entries
(:options (ana-api/current|empty-state))))
(:options (ana-api/empty-state))))
([entries opts]
(closure/node-inputs entries opts)))

(defn node-modules
"Return a sequence of requirable libraries found under node_modules."
([opts]
(env/with-compiler-env (ana-api/empty-state opts)
(ana-api/with-state (ana-api/empty-state opts)
(filter :provides (closure/index-node-modules-dir)))))
27 changes: 13 additions & 14 deletions src/main/clojure/cljs/compiler/api.clj
Original file line number Diff line number Diff line change
Expand Up @@ -9,9 +9,7 @@
(ns cljs.compiler.api
"This is intended to be a stable api for those who need programmatic access
to the compiler."
(:require [cljs.util :as util]
[cljs.env :as env]
[cljs.analyzer :as ana]
(:require [cljs.analyzer :as ana]
[cljs.analyzer.api :as ana-api]
[cljs.compiler :as comp]
[cljs.closure :as closure]))
Expand All @@ -22,32 +20,33 @@
(defn emit
"Given an AST node generated by the analyzer emit JavaScript as a string."
([ast]
(emit (ana-api/current|empty-state) ast))
(emit (ana-api/empty-state) ast))
([state ast]
(env/with-compiler-env state
(ana-api/with-state state
(with-out-str
(comp/emit ast)))))

(defn with-core-cljs
"Ensure that core.cljs has been loaded."
([]
(comp/with-core-cljs
(:options (ana-api/current|empty-state))))
(when-let [state (ana-api/current-state)]
(:options @state))))
([opts] (with-core-cljs opts (fn [])))
([opts body]
(with-core-cljs (ana-api/current|empty-state) opts body))
(with-core-cljs (ana-api/empty-state opts) opts body))
([state opts body]
(env/with-compiler-env state
(ana-api/with-state state
(binding [ana/*cljs-warning-handlers* (:warning-handlers opts ana/*cljs-warning-handlers*)]
(comp/with-core-cljs opts body)))))

(defn requires-compilation?
"Return true if the src file requires compilation."
([src dest] (requires-compilation? src dest nil))
([src dest opts]
(requires-compilation? (ana-api/current|empty-state) src dest opts))
(requires-compilation? (ana-api/empty-state opts) src dest opts))
([state src dest opts]
(env/with-compiler-env state
(ana-api/with-state state
(binding [ana/*cljs-warning-handlers* (:warning-handlers opts ana/*cljs-warning-handlers*)]
(comp/requires-compilation? src dest opts)))))

Expand All @@ -69,9 +68,9 @@
([src dest]
(compile-file src dest nil))
([src dest opts]
(compile-file (ana-api/current|empty-state) src dest opts))
(compile-file (ana-api/empty-state opts) src dest opts))
([state src dest opts]
(env/with-compiler-env state
(ana-api/with-state state
(binding [ana/*cljs-warning-handlers* (:warning-handlers opts ana/*cljs-warning-handlers*)]
(comp/compile-file src dest opts)))))

Expand All @@ -89,8 +88,8 @@
([src-dir] (compile-root src-dir "out"))
([src-dir target-dir] (compile-root src-dir target-dir nil))
([src-dir target-dir opts]
(compile-root (ana-api/current|empty-state) src-dir target-dir opts))
(compile-root (ana-api/empty-state opts) src-dir target-dir opts))
([state src-dir target-dir opts]
(env/with-compiler-env state
(ana-api/with-state state
(binding [ana/*cljs-warning-handlers* (:warning-handlers opts ana/*cljs-warning-handlers*)]
(comp/compile-root src-dir target-dir opts)))))

0 comments on commit 45022fa

Please sign in to comment.