Skip to content

Commit

Permalink
Fix missing style names when compiled under :simple optimizations
Browse files Browse the repository at this point in the history
Fixes #7
  • Loading branch information
dhleong committed Mar 26, 2020
1 parent 9d64aec commit ab28f11
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 6 deletions.
24 changes: 19 additions & 5 deletions src/spade/util.cljc
Original file line number Diff line number Diff line change
@@ -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
Expand Down
7 changes: 6 additions & 1 deletion test/spade/util_test.cljs
Original file line number Diff line number Diff line change
Expand Up @@ -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 ""})))))

0 comments on commit ab28f11

Please sign in to comment.