Skip to content

Commit 00ac012

Browse files
CLJS-3451: make munge-str public (#269)
* CLJS-3451: make munge-str public --------- Co-authored-by: Michiel Borkent <[email protected]>
1 parent f9a6856 commit 00ac012

File tree

3 files changed

+17
-5
lines changed

3 files changed

+17
-5
lines changed

src/main/cljs/cljs/core.cljs

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -11887,7 +11887,9 @@ reduces them without incurring seq initialization"
1188711887
(str_ ret "|\\$"))))))
1188811888
DEMUNGE_PATTERN)
1188911889

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

11902-
(defn munge [name]
11904+
(defn munge
11905+
"Munge symbol or string `name` for safe use in JavaScript.
11906+
11907+
- Replaces '..' with '_DOT__DOT_'.
11908+
- Appends '$' to JavaScript reserved keywords.
11909+
- Returns a symbol if `name` was a symbol, otherwise a string."
11910+
[name]
1190311911
(let [name' (munge-str (str_ name))
1190411912
name' (cond
1190511913
(identical? name' "..") "_DOT__DOT_"

src/main/clojure/cljs/compiler.cljc

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -142,7 +142,7 @@
142142
ss (map rf (string/split ss #"\."))
143143
ss (string/join "." ss)
144144
ms #?(:clj (clojure.lang.Compiler/munge ss)
145-
:cljs (#'cljs.core/munge-str ss))]
145+
:cljs (munge-str ss))]
146146
(if (symbol? s)
147147
(symbol ms)
148148
ms)))))

src/test/cljs/cljs/core_test.cljs

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1136,7 +1136,11 @@
11361136
(is (= "_DOT__DOT_" (munge "..")))
11371137
(is (= "abstract$" (munge "abstract")))
11381138
(is (= 'abc (munge 'abc)))
1139-
(is (= "toString" (munge "toString"))))
1139+
(is (= "toString" (munge "toString")))
1140+
(is (= "function$" (munge "function"))))
1141+
1142+
(deftest test-munge-str
1143+
(is (= "function" (munge-str "function"))))
11401144

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

0 commit comments

Comments
 (0)