Skip to content

Commit 4f400cb

Browse files
swannodettemfikes
andauthored
CLJS-3320: Compiler warning on trying to use js as an ns (#224)
Co-authored-by: Mike Fikes <mike@fikesfarm.com>
1 parent 2c5dbdf commit 4f400cb

File tree

2 files changed

+15
-0
lines changed

2 files changed

+15
-0
lines changed

Diff for: src/main/clojure/cljs/analyzer.cljc

+8
Original file line numberDiff line numberDiff line change
@@ -159,6 +159,7 @@
159159
:protocol-impl-recur-with-target true
160160
:single-segment-namespace true
161161
:munged-namespace true
162+
:js-used-as-alias true
162163
:ns-var-clash true
163164
:non-dynamic-earmuffed-var true
164165
:extend-type-invalid-method-shape true
@@ -439,6 +440,10 @@
439440
(str "Namespace " name " contains a reserved JavaScript keyword,"
440441
" the corresponding Google Closure namespace will be munged to " munged)))
441442

443+
(defmethod error-message :js-used-as-alias
444+
[warning-type {:keys [spec] :as info}]
445+
(str "In " (pr-str spec) ", the alias name js is reserved for JavaScript interop"))
446+
442447
(defmethod error-message :ns-var-clash
443448
[warning-type {:keys [ns var] :as info}]
444449
(str "Namespace " ns " clashes with var " var))
@@ -2994,6 +2999,9 @@
29942999
lib' ((alias-type @aliases) alias)]
29953000
(when (and (some? lib') (not= lib lib'))
29963001
(throw (error env (parse-ns-error-msg spec ":as alias must be unique"))))
3002+
(when (= alias 'js)
3003+
(when-not (= lib (get-in @aliases [(if macros? :fns :macros) 'js])) ; warn only once
3004+
(warning :js-used-as-alias env {:spec spec})))
29973005
(swap! aliases
29983006
update-in [alias-type]
29993007
conj [alias lib] (when js-module-provides [js-module-provides lib]))))

Diff for: src/test/clojure/cljs/analyzer_tests.clj

+7
Original file line numberDiff line numberDiff line change
@@ -1493,6 +1493,13 @@
14931493
(:import goog))]))
14941494
(is (= {} (get-in @cenv [::ana/namespaces 'test.foo :imports])))))
14951495

1496+
(deftest test-cljs-3320
1497+
(let [ws (atom [])]
1498+
(ana/with-warning-handlers [(collecting-warning-handler ws)]
1499+
(binding [ana/*cljs-ns* 'cljs.user]
1500+
(analyze ns-env '(ns cljs3320.core (:require [cljs.js :as js])))))
1501+
(is (string/includes? (first @ws) "the alias name js is reserved for JavaScript interop"))))
1502+
14961503
(deftest test-cljs-3371
14971504
(let [ws (atom [])]
14981505
(ana/with-warning-handlers [(collecting-warning-handler ws)]

0 commit comments

Comments
 (0)