Skip to content

Commit

Permalink
CLJS-3320: Compiler warning on trying to use js as an ns
Browse files Browse the repository at this point in the history
  • Loading branch information
mfikes committed Feb 20, 2024
1 parent 3d110e4 commit dd289f5
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 0 deletions.
9 changes: 9 additions & 0 deletions src/main/clojure/cljs/analyzer.cljc
Original file line number Diff line number Diff line change
Expand Up @@ -159,6 +159,7 @@
:protocol-impl-recur-with-target true
:single-segment-namespace true
:munged-namespace true
:js-used-as-alias true
:ns-var-clash true
:non-dynamic-earmuffed-var true
:extend-type-invalid-method-shape true
Expand All @@ -177,6 +178,7 @@
(::externs @env/*compiler*))

(defn checked-arrays
"Returns false-y, :warn, or :error based on configuration and the
"Returns false-y, :warn, or :error based on configuration and the
current value of *unchecked-arrays*."
[]
Expand Down Expand Up @@ -439,6 +441,10 @@
(str "Namespace " name " contains a reserved JavaScript keyword,"
" the corresponding Google Closure namespace will be munged to " munged)))

(defmethod error-message :js-used-as-alias
[warning-type {:keys [spec] :as info}]
(str \"In \" (pr-str spec) \", the alias name js is reserved for JavaScript interop\"))

(defmethod error-message :ns-var-clash
[warning-type {:keys [ns var] :as info}]
(str "Namespace " ns " clashes with var " var))
Expand Down Expand Up @@ -2994,6 +3000,9 @@
lib' ((alias-type @aliases) alias)]
(when (and (some? lib') (not= lib lib'))
(throw (error env (parse-ns-error-msg spec ":as alias must be unique"))))
(when (= alias 'js)
(when-not (= lib (get-in @aliases [(if macros? :fns :macros) 'js])) ; warn only once
(warning :js-used-as-alias env {:spec spec})))
(swap! aliases
update-in [alias-type]
conj [alias lib] (when js-module-provides [js-module-provides lib]))))
Expand Down
7 changes: 7 additions & 0 deletions src/test/clojure/cljs/analyzer_tests.clj
Original file line number Diff line number Diff line change
Expand Up @@ -1493,6 +1493,13 @@
(:import goog))]))
(is (= {} (get-in @cenv [::ana/namespaces 'test.foo :imports])))))

(deftest test-cljs-3320
(let [ws (atom [])]
(ana/with-warning-handlers [(collecting-warning-handler ws)]
(binding [ana/*cljs-ns* 'cljs.user]
(analyze ns-env '(ns cljs3320.core (:require [cljs.js :as js])))))
(is (string/includes? (first @ws) "the alias name js is reserved for JavaScript interop"))))

(deftest test-cljs-3371
(let [ws (atom [])]
(ana/with-warning-handlers [(collecting-warning-handler ws)]
Expand Down

0 comments on commit dd289f5

Please sign in to comment.