From 980238e27b34d4690ac9c2e149093606d16ea6eb Mon Sep 17 00:00:00 2001 From: Florian Schroedl Date: Thu, 8 Aug 2024 09:22:32 +0200 Subject: [PATCH] Move find-token-references to token namespace --- .../app/main/ui/workspace/tokens/form.cljs | 2 +- .../ui/workspace/tokens/style_dictionary.cljs | 7 ------- .../app/main/ui/workspace/tokens/token.cljs | 7 +++++++ .../token_tests/style_dictionary_test.cljs | 20 ------------------- frontend/test/token_tests/token_test.cljs | 10 ++++++++++ 5 files changed, 18 insertions(+), 28 deletions(-) delete mode 100644 frontend/test/token_tests/style_dictionary_test.cljs diff --git a/frontend/src/app/main/ui/workspace/tokens/form.cljs b/frontend/src/app/main/ui/workspace/tokens/form.cljs index 5094f29ef..400e2b6cc 100644 --- a/frontend/src/app/main/ui/workspace/tokens/form.cljs +++ b/frontend/src/app/main/ui/workspace/tokens/form.cljs @@ -92,7 +92,7 @@ Token names should only contain letters and digits separated by . characters.")} ;; When creating a new token we dont have a token name yet, ;; so we use a temporary token name that hopefully doesn't clash with any of the users token names. token-name (if (str/empty? name-value) "__TOKEN_STUDIO_SYSTEM.TEMP" name-value) - token-references (sd/find-token-references input) + token-references (wtt/find-token-references input) direct-self-reference? (get token-references token-name)] (cond empty-input? (p/rejected nil) 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 7d16ffd0e..83231fc30 100644 --- a/frontend/src/app/main/ui/workspace/tokens/style_dictionary.cljs +++ b/frontend/src/app/main/ui/workspace/tokens/style_dictionary.cljs @@ -23,13 +23,6 @@ ;; Functions ------------------------------------------------------------------- -(defn find-token-references - "Finds token reference values in `value-string` and returns a set with all contained namespaces." - [value-string] - (some->> (re-seq #"\{([^}]*)\}" value-string) - (map second) - (into #{}))) - (defn tokens->style-dictionary+ "Resolves references and math expressions using StyleDictionary. Returns a promise with the resolved dictionary." diff --git a/frontend/src/app/main/ui/workspace/tokens/token.cljs b/frontend/src/app/main/ui/workspace/tokens/token.cljs index 647dab736..557d3dad0 100644 --- a/frontend/src/app/main/ui/workspace/tokens/token.cljs +++ b/frontend/src/app/main/ui/workspace/tokens/token.cljs @@ -4,6 +4,13 @@ [clojure.set :as set] [cuerdas.core :as str])) +(defn find-token-references + "Finds token reference values in `value-string` and returns a set with all contained namespaces." + [value-string] + (some->> (re-seq #"\{([^}]*)\}" value-string) + (map second) + (into #{}))) + (defn token-identifier [{:keys [name] :as _token}] name) diff --git a/frontend/test/token_tests/style_dictionary_test.cljs b/frontend/test/token_tests/style_dictionary_test.cljs deleted file mode 100644 index ff03ba16c..000000000 --- a/frontend/test/token_tests/style_dictionary_test.cljs +++ /dev/null @@ -1,20 +0,0 @@ -;; This Source Code Form is subject to the terms of the Mozilla Public -;; License, v. 2.0. If a copy of the MPL was not distributed with this -;; file, You can obtain one at http://mozilla.org/MPL/2.0/. -;; -;; Copyright (c) KALEIDOS INC - -(ns token-tests.style-dictionary-test - (:require - [app.main.ui.workspace.tokens.style-dictionary :as wtsd] - [cljs.test :as t :include-macros true])) - -(t/deftest test-find-token-references - ;; Return references - (t/is (= #{"foo" "bar"} (wtsd/find-token-references "{foo} + {bar}"))) - ;; Ignore non reference text - (t/is (= #{"foo.bar.baz"} (wtsd/find-token-references "{foo.bar.baz} + something"))) - ;; No references found - (t/is (nil? (wtsd/find-token-references "1 + 2"))) - ;; Edge-case: Ignore unmatched closing parens - (t/is (= #{"foo" "bar"} (wtsd/find-token-references "{foo}} + {bar}")))) diff --git a/frontend/test/token_tests/token_test.cljs b/frontend/test/token_tests/token_test.cljs index 4ad17cf5b..d9f2732b9 100644 --- a/frontend/test/token_tests/token_test.cljs +++ b/frontend/test/token_tests/token_test.cljs @@ -9,6 +9,16 @@ [app.main.ui.workspace.tokens.token :as wtt] [cljs.test :as t :include-macros true])) +(t/deftest find-token-references + ;; Return references + (t/is (= #{"foo" "bar"} (wtt/find-token-references "{foo} + {bar}"))) + ;; Ignore non reference text + (t/is (= #{"foo.bar.baz"} (wtt/find-token-references "{foo.bar.baz} + something"))) + ;; No references found + (t/is (nil? (wtt/find-token-references "1 + 2"))) + ;; Edge-case: Ignore unmatched closing parens + (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"}