From e1b71ce7cd873da081e63d8aacc667fa433ba517 Mon Sep 17 00:00:00 2001 From: Michiel Borkent Date: Sat, 4 Oct 2025 14:43:32 +0200 Subject: [PATCH 1/2] CLJS-3451: make munge-str public --- src/main/cljs/cljs/core.cljs | 12 ++++++++++-- src/main/clojure/cljs/compiler.cljc | 2 +- src/test/cljs/cljs/core_test.cljs | 8 ++++++-- 3 files changed, 17 insertions(+), 5 deletions(-) diff --git a/src/main/cljs/cljs/core.cljs b/src/main/cljs/cljs/core.cljs index 8eabe774b..27464f4f0 100644 --- a/src/main/cljs/cljs/core.cljs +++ b/src/main/cljs/cljs/core.cljs @@ -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)) @@ -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_" diff --git a/src/main/clojure/cljs/compiler.cljc b/src/main/clojure/cljs/compiler.cljc index faba462b5..522a88357 100644 --- a/src/main/clojure/cljs/compiler.cljc +++ b/src/main/clojure/cljs/compiler.cljc @@ -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))))) diff --git a/src/test/cljs/cljs/core_test.cljs b/src/test/cljs/cljs/core_test.cljs index 686a0fd72..31d76b3d3 100644 --- a/src/test/cljs/cljs/core_test.cljs +++ b/src/test/cljs/cljs/core_test.cljs @@ -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])) @@ -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])))) \ No newline at end of file + (is (= "1234" (apply cljs.core/str_ 1 2 [3 4])))) From 92a39bab1770c6056543c64bdd4addb34a933efc Mon Sep 17 00:00:00 2001 From: davidnolen Date: Sat, 4 Oct 2025 09:46:33 -0400 Subject: [PATCH 2/2] fix typo in test-munge-str test --- src/test/cljs/cljs/core_test.cljs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/test/cljs/cljs/core_test.cljs b/src/test/cljs/cljs/core_test.cljs index 31d76b3d3..d92fb5105 100644 --- a/src/test/cljs/cljs/core_test.cljs +++ b/src/test/cljs/cljs/core_test.cljs @@ -1140,7 +1140,7 @@ (is (= "function$" (munge "function")))) (deftest test-munge-str - (is (= "function$" (munge-str "function")))) + (is (= "function" (munge-str "function")))) (defprotocol IFooBar (a-method [t]))