From ba03ee9635abf412523fc6bc3ba337733f2466e0 Mon Sep 17 00:00:00 2001 From: Joren Broekema Date: Tue, 4 Jun 2024 13:58:30 +0200 Subject: [PATCH] fix: expand on platform level to adjust path prop (#1231) --- .changeset/mighty-rings-share.md | 5 ++++ __tests__/utils/expandObjectTokens.test.js | 32 ++++++++++++++++++++++ lib/utils/expandObjectTokens.js | 5 ++++ 3 files changed, 42 insertions(+) create mode 100644 .changeset/mighty-rings-share.md diff --git a/.changeset/mighty-rings-share.md b/.changeset/mighty-rings-share.md new file mode 100644 index 000000000..be240a724 --- /dev/null +++ b/.changeset/mighty-rings-share.md @@ -0,0 +1,5 @@ +--- +'style-dictionary': patch +--- + +Fix for expand utility on platform level to adjust the token's path property. diff --git a/__tests__/utils/expandObjectTokens.test.js b/__tests__/utils/expandObjectTokens.test.js index 88c96b421..b570febb7 100644 --- a/__tests__/utils/expandObjectTokens.test.js +++ b/__tests__/utils/expandObjectTokens.test.js @@ -100,6 +100,38 @@ describe('utils', () => { expect(expanded).to.eql(borderOutput); }); + it('should adjust the path properties of the newly expanded tokens if path prop is already present (platform expand)', () => { + const expanded = expandToken( + { + type: 'border', + value: { + width: '2px', + style: 'solid', + color: '#000', + }, + path: ['input', 'border'], + }, + { expand: true, usesDtcg: false }, + ); + expect(expanded).to.eql({ + color: { + type: 'color', + value: '#000', + path: ['input', 'border', 'color'], + }, + style: { + type: 'strokeStyle', + value: 'solid', + path: ['input', 'border', 'style'], + }, + width: { + type: 'dimension', + value: '2px', + path: ['input', 'border', 'width'], + }, + }); + }); + it('should handle DTCG spec tokens expansion', () => { const expanded = expandToken( { diff --git a/lib/utils/expandObjectTokens.js b/lib/utils/expandObjectTokens.js index 7be36b1f0..83bbbefa8 100644 --- a/lib/utils/expandObjectTokens.js +++ b/lib/utils/expandObjectTokens.js @@ -178,6 +178,11 @@ export function expandToken(token, opts, platform) { [`${uses$ ? '$' : ''}value`]: value, [`${uses$ ? '$' : ''}type`]: getTypeFromMap(key, compositionType, typesMap), }; + if (Array.isArray(expandedTokenObjRef[key].path)) { + // if we're expanding on platform level, we already have a path property + // we will need to adjust this by adding the new keys of th expanded tokens to the path array + expandedTokenObjRef[key].path = [...expandedTokenObjRef[key].path, key]; + } }); });