Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: pass usesDtcg flag to resolveReferences util #294

Merged
merged 1 commit into from
Jul 4, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions .changeset/swift-suns-share.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
'@tokens-studio/sd-transforms': patch
---

Fix bug where usesDtcg flag was not passed to resolveReference utility.
24 changes: 14 additions & 10 deletions src/preprocessors/add-font-styles.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,11 +8,11 @@ import { usesReferences, resolveReferences } from 'style-dictionary/utils';
import { fontWeightReg, fontStyles } from '../transformFontWeight.js';
import { TransformOptions } from '../TransformOptions.js';

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

if (isToken) {
const token = potentiallyToken as SingleToken<false>;
const usesDTCG = Object.hasOwn(token, '$value');
const usesDtcg = Object.hasOwn(token, '$value');
const { value, $value, type, $type } = token;
const tokenType = (usesDTCG ? $type : type) as string;
const tokenValue = (usesDTCG ? $value : value) as
const tokenType = (usesDtcg ? $type : type) as string;
const tokenValue = (usesDtcg ? $value : value) as
| (TokenTypographyValue & { fontStyle: string })
| SingleFontWeightsToken['value'];

if (tokenType === 'typography') {
const tokenTypographyValue = tokenValue as TokenTypographyValue & { fontStyle: string };
if (tokenTypographyValue.fontWeight === undefined) return;
const fontWeight = resolveFontWeight(`${tokenTypographyValue.fontWeight}`, refCopy);
const fontWeight = resolveFontWeight(
`${tokenTypographyValue.fontWeight}`,
refCopy,
usesDtcg,
);
const { weight, style } = splitWeightStyle(fontWeight, alwaysAddFontStyle);

tokenTypographyValue.fontWeight = weight;
Expand All @@ -75,16 +79,16 @@ function recurse(
}
} else if (tokenType === 'fontWeight') {
const tokenFontWeightsValue = tokenValue as SingleFontWeightsToken['value'];
const fontWeight = resolveFontWeight(`${tokenFontWeightsValue}`, refCopy);
const fontWeight = resolveFontWeight(`${tokenFontWeightsValue}`, refCopy, usesDtcg);
const { weight, style } = splitWeightStyle(fontWeight, alwaysAddFontStyle);

// since tokenFontWeightsValue is a primitive (string), we have to permutate the change directly
token[`${usesDTCG ? '$' : ''}value`] = weight;
token[`${usesDtcg ? '$' : ''}value`] = weight;
if (style) {
(slice as DeepKeyTokenMap<false>)[`fontStyle`] = {
...token,
[`${usesDTCG ? '$' : ''}type`]: 'fontStyle',
[`${usesDTCG ? '$' : ''}value`]: style,
[`${usesDtcg ? '$' : ''}type`]: 'fontStyle',
[`${usesDtcg ? '$' : ''}value`]: style,
};
}
}
Expand Down
39 changes: 39 additions & 0 deletions test/spec/preprocessors/add-font-styles.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -243,4 +243,43 @@ describe('add font style', () => {
},
});
});

it('handlestinvalid fontweight structures e.g. mixing token group / token, also handles DTCG format', () => {
// @ts-expect-error aligned types already here
const tokens = {
foo: {
$type: 'fontWeight',
$value: '700',
bar: {
$type: 'fontWeight',
$value: '800',
},
},
thing: {
$type: 'typography',
$value: {
fontWeight: '{foo}',
},
},
} as DeepKeyTokenMap<false>;

const processed = addFontStyles(tokens);

expect(processed).to.eql({
foo: {
$type: 'fontWeight',
$value: '700',
bar: {
$type: 'fontWeight',
$value: '800',
},
},
thing: {
$type: 'typography',
$value: {
fontWeight: '700',
},
},
});
});
});
Loading