Skip to content

Commit

Permalink
fix: dtcg option not passed in some places (#1116)
Browse files Browse the repository at this point in the history
  • Loading branch information
jorenbroekema authored Mar 11, 2024
1 parent d5d7a8e commit d008c67
Show file tree
Hide file tree
Showing 7 changed files with 43 additions and 6 deletions.
5 changes: 5 additions & 0 deletions .changeset/happy-vans-end.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
'style-dictionary': patch
---

Fix a couple of spots where DTCG option wasn't properly taken into account, more tests added.
7 changes: 7 additions & 0 deletions __tests__/exportPlatform.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -461,6 +461,10 @@ describe('exportPlatform', () => {
},
},
},
reftest: { $value: '{zero.$value}' },
zero: { $value: '0' },
reftest2: { $value: '{one}' },
one: { $value: '1' },
},
transform: {
'custom/add/px': {
Expand Down Expand Up @@ -493,6 +497,9 @@ describe('exportPlatform', () => {

// considers "value" a token group here
expect(tokens.dimensions.nested.value.deep.$value).to.equal('30px');

expect(tokens.reftest.$value).to.equal('0px');
expect(tokens.reftest2.$value).to.equal('1px');
});
});
});
14 changes: 14 additions & 0 deletions __tests__/utils/resolveObject.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -336,5 +336,19 @@ describe('utils', () => {
expect(GroupMessages.fetchMessages(PROPERTY_REFERENCE_WARNINGS).length).to.equal(0);
expect(test.test.value).to.equal(0);
});

it('should support DTCG format', () => {
const test = resolveObject(
{
test: { $value: '{zero.$value}' },
zero: { $value: 0 },
test2: { $value: '{one}' },
one: { $value: 1 },
},
{ usesDtcg: true },
);
expect(test.test.$value).to.equal(0);
expect(test.test2.$value).to.equal(1);
});
});
});
9 changes: 7 additions & 2 deletions lib/StyleDictionary.js
Original file line number Diff line number Diff line change
Expand Up @@ -341,9 +341,12 @@ export default class StyleDictionary extends Register {
);

// referenced values, that have not (yet) been transformed should be excluded from resolving
const ignorePathsToResolve = deferredPropValueTransforms.map((p) => getName([p, 'value']));
const ignorePathsToResolve = deferredPropValueTransforms.map((p) =>
getName([p, this.options.usesDtcg ? '$value' : 'value']),
);
exportableResult = resolveObject(transformed, {
ignorePaths: ignorePathsToResolve,
usesDtcg: this.options.usesDtcg,
});

const newDeferredPropCount = deferredPropValueTransforms.length;
Expand All @@ -357,7 +360,9 @@ export default class StyleDictionary extends Register {
// if we didn't resolve any deferred references then we have a circular reference
// the resolveObject method will find the circular references
// we do this in case there are multiple circular references
resolveObject(transformed);
resolveObject(transformed, {
usesDtcg: this.options.usesDtcg,
});
finished = true;
} else {
// neither of these things, keep going.
Expand Down
8 changes: 7 additions & 1 deletion lib/transform/token.js
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,13 @@ export default async function transformToken(token, config, options) {
) {
// Only transform non-referenced values (from original)
// and transitive transforms if the value has been resolved
if (!usesReferences(token.original.value, config) || transform.transitive) {
if (
!usesReferences(
options.usesDtcg ? token.original.$value : token.original.value,
config,
) ||
transform.transitive
) {
const transformedValue = await transform.transformer(to_ret, config, options);
if (transformedValue === undefined) {
return undefined;
Expand Down
2 changes: 1 addition & 1 deletion lib/utils/resolveObject.js
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ import { _resolveReferences } from './references/resolveReferences.js';
* @typedef {import('../../types/DesignToken.d.ts').TransformedTokens} TransformedTokens
* @typedef {import('../../types/DesignToken.d.ts').TransformedToken} TransformedToken
* @typedef {import('../../types/Config.d.ts').RegexOptions} RegexOptions
* @typedef {RegexOptions & {ignorePaths?: string[]; ignoreKeys?: string[]}} Options
* @typedef {RegexOptions & {ignorePaths?: string[]; ignoreKeys?: string[]; usesDtcg?: boolean }} Options
*/

const defaults = {
Expand Down
4 changes: 2 additions & 2 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

0 comments on commit d008c67

Please sign in to comment.