Skip to content

Commit 9c02741

Browse files
fix: pass usesDtcg flag to resolveReferences util (#294)
1 parent ed10715 commit 9c02741

File tree

3 files changed

+58
-10
lines changed

3 files changed

+58
-10
lines changed

.changeset/swift-suns-share.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
---
2+
'@tokens-studio/sd-transforms': patch
3+
---
4+
5+
Fix bug where usesDtcg flag was not passed to resolveReference utility.

src/preprocessors/add-font-styles.ts

Lines changed: 14 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -8,11 +8,11 @@ import { usesReferences, resolveReferences } from 'style-dictionary/utils';
88
import { fontWeightReg, fontStyles } from '../transformFontWeight.js';
99
import { TransformOptions } from '../TransformOptions.js';
1010

11-
function resolveFontWeight(fontWeight: string, copy: DeepKeyTokenMap<false>) {
11+
function resolveFontWeight(fontWeight: string, copy: DeepKeyTokenMap<false>, usesDtcg: boolean) {
1212
let resolved = fontWeight;
1313
if (usesReferences(fontWeight)) {
1414
try {
15-
resolved = `${resolveReferences(fontWeight, copy)}`;
15+
resolved = `${resolveReferences(fontWeight, copy, { usesDtcg })}`;
1616
} catch (e) {
1717
// dont throw fatal, see: https://github.com/tokens-studio/sd-transforms/issues/217
1818
// we throw once we only support SD v4, for v3 we need to be more permissive
@@ -56,17 +56,21 @@ function recurse(
5656

5757
if (isToken) {
5858
const token = potentiallyToken as SingleToken<false>;
59-
const usesDTCG = Object.hasOwn(token, '$value');
59+
const usesDtcg = Object.hasOwn(token, '$value');
6060
const { value, $value, type, $type } = token;
61-
const tokenType = (usesDTCG ? $type : type) as string;
62-
const tokenValue = (usesDTCG ? $value : value) as
61+
const tokenType = (usesDtcg ? $type : type) as string;
62+
const tokenValue = (usesDtcg ? $value : value) as
6363
| (TokenTypographyValue & { fontStyle: string })
6464
| SingleFontWeightsToken['value'];
6565

6666
if (tokenType === 'typography') {
6767
const tokenTypographyValue = tokenValue as TokenTypographyValue & { fontStyle: string };
6868
if (tokenTypographyValue.fontWeight === undefined) return;
69-
const fontWeight = resolveFontWeight(`${tokenTypographyValue.fontWeight}`, refCopy);
69+
const fontWeight = resolveFontWeight(
70+
`${tokenTypographyValue.fontWeight}`,
71+
refCopy,
72+
usesDtcg,
73+
);
7074
const { weight, style } = splitWeightStyle(fontWeight, alwaysAddFontStyle);
7175

7276
tokenTypographyValue.fontWeight = weight;
@@ -75,16 +79,16 @@ function recurse(
7579
}
7680
} else if (tokenType === 'fontWeight') {
7781
const tokenFontWeightsValue = tokenValue as SingleFontWeightsToken['value'];
78-
const fontWeight = resolveFontWeight(`${tokenFontWeightsValue}`, refCopy);
82+
const fontWeight = resolveFontWeight(`${tokenFontWeightsValue}`, refCopy, usesDtcg);
7983
const { weight, style } = splitWeightStyle(fontWeight, alwaysAddFontStyle);
8084

8185
// since tokenFontWeightsValue is a primitive (string), we have to permutate the change directly
82-
token[`${usesDTCG ? '$' : ''}value`] = weight;
86+
token[`${usesDtcg ? '$' : ''}value`] = weight;
8387
if (style) {
8488
(slice as DeepKeyTokenMap<false>)[`fontStyle`] = {
8589
...token,
86-
[`${usesDTCG ? '$' : ''}type`]: 'fontStyle',
87-
[`${usesDTCG ? '$' : ''}value`]: style,
90+
[`${usesDtcg ? '$' : ''}type`]: 'fontStyle',
91+
[`${usesDtcg ? '$' : ''}value`]: style,
8892
};
8993
}
9094
}

test/spec/preprocessors/add-font-styles.spec.ts

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -243,4 +243,43 @@ describe('add font style', () => {
243243
},
244244
});
245245
});
246+
247+
it('handlestinvalid fontweight structures e.g. mixing token group / token, also handles DTCG format', () => {
248+
// @ts-expect-error aligned types already here
249+
const tokens = {
250+
foo: {
251+
$type: 'fontWeight',
252+
$value: '700',
253+
bar: {
254+
$type: 'fontWeight',
255+
$value: '800',
256+
},
257+
},
258+
thing: {
259+
$type: 'typography',
260+
$value: {
261+
fontWeight: '{foo}',
262+
},
263+
},
264+
} as DeepKeyTokenMap<false>;
265+
266+
const processed = addFontStyles(tokens);
267+
268+
expect(processed).to.eql({
269+
foo: {
270+
$type: 'fontWeight',
271+
$value: '700',
272+
bar: {
273+
$type: 'fontWeight',
274+
$value: '800',
275+
},
276+
},
277+
thing: {
278+
$type: 'typography',
279+
$value: {
280+
fontWeight: '700',
281+
},
282+
},
283+
});
284+
});
246285
});

0 commit comments

Comments
 (0)