Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

CLJS-3320: Compiler warning on trying to use js as an ns #224

Merged
merged 1 commit into from
Feb 21, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 8 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 Down Expand Up @@ -439,6 +440,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 +2999,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
Loading