Skip to content

Commit

Permalink
fix: expanding composition tokens across files (#254)
Browse files Browse the repository at this point in the history
  • Loading branch information
jorenbroekema authored Jan 23, 2024
1 parent 8bde22d commit 771428a
Show file tree
Hide file tree
Showing 5 changed files with 61 additions and 9 deletions.
5 changes: 5 additions & 0 deletions .changeset/thirty-kings-tan.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
'@tokens-studio/sd-transforms': patch
---

Fix expanding composition tokens flattening to not occur for certain object values and resulting in [object Object].
17 changes: 8 additions & 9 deletions src/parsers/expand-composites.ts
Original file line number Diff line number Diff line change
Expand Up @@ -121,15 +121,14 @@ function recurse(
console.error(e);
}
// If every key of the result (object) is a number, the ref value is a multi-value, which means TokenBoxshadowValue[]
if (
typeof token.value === 'object' &&
Object.keys(token.value).every(key => !isNaN(Number(key)))
) {
token.value = (Object.values(token.value) as TokenBoxshadowValue[]).map(part =>
flattenValues(part),
);
} else if (!usesReferences(token.value)) {
token.value = flattenValues(token.value);
if (typeof token.value === 'object') {
if (Object.keys(token.value).every(key => !isNaN(Number(key)))) {
token.value = (Object.values(token.value) as TokenBoxshadowValue[]).map(part =>
flattenValues(part),
);
} else {
token.value = flattenValues(token.value);
}
}
}
slice[key] = expandToken(token, expandType === 'shadow');
Expand Down
13 changes: 13 additions & 0 deletions test/integration/cross-file-refs.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -42,9 +42,22 @@ describe('cross file references', () => {
expect(file).to.include(`
--sdTypoFontWeight: 400;
--sdTypoFontStyle: italic;
--sdPrimaryFont: Inter;
--sdFontWeight: 800;
--sdLineHeight: 1.5;
--sdTypo2FontFamily: Inter;
--sdTypo2FontWeight: 800;
--sdTypo2LineHeight: 1.5;
--sdTypo2FontSize: 8px;
--sdDimensionScale: 2;
--sdDimensionXs: 4px;
--sdWeight: 400 italic;
--sdTypoAliasFontWeight: 400;
--sdTypoAliasFontStyle: italic;
--sdTypo3FontFamily: Inter;
--sdTypo3FontWeight: 800;
--sdTypo3LineHeight: 1.5;
--sdTypo3FontSize: 8px;
`);
});
});
31 changes: 31 additions & 0 deletions test/integration/tokens/cross-file-refs-1.tokens.json
Original file line number Diff line number Diff line change
Expand Up @@ -4,5 +4,36 @@
"fontWeight": "{weight}"
},
"type": "typography"
},
"primaryFont": {
"value": "Inter",
"type": "fontFamilies"
},
"fontWeight": {
"value": "ExtraBold",
"type": "fontWeights"
},
"lineHeight": {
"value": "1.5",
"type": "lineHeights"
},
"typo-2": {
"value": {
"fontFamily": "{primaryFont}",
"fontWeight": "{fontWeight}",
"lineHeight": "{lineHeight}",
"fontSize": "{dimension.scale}*{dimension.xs}"
},
"type": "typography"
},
"dimension": {
"scale": {
"value": "2",
"type": "number"
},
"xs": {
"value": "4px",
"type": "number"
}
}
}
4 changes: 4 additions & 0 deletions test/integration/tokens/cross-file-refs-3.tokens.json
Original file line number Diff line number Diff line change
Expand Up @@ -2,5 +2,9 @@
"typo-alias": {
"value": "{typo}",
"type": "typography"
},
"typo-3": {
"value": "{typo-2}",
"type": "typography"
}
}

0 comments on commit 771428a

Please sign in to comment.