Skip to content
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
12 changes: 10 additions & 2 deletions src/main/cljs/cljs/core.cljs
Original file line number Diff line number Diff line change
Expand Up @@ -11887,7 +11887,9 @@ reduces them without incurring seq initialization"
(str_ ret "|\\$"))))))
DEMUNGE_PATTERN)

(defn- ^string munge-str [name]
(defn ^string munge-str
"Munge string `name` without considering `..` or JavaScript reserved keywords."
[name]
(let [sb (StringBuffer.)]
(loop [i 0]
(if (< i (. name -length))
Expand All @@ -11899,7 +11901,13 @@ reduces them without incurring seq initialization"
(recur (inc i)))))
(.toString sb)))

(defn munge [name]
(defn munge
"Munge symbol or string `name` for safe use in JavaScript.

- Replaces '..' with '_DOT__DOT_'.
- Appends '$' to JavaScript reserved keywords.
- Returns a symbol if `name` was a symbol, otherwise a string."
[name]
(let [name' (munge-str (str_ name))
name' (cond
(identical? name' "..") "_DOT__DOT_"
Expand Down
2 changes: 1 addition & 1 deletion src/main/clojure/cljs/compiler.cljc
Original file line number Diff line number Diff line change
Expand Up @@ -142,7 +142,7 @@
ss (map rf (string/split ss #"\."))
ss (string/join "." ss)
ms #?(:clj (clojure.lang.Compiler/munge ss)
:cljs (#'cljs.core/munge-str ss))]
:cljs (munge-str ss))]
(if (symbol? s)
(symbol ms)
ms)))))
Expand Down
8 changes: 6 additions & 2 deletions src/test/cljs/cljs/core_test.cljs
Original file line number Diff line number Diff line change
Expand Up @@ -1136,7 +1136,11 @@
(is (= "_DOT__DOT_" (munge "..")))
(is (= "abstract$" (munge "abstract")))
(is (= 'abc (munge 'abc)))
(is (= "toString" (munge "toString"))))
(is (= "toString" (munge "toString")))
(is (= "function$" (munge "function"))))

(deftest test-munge-str
(is (= "function" (munge-str "function"))))

(defprotocol IFooBar
(a-method [t]))
Expand Down Expand Up @@ -1952,4 +1956,4 @@
(is (= "12" (apply cljs.core/str_ 1 [2])))
(is (= "1two:threefour#{:five}[:six]#{:seven}{:eight :nine}"
(apply cljs.core/str_ 1 ["two" :three 'four #{:five} [:six] #{:seven} {:eight :nine}])))
(is (= "1234" (apply cljs.core/str_ 1 2 [3 4]))))
(is (= "1234" (apply cljs.core/str_ 1 2 [3 4]))))