diff --git a/.changeset/happy-vans-end.md b/.changeset/happy-vans-end.md new file mode 100644 index 000000000..ee0827ca6 --- /dev/null +++ b/.changeset/happy-vans-end.md @@ -0,0 +1,5 @@ +--- +'style-dictionary': patch +--- + +Fix a couple of spots where DTCG option wasn't properly taken into account, more tests added. diff --git a/__tests__/exportPlatform.test.js b/__tests__/exportPlatform.test.js index 4b62e232f..83786e750 100644 --- a/__tests__/exportPlatform.test.js +++ b/__tests__/exportPlatform.test.js @@ -461,6 +461,10 @@ describe('exportPlatform', () => { }, }, }, + reftest: { $value: '{zero.$value}' }, + zero: { $value: '0' }, + reftest2: { $value: '{one}' }, + one: { $value: '1' }, }, transform: { 'custom/add/px': { @@ -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'); }); }); }); diff --git a/__tests__/utils/resolveObject.test.js b/__tests__/utils/resolveObject.test.js index 4364db158..c753b841d 100644 --- a/__tests__/utils/resolveObject.test.js +++ b/__tests__/utils/resolveObject.test.js @@ -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); + }); }); }); diff --git a/lib/StyleDictionary.js b/lib/StyleDictionary.js index 4155ee18f..a4267c3d5 100644 --- a/lib/StyleDictionary.js +++ b/lib/StyleDictionary.js @@ -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; @@ -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. diff --git a/lib/transform/token.js b/lib/transform/token.js index 629b292c4..7f952d99a 100644 --- a/lib/transform/token.js +++ b/lib/transform/token.js @@ -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; diff --git a/lib/utils/resolveObject.js b/lib/utils/resolveObject.js index 6041ad1d3..efb329f68 100644 --- a/lib/utils/resolveObject.js +++ b/lib/utils/resolveObject.js @@ -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 = { diff --git a/package-lock.json b/package-lock.json index d6fbc1d4b..72f0cf33c 100644 --- a/package-lock.json +++ b/package-lock.json @@ -1,12 +1,12 @@ { "name": "style-dictionary", - "version": "4.0.0-prerelease.12", + "version": "4.0.0-prerelease.15", "lockfileVersion": 3, "requires": true, "packages": { "": { "name": "style-dictionary", - "version": "4.0.0-prerelease.12", + "version": "4.0.0-prerelease.15", "hasInstallScript": true, "license": "Apache-2.0", "dependencies": {