Skip to content

Commit 952dc4b

Browse files
author
dnolen
committed
consistently provide the compiler state in the api nses if not provided by user
1 parent 078f207 commit 952dc4b

File tree

3 files changed

+96
-26
lines changed

3 files changed

+96
-26
lines changed

src/main/clojure/cljs/analyzer/api.clj

Lines changed: 19 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -67,12 +67,14 @@
6767
facilitate code walking without knowing the details of the op set."
6868
([env form] (analyze env form nil))
6969
([env form name] (analyze env form name nil))
70-
([env form name opts] (analyze env/*compiler* env form name opts))
70+
([env form name opts]
71+
(analyze
72+
(if-not (nil? env/*compiler*)
73+
env/*compiler*
74+
(env/default-compiler-env opts))
75+
env form name opts))
7176
([state env form name opts]
72-
(if state
73-
(env/with-compiler-env state
74-
(binding [ana/*cljs-warning-handlers* (:warning-handlers opts ana/*cljs-warning-handlers*)]
75-
(ana/analyze env form name opts)))
77+
(env/with-compiler-env state
7678
(binding [ana/*cljs-warning-handlers* (:warning-handlers opts ana/*cljs-warning-handlers*)]
7779
(ana/analyze env form name opts)))))
7880

@@ -94,7 +96,12 @@
9496
requested via opts where :restore is false."
9597
([src] (parse-ns src nil nil))
9698
([src opts] (parse-ns src nil opts))
97-
([src dest opts] (parse-ns env/*compiler* src dest opts))
99+
([src dest opts]
100+
(parse-ns
101+
(if-not (nil? env/*compiler*)
102+
env/*compiler*
103+
(env/default-compiler-env opts))
104+
src dest opts))
98105
([state src dest opts]
99106
(env/with-compiler-env state
100107
(binding [ana/*cljs-warning-handlers* (:warning-handlers opts ana/*cljs-warning-handlers*)]
@@ -110,7 +117,12 @@
110117
\":output-dir/some/ns/foo.cljs.cache.edn\". This function does not return a
111118
meaningful value."
112119
([f] (analyze-file f nil))
113-
([f opts] (analyze-file env/*compiler* f opts))
120+
([f opts]
121+
(analyze-file
122+
(if-not (nil? env/*compiler*)
123+
env/*compiler*
124+
(env/default-compiler-env opts))
125+
f opts))
114126
([state f opts]
115127
(env/with-compiler-env state
116128
(binding [ana/*cljs-warning-handlers* (:warning-handlers opts ana/*cljs-warning-handlers*)]

src/main/clojure/cljs/build/api.clj

Lines changed: 47 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -57,19 +57,29 @@
5757
'example.macros :
5858
(cljs-dependents-for-macro-namespaces 'example.macros) ->
5959
('example.core 'example.util)"
60-
([namespaces] (cljs-dependents-for-macro-namespaces env/*compiler* namespaces))
60+
([namespaces]
61+
(cljs-dependents-for-macro-namespaces
62+
(if-not (nil? env/*compiler*)
63+
env/*compiler*
64+
(env/default-compiler-env))
65+
namespaces))
6166
([state namespaces]
6267
(map :name
63-
(let [namespaces-set (set namespaces)]
64-
(filter (fn [x] (not-empty
65-
(intersection namespaces-set (-> x :require-macros vals set))))
66-
(vals (:cljs.analyzer/namespaces @state)))))))
68+
(let [namespaces-set (set namespaces)]
69+
(filter (fn [x] (not-empty
70+
(intersection namespaces-set (-> x :require-macros vals set))))
71+
(vals (:cljs.analyzer/namespaces @state)))))))
6772

6873
(defn cljs-ns-dependents
6974
"Given a namespace symbol return a seq of all dependent
7075
namespaces sorted in dependency order. Will include
7176
transient dependents."
72-
([ns] (cljs-ns-dependents env/*compiler* ns))
77+
([ns]
78+
(cljs-ns-dependents
79+
(if-not (nil? env/*compiler*)
80+
env/*compiler*
81+
(env/default-compiler-env))
82+
ns))
7383
([state ns]
7484
(env/with-compiler-env state
7585
(ana/ns-dependents ns))))
@@ -85,7 +95,12 @@
8595
"Given a ClojureScript source file return the target file. May optionally
8696
provide build options with :output-dir specified."
8797
([src] (src-file->target-file src nil))
88-
([src opts] (src-file->target-file env/*compiler* src opts))
98+
([src opts]
99+
(src-file->target-file
100+
(if-not (nil? env/*compiler*)
101+
env/*compiler*
102+
(env/default-compiler-env opts))
103+
src opts))
89104
([state src opts]
90105
(env/with-compiler-env state
91106
(binding [ana/*cljs-warning-handlers* (:warning-handlers opts ana/*cljs-warning-handlers*)]
@@ -95,7 +110,12 @@
95110
"Given a ClojureScript or Google Closure style JavaScript source file return
96111
the goog.require statement for it."
97112
([src] (src-file->goog-require src nil))
98-
([src options] (src-file->goog-require env/*compiler* src options))
113+
([src options]
114+
(src-file->goog-require
115+
(if-not (nil? env/*compiler*)
116+
env/*compiler*
117+
(env/default-compiler-env))
118+
src options))
99119
([state src options]
100120
(env/with-compiler-env state
101121
(binding [ana/*cljs-warning-handlers* (:warning-handlers options ana/*cljs-warning-handlers*)]
@@ -126,7 +146,11 @@
126146
uri of the corresponding source regardless of the source language extension:
127147
.cljs, .cljc, .js. Returns a map containing :relative-path a string, and
128148
:uri a URL."
129-
([ns] (ns->location ns env/*compiler*))
149+
([ns]
150+
(ns->location ns
151+
(if-not (nil? env/*compiler*)
152+
env/*compiler*
153+
(env/default-compiler-env))))
130154
([ns compiler-env]
131155
(closure/source-for-namespace ns compiler-env)))
132156

@@ -163,7 +187,12 @@
163187

164188
(defn compile
165189
"Given a Compilable, compile it and return an IJavaScript."
166-
([opts compilable] (compile env/*compiler* opts compilable))
190+
([opts compilable]
191+
(compile
192+
(if-not (nil? env/*compiler*)
193+
env/*compiler*
194+
(env/default-compiler-env opts))
195+
opts compilable))
167196
([state opts compilable]
168197
(env/with-compiler-env state
169198
(closure/compile compilable opts))))
@@ -181,17 +210,21 @@
181210
(defn build
182211
"Given a source which can be compiled, produce runnable JavaScript."
183212
([source opts]
184-
(build source opts nil))
213+
(build source opts
214+
(if-not (nil? env/*compiler*)
215+
env/*compiler*
216+
(env/default-compiler-env opts))))
185217
([source opts compiler-env]
186218
(binding [ana/*cljs-warning-handlers* (:warning-handlers opts ana/*cljs-warning-handlers*)]
187219
(closure/build source opts compiler-env))))
188220

189221
(defn watch
190222
"Given a source which can be compiled, watch it for changes to produce."
191223
([source opts]
192-
(watch source opts (if-not (nil? env/*compiler*)
193-
env/*compiler*
194-
(env/default-compiler-env opts))))
224+
(watch source opts
225+
(if-not (nil? env/*compiler*)
226+
env/*compiler*
227+
(env/default-compiler-env opts))))
195228
([source opts compiler-env]
196229
(watch source opts compiler-env nil))
197230
([source opts compiler-env stop]

src/main/clojure/cljs/compiler/api.clj

Lines changed: 30 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,12 @@
1919

2020
(defn emit
2121
"Given an AST node generated by the analyzer emit JavaScript as a string."
22-
([ast] (emit env/*compiler* ast))
22+
([ast]
23+
(emit
24+
(if-not (nil? env/*compiler*)
25+
env/*compiler*
26+
(env/default-compiler-env))
27+
ast))
2328
([state ast]
2429
(env/with-compiler-env state
2530
(with-out-str
@@ -29,7 +34,12 @@
2934
"Ensure that core.cljs has been loaded."
3035
([] (comp/with-core-cljs nil))
3136
([opts] (with-core-cljs opts (fn [])))
32-
([opts body] (with-core-cljs env/*compiler* opts body))
37+
([opts body]
38+
(with-core-cljs
39+
(if-not (nil? env/*compiler*)
40+
env/*compiler*
41+
(env/default-compiler-env opts))
42+
opts body))
3343
([state opts body]
3444
(env/with-compiler-env state
3545
(binding [ana/*cljs-warning-handlers* (:warning-handlers opts ana/*cljs-warning-handlers*)]
@@ -38,7 +48,12 @@
3848
(defn requires-compilation?
3949
"Return true if the src file requires compilation."
4050
([src dest] (requires-compilation? src dest nil))
41-
([src dest opts] (requires-compilation? env/*compiler* src dest opts))
51+
([src dest opts]
52+
(requires-compilation?
53+
(if-not (nil? env/*compiler*)
54+
env/*compiler*
55+
(env/default-compiler-env opts))
56+
src dest opts))
4257
([state src dest opts]
4358
(env/with-compiler-env state
4459
(binding [ana/*cljs-warning-handlers* (:warning-handlers opts ana/*cljs-warning-handlers*)]
@@ -59,7 +74,12 @@
5974
If the file was not compiled returns only {:file ...}"
6075
([src] (compile-file src))
6176
([src dest] (compile-file src dest))
62-
([src dest opts] (compile-file env/*compiler* src dest opts))
77+
([src dest opts]
78+
(compile-file
79+
(if-not (nil? env/*compiler*)
80+
env/*compiler*
81+
(env/default-compiler-env opts))
82+
src dest opts))
6383
([state src dest opts]
6484
(env/with-compiler-env state
6585
(binding [ana/*cljs-warning-handlers* (:warning-handlers opts ana/*cljs-warning-handlers*)]
@@ -78,7 +98,12 @@
7898
in dependency order."
7999
([src-dir] (compile-root src-dir "out"))
80100
([src-dir target-dir] (compile-root src-dir target-dir nil))
81-
([src-dir target-dir opts] (compile-root env/*compiler* src-dir target-dir opts))
101+
([src-dir target-dir opts]
102+
(compile-root
103+
(if-not (nil? env/*compiler*)
104+
env/*compiler*
105+
(env/default-compiler-env opts))
106+
src-dir target-dir opts))
82107
([state src-dir target-dir opts]
83108
(env/with-compiler-env state
84109
(binding [ana/*cljs-warning-handlers* (:warning-handlers opts ana/*cljs-warning-handlers*)]

0 commit comments

Comments
 (0)