From ab28f11c68f2b265f549dccc6d4044abd68ceb10 Mon Sep 17 00:00:00 2001 From: Daniel Leong Date: Wed, 25 Mar 2020 21:21:54 -0400 Subject: [PATCH] Fix missing style names when compiled under :simple optimizations Fixes #7 --- src/spade/util.cljc | 24 +++++++++++++++++++----- test/spade/util_test.cljs | 7 ++++++- 2 files changed, 25 insertions(+), 6 deletions(-) diff --git a/src/spade/util.cljc b/src/spade/util.cljc index a0c2e01..f30b896 100644 --- a/src/spade/util.cljc +++ b/src/spade/util.cljc @@ -1,11 +1,25 @@ (ns ^:no-doc spade.util (:require [clojure.string :as str])) -(defn factory->name [factory] - (-> (.-name factory) - (str/replace "_factory$" "") - (str/replace #"[_$]" "-") - (str/replace #"^-" "_"))) +(defn factory->name + "Given a style factory function, return an appropriate name for its + style. This function assumes it will be called *once* for any given + factory; subsequent calls for the same factory *may not* return the + same value (especially under :simple optimizations)." + [factory] + (let [given-name (.-name factory)] + (if (empty? given-name) + ; under :simple optimizations, the way the function is declared does + ; not leave any value for its name. so... generate one! + (name (gensym "SPD")) + + ; normal case: base the style name on the factory function's name. + ; this lets us have descriptive names in dev, and concise names in + ; prod, without having to embed anything extra in the file + (-> given-name + (str/replace "_factory$" "") + (str/replace #"[_$]" "-") + (str/replace #"^-" "_"))))) (defn sanitize [s] (-> s diff --git a/test/spade/util_test.cljs b/test/spade/util_test.cljs index a446bd5..6a42b8c 100644 --- a/test/spade/util_test.cljs +++ b/test/spade/util_test.cljs @@ -7,5 +7,10 @@ ; simulate a fn under advanced compilation having ; a name that starts with _ (is (= "_advanced-compile" - (factory->name #js {:name "_advanced_compile"}))))) + (factory->name #js {:name "_advanced_compile"})))) + + (testing "Never prefix with illegal characters" + ; simulate a fn under simple compilation not + ; having *any* name + (is (seq (factory->name #js {:name ""})))))