From 5de1f450c185d9bfcc0fcc1cf9b75b2cd5f993a7 Mon Sep 17 00:00:00 2001 From: Florian Schroedl Date: Thu, 3 Oct 2024 15:14:38 +0200 Subject: [PATCH] Move over token value reference check function --- common/src/app/common/types/tokens_lib.cljc | 11 +++++++++++ common/test/common_tests/types/tokens_lib_test.cljc | 11 ++++++++++- frontend/src/app/main/ui/workspace/tokens/form.cljs | 2 +- .../main/ui/workspace/tokens/style_dictionary.cljs | 3 ++- frontend/test/token_tests/token_test.cljs | 10 ---------- 5 files changed, 24 insertions(+), 13 deletions(-) diff --git a/common/src/app/common/types/tokens_lib.cljc b/common/src/app/common/types/tokens_lib.cljc index d9a5c95a0..e48c02fd8 100644 --- a/common/src/app/common/types/tokens_lib.cljc +++ b/common/src/app/common/types/tokens_lib.cljc @@ -132,6 +132,17 @@ token)) +(defn find-token-value-references + "Returns set of token references found in `token-value`. + + Used for checking if a token has a reference in the value. + Token references are strings delimited by curly braces. + E.g.: {foo.bar.baz} -> foo.bar.baz" + [token-value] + (some->> (re-seq #"\{([^}]*)\}" token-value) + (map second) + (into #{}))) + (defn group-by-type [tokens] (let [tokens' (if (or (map? tokens) (d/ordered-map? tokens)) diff --git a/common/test/common_tests/types/tokens_lib_test.cljc b/common/test/common_tests/types/tokens_lib_test.cljc index 072f71382..cfb6920ef 100644 --- a/common/test/common_tests/types/tokens_lib_test.cljc +++ b/common/test/common_tests/types/tokens_lib_test.cljc @@ -44,8 +44,17 @@ :type :invalid}] (t/is (thrown-with-msg? Exception #"expected valid token" (apply ctob/make-token args))) - (t/is (false? (ctob/valid-token? {})))))) + (t/is (false? (ctob/valid-token? {}))))) + (t/deftest find-token-value-references + (t/testing "finds references inside curly braces in a string" + (t/is (= #{"foo" "bar"} (ctob/find-token-value-references "{foo} + {bar}"))) + (t/testing "ignores extra text" + (t/is (= #{"foo.bar.baz"} (ctob/find-token-value-references "{foo.bar.baz} + something"))))) + (t/testing "ignores string without references" + (t/is (nil? (ctob/find-token-value-references "1 + 2")))) + (t/testing "handles edge-case for extra curly braces" + (t/is (= #{"foo" "bar"} (ctob/find-token-value-references "{foo}} + {bar}")))))) (t/testing "token-set" (t/deftest make-token-set diff --git a/frontend/src/app/main/ui/workspace/tokens/form.cljs b/frontend/src/app/main/ui/workspace/tokens/form.cljs index 3bb0339f4..26522e2db 100644 --- a/frontend/src/app/main/ui/workspace/tokens/form.cljs +++ b/frontend/src/app/main/ui/workspace/tokens/form.cljs @@ -93,7 +93,7 @@ Token names should only contain letters and digits separated by . characters.")} (defn token-self-reference? [token-name input] - (let [token-references (wtt/find-token-references input) + (let [token-references (ctob/find-token-value-references input) self-reference? (get token-references token-name)] self-reference?)) diff --git a/frontend/src/app/main/ui/workspace/tokens/style_dictionary.cljs b/frontend/src/app/main/ui/workspace/tokens/style_dictionary.cljs index eb603f50f..9e55a3b3d 100644 --- a/frontend/src/app/main/ui/workspace/tokens/style_dictionary.cljs +++ b/frontend/src/app/main/ui/workspace/tokens/style_dictionary.cljs @@ -3,6 +3,7 @@ ["@tokens-studio/sd-transforms" :as sd-transforms] ["style-dictionary$default" :as sd] [app.common.logging :as l] + [app.common.types.tokens-lib :as ctob] [app.main.refs :as refs] [app.main.ui.workspace.tokens.errors :as wte] [app.main.ui.workspace.tokens.tinycolor :as tinycolor] @@ -85,7 +86,7 @@ {:value value :unit (tinycolor/color-format tc)} {:errors [(wte/error-with-value :error.token/invalid-color value)]}) (or (wtt/parse-token-value value) - (if-let [references (-> (wtt/find-token-references value) + (if-let [references (-> (ctob/find-token-value-references value) (seq))] {:errors [(wte/error-with-value :error.style-dictionary/missing-reference references)] :references references} diff --git a/frontend/test/token_tests/token_test.cljs b/frontend/test/token_tests/token_test.cljs index 0dfaf675d..1157125ad 100644 --- a/frontend/test/token_tests/token_test.cljs +++ b/frontend/test/token_tests/token_test.cljs @@ -25,16 +25,6 @@ (t/testing "doesnt accept invalid double" (t/is (nil? (wtt/parse-token-value ".3"))))) -(t/deftest find-token-references - (t/testing "finds references inside curly braces in a string" - (t/is (= #{"foo" "bar"} (wtt/find-token-references "{foo} + {bar}"))) - (t/testing "ignores extra text" - (t/is (= #{"foo.bar.baz"} (wtt/find-token-references "{foo.bar.baz} + something")))) - (t/testing "ignores string without references" - (t/is (nil? (wtt/find-token-references "1 + 2")))) - (t/testing "handles edge-case for extra curly braces" - (t/is (= #{"foo" "bar"} (wtt/find-token-references "{foo}} + {bar}")))))) - (t/deftest remove-attributes-for-token-id (t/testing "removes attributes matching the `token`, keeps other attributes" (t/is (= {:ry "b"}