From 5c4fd975419e36bc75c7fbc3a5afe6e2a5735e37 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Andr=C3=A9s=20Moya?= Date: Fri, 27 Jun 2025 11:32:14 +0200 Subject: [PATCH] :bug: Allow importing file without any token but with themes or sets (#6796) --- common/src/app/common/types/tokens_lib.cljc | 28 +++++++++++++++------ 1 file changed, 21 insertions(+), 7 deletions(-) diff --git a/common/src/app/common/types/tokens_lib.cljc b/common/src/app/common/types/tokens_lib.cljc index e6ca9e89b5..dd64992312 100644 --- a/common/src/app/common/types/tokens_lib.cljc +++ b/common/src/app/common/types/tokens_lib.cljc @@ -1212,7 +1212,8 @@ Will return a value that matches this schema: "Searches through decoded token file and returns: - `:json-format/legacy` when first node satisfies `legacy-node?` predicate - `:json-format/dtcg` when first node satisfies `dtcg-node?` predicate - - `nil` if neither combination is found" + - If neither combination is found, return dtcg format by default (we assume that + the file does not contain any token, so the format is irrelevan)." ([decoded-json] (get-json-format decoded-json legacy-node? dtcg-node?)) ([decoded-json legacy-node? dtcg-node?] @@ -1230,9 +1231,10 @@ Will return a value that matches this schema: (check-node node) (when (branch? node) (mapcat walk (children node))))))] - (->> (walk decoded-json) - (filter some?) - first)))) ;; TODO: throw error if format cannot be determined + (d/nilv (->> (walk decoded-json) + (filter some?) + first) + :json-format/dtcg)))) (defn- legacy-json->dtcg-json "Converts a decoded json file in legacy format into DTCG format." @@ -1291,9 +1293,17 @@ Will return a value that matches this schema: [set-name decoded-json-tokens] (assert (map? decoded-json-tokens) "expected a plain clojure map for `decoded-json-tokens`") (assert (= (get-json-format decoded-json-tokens) :json-format/dtcg) "expected a dtcg format for `decoded-json-tokens`") - (-> (make-tokens-lib) - (add-set (make-token-set :name (normalize-set-name set-name) - :tokens (flatten-nested-tokens-json decoded-json-tokens ""))))) + + (let [set-name (normalize-set-name set-name) + tokens (flatten-nested-tokens-json decoded-json-tokens "")] + + (when (empty? tokens) + (throw (ex-info "the file doesn't contain any tokens" + {:error/code :error.import/invalid-json-data}))) + + (-> (make-tokens-lib) + (add-set (make-token-set :name set-name + :tokens tokens))))) (defn- parse-single-set-legacy-json "Parse a decoded json file with a single set of tokens in legacy format into a TokensLib." @@ -1385,6 +1395,10 @@ Will return a value that matches this schema: library active-theme-names)] + (when (and (empty? sets) (empty? themes)) + (throw (ex-info "the file doesn't contain any tokens" + {:error/code :error.import/invalid-json-data}))) + library)) (defn- parse-multi-set-legacy-json