Skip to content

Commit aec9f0c

Browse files
committed
CLJS-3074: Resolve :warning-handlers compiler option when symbols
Handle in add-implicit-options. Add tests.
1 parent 5590b8d commit aec9f0c

File tree

2 files changed

+68
-20
lines changed

2 files changed

+68
-20
lines changed

src/main/clojure/cljs/closure.clj

Lines changed: 47 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -83,25 +83,27 @@
8383
"Converts a namespaced symbol to a var, loading the requisite namespace if
8484
needed. For use with a function defined under a keyword in opts. The kw and
8585
ex-data arguments are used to form exceptions."
86-
[sym kw ex-data]
87-
(let [ns (namespace sym)
88-
_ (when (nil? ns)
89-
(throw
90-
(ex-info (str kw " symbol " sym " is not fully qualified")
91-
(merge ex-data {kw sym
92-
:clojure.error/phase :compilation}))))
93-
var-ns (symbol ns)]
94-
(when (not (find-ns var-ns))
95-
(try
96-
(locking ana/load-mutex
97-
(require var-ns))
98-
(catch Throwable t
99-
(throw (ex-info (str "Cannot require namespace referred by " kw " value " sym)
100-
(merge ex-data {kw sym
101-
:clojure.error/phase :compilation})
102-
t)))))
103-
104-
(find-var sym)))
86+
([sym kw]
87+
(sym->var sym kw nil))
88+
([sym kw ex-data]
89+
(let [ns (namespace sym)
90+
_ (when (nil? ns)
91+
(throw
92+
(ex-info (str kw " symbol " sym " is not fully qualified")
93+
(merge ex-data {kw sym
94+
:clojure.error/phase :compilation}))))
95+
var-ns (symbol ns)]
96+
(when (not (find-ns var-ns))
97+
(try
98+
(locking ana/load-mutex
99+
(require var-ns))
100+
(catch Throwable t
101+
(throw (ex-info (str "Cannot require namespace referred by " kw " value " sym)
102+
(merge ex-data {kw sym
103+
:clojure.error/phase :compilation})
104+
t)))))
105+
106+
(find-var sym))))
105107

106108
(defn- opts-fn
107109
"Extracts a function from opts, by default expecting a function value, but
@@ -2483,6 +2485,28 @@
24832485
[(if (symbol? k) (str (comp/munge k)) k) v])
24842486
defines)))
24852487

2488+
(defn resolve-warning-handlers [fns]
2489+
(reduce
2490+
(fn [ret afn]
2491+
(cond
2492+
(fn? afn) (conj ret afn)
2493+
2494+
(symbol? afn)
2495+
(let [afn' (sym->var afn :warning-handlers)]
2496+
(when-not afn'
2497+
(throw
2498+
(ex-info (str "Could not resolve warning handler: " afn)
2499+
{:warning-handlers fns
2500+
:clojure.error/phase :compilation})))
2501+
(conj ret afn'))
2502+
2503+
:else
2504+
(throw
2505+
(ex-info (str "Invalid warning handler " afn " of type " (type afn))
2506+
{:warning-handlers fns
2507+
:clojure.error/phase :compilation}))))
2508+
[] fns))
2509+
24862510
(defn add-implicit-options
24872511
[{:keys [optimizations output-dir]
24882512
:or {optimizations :none
@@ -2586,7 +2610,10 @@
25862610
opts)))
25872611

25882612
(nil? (:ignore-js-module-exts opts))
2589-
(assoc :ignore-js-module-exts [".css"]))))
2613+
(assoc :ignore-js-module-exts [".css"])
2614+
2615+
(:warning-handlers opts)
2616+
(update :warning-handlers resolve-warning-handlers))))
25902617

25912618
(defn- alive? [proc]
25922619
(try (.exitValue proc) false (catch IllegalThreadStateException _ true)))

src/test/clojure/cljs/closure_tests.clj

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -490,3 +490,24 @@
490490
(.delete (io/file "package.json"))
491491
(test/delete-node-modules)
492492
(test/delete-out-files out)))
493+
494+
(defn empty-handler [warning-type env extra])
495+
496+
(deftest test-cljs-3074
497+
(testing "CLJS-3074: resolve-warning-handlers\n"
498+
(testing "\tfunctions are left alone"
499+
(let [h (fn [warning-type env extra])]
500+
(is (= [h] (closure/resolve-warning-handlers [h])))))
501+
(testing "\tsymbols are resolved"
502+
(is (= [#'empty-handler] (closure/resolve-warning-handlers [`empty-handler]))))
503+
(testing "\tsymbols and fns can be mixed"
504+
(let [h (fn [warning-type env extra])]
505+
(is (= [h #'empty-handler] (closure/resolve-warning-handlers [h `empty-handler])))))
506+
(testing "\tinvalid warning handler types are detected"
507+
(is (thrown-with-msg? Throwable
508+
#"Invalid warning handler 1 of type class java.lang.Long"
509+
(closure/resolve-warning-handlers [1]))))
510+
(testing "\tnon-existent handlers are detected"
511+
(is (thrown-with-msg? Throwable
512+
#"Could not resolve warning handler: clojure.core/foo"
513+
(closure/resolve-warning-handlers ['clojure.core/foo]))))))

0 commit comments

Comments
 (0)