Skip to content

Commit b0b135b

Browse files
committed
CLJS-813: Warn about reserved JS keyword usage in namespace names
Move cljs.compiler/js-reserved to cljs.analyzer, keep var to avoid breakage Emit warning if any ns segment is a JS keyword
1 parent b6a9dc0 commit b0b135b

File tree

2 files changed

+32
-17
lines changed

2 files changed

+32
-17
lines changed

src/clj/cljs/analyzer.clj

Lines changed: 31 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -68,7 +68,23 @@
6868
:protocol-invalid-method true
6969
:protocol-duped-method true
7070
:protocol-multiple-impls true
71-
:single-segment-namespace true})
71+
:single-segment-namespace true
72+
:munged-namespace true})
73+
74+
(def js-reserved
75+
#{"abstract" "boolean" "break" "byte" "case"
76+
"catch" "char" "class" "const" "continue"
77+
"debugger" "default" "delete" "do" "double"
78+
"else" "enum" "export" "extends" "final"
79+
"finally" "float" "for" "function" "goto" "if"
80+
"implements" "import" "in" "instanceof" "int"
81+
"interface" "let" "long" "native" "new"
82+
"package" "private" "protected" "public"
83+
"return" "short" "static" "super" "switch"
84+
"synchronized" "this" "throw" "throws"
85+
"transient" "try" "typeof" "var" "void"
86+
"volatile" "while" "with" "yield" "methods"
87+
"null"})
7288

7389
(declare message namespaces)
7490

@@ -184,6 +200,15 @@
184200
[warning-type info]
185201
(str (:name info) " is a single segment namespace"))
186202

203+
(defmethod error-message :munged-namespace
204+
[warning-type {:keys [name] :as info}]
205+
(let [munged (->> (string/split (clojure.core/name name) #"\.")
206+
(map #(if (js-reserved %) (str % "$") %))
207+
(string/join ".")
208+
(munge))]
209+
(str "Namespace " name " contains a reserved JavaScript keyword,"
210+
" the corresponding Google Closure namespace will be munged to " munged)))
211+
187212
(defn ^:private default-warning-handler [warning-type env extra]
188213
(when (warning-type *cljs-warnings*)
189214
(when-let [s (error-message warning-type extra)]
@@ -1385,8 +1410,11 @@
13851410
[_ env [_ name & args :as form] _ opts]
13861411
(when-not (symbol? name)
13871412
(throw (error env "Namespaces must be named by a symbol.")))
1388-
(when (= 1 (count (string/split (clojure.core/name name) #"\.")))
1389-
(warning :single-segment-namespace env {:name name}))
1413+
(let [segments (string/split (clojure.core/name name) #"\.")]
1414+
(when (= 1 (count segments))
1415+
(warning :single-segment-namespace env {:name name}))
1416+
(when (some js-reserved segments)
1417+
(warning :munged-namespace env {:name name})))
13901418
(let [docstring (if (string? (first args)) (first args))
13911419
mdocstr (-> name meta :doc)
13921420
args (if docstring (next args) args)

src/clj/cljs/compiler.clj

Lines changed: 1 addition & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -22,20 +22,7 @@
2222

2323
(set! *warn-on-reflection* true)
2424

25-
(def js-reserved
26-
#{"abstract" "boolean" "break" "byte" "case"
27-
"catch" "char" "class" "const" "continue"
28-
"debugger" "default" "delete" "do" "double"
29-
"else" "enum" "export" "extends" "final"
30-
"finally" "float" "for" "function" "goto" "if"
31-
"implements" "import" "in" "instanceof" "int"
32-
"interface" "let" "long" "native" "new"
33-
"package" "private" "protected" "public"
34-
"return" "short" "static" "super" "switch"
35-
"synchronized" "this" "throw" "throws"
36-
"transient" "try" "typeof" "var" "void"
37-
"volatile" "while" "with" "yield" "methods"
38-
"null"})
25+
(def js-reserved ana/js-reserved)
3926

4027
(def ^:dynamic *dependents* nil)
4128
(def ^:dynamic *source-map-data* nil)

0 commit comments

Comments
 (0)