From 4f400cb926e2a567293131d3743e71395691fe21 Mon Sep 17 00:00:00 2001 From: David Nolen Date: Wed, 21 Feb 2024 12:29:40 -0500 Subject: [PATCH] CLJS-3320: Compiler warning on trying to use `js` as an ns (#224) Co-authored-by: Mike Fikes --- src/main/clojure/cljs/analyzer.cljc | 8 ++++++++ src/test/clojure/cljs/analyzer_tests.clj | 7 +++++++ 2 files changed, 15 insertions(+) diff --git a/src/main/clojure/cljs/analyzer.cljc b/src/main/clojure/cljs/analyzer.cljc index 6759c6d24..f0f179a49 100644 --- a/src/main/clojure/cljs/analyzer.cljc +++ b/src/main/clojure/cljs/analyzer.cljc @@ -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 @@ -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)) @@ -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])))) diff --git a/src/test/clojure/cljs/analyzer_tests.clj b/src/test/clojure/cljs/analyzer_tests.clj index 8ae750ddc..e96b6ae3f 100644 --- a/src/test/clojure/cljs/analyzer_tests.clj +++ b/src/test/clojure/cljs/analyzer_tests.clj @@ -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)]