diff --git a/.changeset/cuddly-cheetahs-drum.md b/.changeset/cuddly-cheetahs-drum.md new file mode 100644 index 000000000..fd8af976e --- /dev/null +++ b/.changeset/cuddly-cheetahs-drum.md @@ -0,0 +1,5 @@ +--- +'style-dictionary': minor +--- + +Rename `typeW3CDelegate` utility function to `typeDtcgDelegate`, as using "W3C" is highly discouraged when the standard isn't a W3C standard yet. diff --git a/.changeset/grumpy-peaches-flow.md b/.changeset/grumpy-peaches-flow.md index 346e948d9..280933454 100644 --- a/.changeset/grumpy-peaches-flow.md +++ b/.changeset/grumpy-peaches-flow.md @@ -2,4 +2,4 @@ 'style-dictionary': patch --- -Fix small issue in type w3c delegate utility type tracking. +Fix small issue in type DTCG delegate utility type tracking. diff --git a/.changeset/neat-zoos-reflect.md b/.changeset/neat-zoos-reflect.md index b0cce931a..330262c05 100644 --- a/.changeset/neat-zoos-reflect.md +++ b/.changeset/neat-zoos-reflect.md @@ -2,4 +2,4 @@ 'style-dictionary': minor --- -Support the use of "value"/"type"/"description" as token names or token group names, at the sacrifice of now no longer being able to combine non-W3C and W3C syntax within the same token dictionary. +Support the use of "value"/"type"/"description" as token names or token group names, at the sacrifice of now no longer being able to combine non-DTCG and DTCG syntax within the same token dictionary. diff --git a/.changeset/ninety-geckos-build.md b/.changeset/ninety-geckos-build.md index 16a3f4768..b08229325 100644 --- a/.changeset/ninety-geckos-build.md +++ b/.changeset/ninety-geckos-build.md @@ -2,4 +2,4 @@ 'style-dictionary': patch --- -Expose typeW3CDelegate utility. Don't take "value" into account anymore to determine that it's a design token, use $value. +Expose `typeDtcgDelegate` utility. Don't take `value` into account anymore to determine that it's a design token, use `$value`. diff --git a/.changeset/rich-pianos-walk.md b/.changeset/rich-pianos-walk.md index 188105b1f..09d4a97a4 100644 --- a/.changeset/rich-pianos-walk.md +++ b/.changeset/rich-pianos-walk.md @@ -2,4 +2,4 @@ 'style-dictionary': minor --- -Support W3C Draft specification for Design Tokens, by adding support for $value, $type and $description properties. +Support Design Token Community Group Draft specification for Design Tokens, by adding support for $value, $type and $description properties. diff --git a/CHANGELOG.md b/CHANGELOG.md index bdf7ca246..df58b780f 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -22,14 +22,14 @@ ### Patch Changes -- 0c1a36f: Fix small issue in type w3c delegate utility type tracking. -- 0c1a36f: Expose typeW3CDelegate utility. Don't take "value" into account anymore to determine that it's a design token, use $value. +- 0c1a36f: Fix small issue in type DTCG delegate utility type tracking. +- 0c1a36f: Expose `typeDtcgDelegate` utility. Don't take `value` into account anymore to determine that it's a design token, use `$value`. ## 4.0.0-prerelease.9 ### Minor Changes -- 294fd0e: Support W3C Draft specification for Design Tokens, by adding support for $value, $type and $description properties. +- 294fd0e: Support Design Token Community Group Draft specification for Design Tokens, by adding support for $value, $type and $description properties. ### Patch Changes diff --git a/__integration__/__snapshots__/customFormats.test.snap.js b/__integration__/__snapshots__/customFormats.test.snap.js index a65aa055a..48e2ea65f 100644 --- a/__integration__/__snapshots__/customFormats.test.snap.js +++ b/__integration__/__snapshots__/customFormats.test.snap.js @@ -963,7 +963,7 @@ snapshots["inline custom with new args should match snapshot"] = "options": { "otherOption": "Test", "showFileHeader": true, - "usesW3C": false + "usesDtcg": false } }`; /* end snapshot inline custom with new args should match snapshot */ @@ -1895,7 +1895,7 @@ snapshots["register custom format with new args should match snapshot"] = "options": { "otherOption": "Test", "showFileHeader": true, - "usesW3C": false + "usesDtcg": false } }`; /* end snapshot register custom format with new args should match snapshot */ diff --git a/__integration__/w3c-forward-compat.test.js b/__integration__/w3c-forward-compat.test.js index c62fae562..71a9d3fff 100644 --- a/__integration__/w3c-forward-compat.test.js +++ b/__integration__/w3c-forward-compat.test.js @@ -29,7 +29,7 @@ describe('integration', () => { * - $type special property & inherits from ancestors * - $description special property */ - describe('W3C DTCG draft spec forward compatibility', async () => { + describe('DTCG draft spec forward compatibility', async () => { const sd = new StyleDictionary({ tokens: { colors: { @@ -58,14 +58,14 @@ describe('integration', () => { type: 'value', matcher: (token) => token.$type === 'color', transformer: (token) => { - return Color(sd.options.usesW3C ? token.$value : token.value).toRgbString(); + return Color(sd.options.usesDtcg ? token.$value : token.value).toRgbString(); }, }, 'custom/add/px': { type: 'value', matcher: (token) => token.$type === 'dimension', transformer: (token) => { - return `${sd.options.usesW3C ? token.$value : token.value}px`; + return `${sd.options.usesDtcg ? token.$value : token.value}px`; }, }, }, diff --git a/__tests__/StyleDictionary.test.js b/__tests__/StyleDictionary.test.js index af781bedc..6ace4d45a 100644 --- a/__tests__/StyleDictionary.test.js +++ b/__tests__/StyleDictionary.test.js @@ -327,7 +327,7 @@ describe('StyleDictionary class + extend method', () => { expect(sd.tokens.dimensions.nope.$type).to.equal('spacing'); }); - it('should detect usage of W3C draft spec tokens', async () => { + it('should detect usage of DTCG draft spec tokens', async () => { const sd = new StyleDictionary({ tokens: { datalist: { @@ -337,6 +337,6 @@ describe('StyleDictionary class + extend method', () => { }, }); await sd.hasInitialized; - expect(sd.options.usesW3C).to.be.true; + expect(sd.options.usesDtcg).to.be.true; }); }); diff --git a/__tests__/common/formatHelpers/__snapshots__/createPropertyFormatter.test.snap.js b/__tests__/common/formatHelpers/__snapshots__/createPropertyFormatter.test.snap.js index 7a0b2aa62..31abc5d65 100644 --- a/__tests__/common/formatHelpers/__snapshots__/createPropertyFormatter.test.snap.js +++ b/__tests__/common/formatHelpers/__snapshots__/createPropertyFormatter.test.snap.js @@ -34,15 +34,15 @@ snapshots["common formatHelpers createPropertyFormatter commentStyle allows over $color-green: #00FF00;`; /* end snapshot common formatHelpers createPropertyFormatter commentStyle allows overriding formatting commentStyle 2 */ -snapshots["common formatHelpers createPropertyFormatter commentStyle supports W3C spec $description property for comments 1"] = +snapshots["common formatHelpers createPropertyFormatter commentStyle supports DTCG spec $description property for comments 1"] = ` --color-red: #FF0000; /* Foo bar qux red */`; -/* end snapshot common formatHelpers createPropertyFormatter commentStyle supports W3C spec $description property for comments 1 */ +/* end snapshot common formatHelpers createPropertyFormatter commentStyle supports DTCG spec $description property for comments 1 */ -snapshots["common formatHelpers createPropertyFormatter commentStyle supports W3C spec $description property for comments 2"] = +snapshots["common formatHelpers createPropertyFormatter commentStyle supports DTCG spec $description property for comments 2"] = ` --color-green: #00FF00; /* Foo bar qux green */`; -/* end snapshot common formatHelpers createPropertyFormatter commentStyle supports W3C spec $description property for comments 2 */ +/* end snapshot common formatHelpers createPropertyFormatter commentStyle supports DTCG spec $description property for comments 2 */ -snapshots["common formatHelpers createPropertyFormatter commentStyle supports W3C spec $description property for comments 3"] = +snapshots["common formatHelpers createPropertyFormatter commentStyle supports DTCG spec $description property for comments 3"] = ` /** * Foo * bar @@ -50,5 +50,5 @@ snapshots["common formatHelpers createPropertyFormatter commentStyle supports W3 * blue */ --color-blue: #0000FF;`; -/* end snapshot common formatHelpers createPropertyFormatter commentStyle supports W3C spec $description property for comments 3 */ +/* end snapshot common formatHelpers createPropertyFormatter commentStyle supports DTCG spec $description property for comments 3 */ diff --git a/__tests__/common/formatHelpers/createPropertyFormatter.test.js b/__tests__/common/formatHelpers/createPropertyFormatter.test.js index 705ba6e4c..dedbd464a 100644 --- a/__tests__/common/formatHelpers/createPropertyFormatter.test.js +++ b/__tests__/common/formatHelpers/createPropertyFormatter.test.js @@ -359,7 +359,7 @@ describe('common', () => { await expect(sassRed).to.matchSnapshot(2); }); - it('supports W3C spec $description property for comments', async () => { + it('supports DTCG spec $description property for comments', async () => { const descriptionDictionary = { color: { red: { diff --git a/__tests__/exportPlatform.test.js b/__tests__/exportPlatform.test.js index a87d61277..4b62e232f 100644 --- a/__tests__/exportPlatform.test.js +++ b/__tests__/exportPlatform.test.js @@ -209,7 +209,7 @@ describe('exportPlatform', () => { expect(dictionary.color.red.value.h).to.equal(20); }); - it('should handle .value and non .value references per the W3C spec', async () => { + it('should handle .value and non .value references per the DTCG spec', async () => { const tokens = { colors: { red: { value: '#f00' }, @@ -437,7 +437,7 @@ describe('exportPlatform', () => { }); }); - describe('w3c forward compatibility', () => { + describe('DTCG forward compatibility', () => { it('should allow using $value instead of value', async () => { const sd = new StyleDictionary({ tokens: { @@ -469,7 +469,7 @@ describe('exportPlatform', () => { return token.$type === 'dimension'; }, transformer: (token) => { - return `${sd.options.usesW3C ? token.$value : token.value}px`; + return `${sd.options.usesDtcg ? token.$value : token.value}px`; }, }, }, diff --git a/__tests__/utils/combineJSON.test.js b/__tests__/utils/combineJSON.test.js index d8741553f..f90ba66c2 100644 --- a/__tests__/utils/combineJSON.test.js +++ b/__tests__/utils/combineJSON.test.js @@ -18,20 +18,20 @@ import combineJSON from '../../lib/utils/combineJSON.js'; describe('utils', () => { describe('combineJSON', () => { - it('should return an object with usesW3C & tokens prop', async () => { + it('should return an object with usesDtcg & tokens prop', async () => { const test = await combineJSON(['__tests__/__json_files/*.json']); expect(typeof test).to.equal('object'); - const { tokens, usesW3C } = test; + const { tokens, usesDtcg } = test; expect(typeof tokens).to.equal('object'); - expect(typeof usesW3C).to.equal('boolean'); + expect(typeof usesDtcg).to.equal('boolean'); }); it('should handle wildcards', async () => { const test = await combineJSON(['__tests__/__json_files/*.json']); expect(typeof test).to.equal('object'); - const { tokens, usesW3C } = test; + const { tokens, usesDtcg } = test; expect(typeof tokens).to.equal('object'); - expect(typeof usesW3C).to.equal('boolean'); + expect(typeof usesDtcg).to.equal('boolean'); }); it('should handle js modules that export objects', async () => { @@ -39,9 +39,9 @@ describe('utils', () => { const relativePath = '__tests__/__json_files/*.js'; const test = await combineJSON([absPath, relativePath]); expect(typeof test).to.equal('object'); - const { tokens, usesW3C } = test; + const { tokens, usesDtcg } = test; expect(typeof tokens).to.equal('object'); - expect(typeof usesW3C).to.equal('boolean'); + expect(typeof usesDtcg).to.equal('boolean'); }); it('should do a deep merge', async () => { diff --git a/__tests__/utils/preprocess.test.js b/__tests__/utils/preprocess.test.js index 622616c7c..d203c3b68 100644 --- a/__tests__/utils/preprocess.test.js +++ b/__tests__/utils/preprocess.test.js @@ -11,10 +11,10 @@ * and limitations under the License. */ import { expect } from 'chai'; -import { typeW3CDelegate } from '../../lib/utils/preprocess.js'; +import { typeDtcgDelegate } from '../../lib/utils/preprocess.js'; describe('utils', () => { - describe('typeW3CDelegate', () => { + describe('typeDtcgDelegate', () => { it('should correctly let tokens inherit the $type property while respecting local overrides', () => { const tokens = { dimension: { @@ -52,7 +52,7 @@ describe('utils', () => { }, }; - expect(typeW3CDelegate(tokens)).to.eql({ + expect(typeDtcgDelegate(tokens)).to.eql({ dimension: { $type: 'dimension', scale: { diff --git a/docs/using_reference_utils.md b/docs/using_reference_utils.md index 2b966e15b..12cb50b49 100644 --- a/docs/using_reference_utils.md +++ b/docs/using_reference_utils.md @@ -51,7 +51,7 @@ const flat = flattenTokens(sd); */ ``` -> You can pass a second argument `usesW3C`, if set to true, the flattenTokens utility will assume W3C syntax (`$value` props) +> You can pass a second argument `usesDtcg`, if set to true, the flattenTokens utility will assume DTCG syntax (`$value` props) ### usesReference @@ -102,7 +102,7 @@ resolveReferences('solid {spacing.2} {colors.black}', sd.tokens); // alternative ``` > You can pass a third `options` argument where you can pass some configuration options for how references are resolved -> Most notable option for public usage is `usesW3C`, if set to true, the resolveReferences utility will assume W3C syntax (`$value` props) +> Most notable option for public usage is `usesDtcg`, if set to true, the resolveReferences utility will assume DTCG syntax (`$value` props) ### getReferences @@ -143,7 +143,7 @@ getReferences(sd, 'solid {spacing.2} {colors.black}'); // alternative way, yet i ``` > You can pass a third `options` argument where you can pass some configuration options for how references are resolved -> Most notable option for public usage is `usesW3C`, if set to true, the resolveReferences utility will assume W3C syntax (`$value` props) +> Most notable option for public usage is `usesDtcg`, if set to true, the resolveReferences utility will assume DTCG syntax (`$value` props) #### Complicated example @@ -273,12 +273,12 @@ export const SemanticBgPrimary = ColorsBlack; export const Border = `solid ${Spacing2} ${SemanticBgPrimary}`; ``` -> Note that the above example does not support W3C syntax, but this could be quite easily added, -> since you can query `sd.options.usesW3C` or inside a formatter functions `dictionary.options.usesW3C` +> Note that the above example does not support DTCG syntax, but this could be quite easily added, +> since you can query `sd.options.usesDtcg` or inside a formatter functions `dictionary.options.usesDtcg` -### typeW3CDelegate +### typeDtcgDelegate -This function processes your ["W3C Design Token Community Group Draft spec"-compliant](https://design-tokens.github.io/community-group/format/) dictionary of tokens, and ensures that `$type` inheritance is applied. +This function processes your ["Design Token Community Group Draft spec"-compliant](https://design-tokens.github.io/community-group/format/) dictionary of tokens, and ensures that `$type` inheritance is applied. We built this utility because it's cheaper to apply the inheritance once, rather than on every access of a token's "$type" property, checking the ancestor tree to find it. diff --git a/lib/StyleDictionary.js b/lib/StyleDictionary.js index 4985ce904..09766e8e6 100644 --- a/lib/StyleDictionary.js +++ b/lib/StyleDictionary.js @@ -26,7 +26,7 @@ import resolveObject from './utils/resolveObject.js'; import getName from './utils/references/getName.js'; import GroupMessages from './utils/groupMessages.js'; import flattenTokens from './utils/flattenTokens.js'; -import { detectW3CSyntax } from './utils/detectW3CSyntax.js'; +import { detectDtcgSyntax } from './utils/detectDtcgSyntax.js'; import { preprocess } from './utils/preprocess.js'; import transformObject from './transform/object.js'; @@ -178,11 +178,11 @@ export default class StyleDictionary extends Register { }); this.options = options; - let { usesW3C } = this.options; + let { usesDtcg } = this.options; - // Try to detect W3C if not specified by user in options - if (this.tokens && usesW3C === undefined) { - usesW3C = detectW3CSyntax(this.tokens); + // Try to detect DTCG if not specified by user in options + if (Object.entries(this.tokens).length > 0 && usesDtcg === undefined) { + usesDtcg = detectDtcgSyntax(this.tokens); } // grab the inline tokens, ones either defined in the configuration object // or that already exist from extending another style dictionary instance @@ -199,13 +199,13 @@ export default class StyleDictionary extends Register { undefined, false, this.parsers, - this.options.usesW3C, + this.options.usesDtcg, ); includeTokens = result.tokens; - // If it wasn't known yet whether W3C is used, combineJSON util will have auto-detected it - if (usesW3C === undefined) { - usesW3C = result.usesW3C; + // If it wasn't known yet whether DTCG is used, combineJSON util will have auto-detected it + if (usesDtcg === undefined) { + usesDtcg = result.usesDtcg; } } @@ -228,13 +228,13 @@ export default class StyleDictionary extends Register { }, true, this.parsers, - this.options.usesW3C, + this.options.usesDtcg, ); sourceTokens = result.tokens; - // If it wasn't known yet whether W3C is used, combineJSON util will have auto-detected it - if (usesW3C === undefined) { - usesW3C = result.usesW3C; + // If it wasn't known yet whether DTCG is used, combineJSON util will have auto-detected it + if (usesDtcg === undefined) { + usesDtcg = result.usesDtcg; } if (GroupMessages.count(PROPERTY_VALUE_COLLISIONS) > 0) { @@ -252,7 +252,7 @@ export default class StyleDictionary extends Register { // Merge inline, include, and source tokens const unprocessedTokens = deepExtend([{}, inlineTokens, includeTokens, sourceTokens]); this.tokens = await preprocess(unprocessedTokens, this.preprocessors); - this.options = { ...this.options, usesW3C }; + this.options = { ...this.options, usesDtcg }; this.hasInitializedResolve(null); // For chaining @@ -398,7 +398,7 @@ export default class StyleDictionary extends Register { // replacing that value in the string. const tokens = await this.exportPlatform(platform); this.allTokens = /** @type {TransformedToken[]} */ ( - flattenTokens(tokens, this.options.usesW3C) + flattenTokens(tokens, this.options.usesDtcg) ); // This is the dictionary object we pass to the file // building and action methods. diff --git a/lib/buildFile.js b/lib/buildFile.js index f086aa240..897ee260e 100644 --- a/lib/buildFile.js +++ b/lib/buildFile.js @@ -100,7 +100,7 @@ export default function buildFile(file, platform = {}, dictionary, options) { let collisions = nameCollisionObj[tokenName] .map((token) => { let tokenPathText = chalk.rgb(255, 69, 0)(token.path.join('.')); - let valueText = chalk.rgb(255, 140, 0)(options.usesW3C ? token.$value : token.value); + let valueText = chalk.rgb(255, 140, 0)(options.usesDtcg ? token.$value : token.value); return tokenPathText + ' ' + valueText; }) .join('\n '); diff --git a/lib/common/actions.js b/lib/common/actions.js index d100b0e18..078e7c6a8 100644 --- a/lib/common/actions.js +++ b/lib/common/actions.js @@ -42,7 +42,7 @@ export default { const dir = `${imagesDir}${token.attributes.state}`; const path = `${dir}/${name}.png`; fs.mkdirSync(dir, { recursive: true }); - fs.copyFileSync(options.usesW3C ? token.$value : token.value, path); + fs.copyFileSync(options.usesDtcg ? token.$value : token.value, path); } }); }, diff --git a/lib/common/formatHelpers/createPropertyFormatter.js b/lib/common/formatHelpers/createPropertyFormatter.js index 4c92706e4..6bd25c57c 100644 --- a/lib/common/formatHelpers/createPropertyFormatter.js +++ b/lib/common/formatHelpers/createPropertyFormatter.js @@ -113,7 +113,7 @@ function addComment(to_ret_token, comment, options) { * @param {string} [options.format] - Available formats are: 'css', 'sass', 'less', and 'stylus'. If you want to customize the format and can't use one of those predefined formats, use the `formatting` option * @param {Formatting} [options.formatting] - Custom formatting properties that define parts of a declaration line in code. The configurable strings are: prefix, indentation, separator, suffix, and commentStyle. Those are used to generate a line like this: `${indentation}${prefix}${token.name}${separator} ${token.value}${suffix}` * @param {boolean} [options.themeable] [false] - Whether tokens should default to being themeable. - * @param {boolean} [options.usesW3C] [false] - Whether W3C token syntax should be uses. + * @param {boolean} [options.usesDtcg] [false] - Whether DTCG token syntax should be uses. * @returns {(token: import('../../../types/DesignToken.d.ts').TransformedToken) => string} */ export default function createPropertyFormatter({ @@ -123,7 +123,7 @@ export default function createPropertyFormatter({ format, formatting = {}, themeable = false, - usesW3C = false, + usesDtcg = false, }) { /** @type {Formatting} */ const formatDefaults = {}; @@ -161,7 +161,7 @@ export default function createPropertyFormatter({ const { tokens, unfilteredTokens } = dictionary; return function (token) { let to_ret_token = `${indentation}${prefix}${token.name}${separator} `; - let value = usesW3C ? token.$value : token.value; + let value = usesDtcg ? token.$value : token.value; /** * A single value can have multiple references either by interpolation: @@ -200,8 +200,8 @@ export default function createPropertyFormatter({ // because Style Dictionary resolved this in the resolution step. // Here we are undoing that by replacing the value with // the reference's name - if (Object.hasOwn(ref, `${usesW3C ? '$' : ''}value`) && Object.hasOwn(ref, 'name')) { - const refVal = usesW3C ? ref.$value : ref.value; + if (Object.hasOwn(ref, `${usesDtcg ? '$' : ''}value`) && Object.hasOwn(ref, 'name')) { + const refVal = usesDtcg ? ref.$value : ref.value; const replaceFunc = function () { if (format === 'css') { if (outputReferenceFallbacks) { diff --git a/lib/common/formatHelpers/formattedVariables.js b/lib/common/formatHelpers/formattedVariables.js index b0a64f282..ffc4208fc 100644 --- a/lib/common/formatHelpers/formattedVariables.js +++ b/lib/common/formatHelpers/formattedVariables.js @@ -36,7 +36,7 @@ const defaultFormatting = { * @param {Boolean} [options.outputReferences] - Whether or not to output references * @param {Formatting} [options.formatting] - Custom formatting properties that define parts of a declaration line in code. This will get passed to `formatHelpers.createPropertyFormatter` and used for the `lineSeparator` between lines of code. * @param {Boolean} [options.themeable] [false] - Whether tokens should default to being themeable. - * @param {boolean} [options.usesW3C] [false] - Whether W3C token syntax should be uses. + * @param {boolean} [options.usesDtcg] [false] - Whether DTCG token syntax should be uses. * @returns {String} * @example * ```js @@ -58,7 +58,7 @@ export default function formattedVariables({ outputReferences = false, formatting = {}, themeable = false, - usesW3C = false, + usesDtcg = false, }) { // typecast, we know that by know the tokens have been transformed let allTokens = /** @type {Token[]} */ (dictionary.allTokens); @@ -89,7 +89,7 @@ export default function formattedVariables({ format, formatting, themeable, - usesW3C, + usesDtcg, }), ) .filter(function (strVal) { diff --git a/lib/common/formatHelpers/iconsWithPrefix.js b/lib/common/formatHelpers/iconsWithPrefix.js index d390931d8..3625b8307 100644 --- a/lib/common/formatHelpers/iconsWithPrefix.js +++ b/lib/common/formatHelpers/iconsWithPrefix.js @@ -47,7 +47,7 @@ export default function iconsWithPrefix(prefix, allTokens, options, platform) { }) .map(function (token) { const varName = - prefix + token.name + ': ' + (options.usesW3C ? token.$value : token.value) + ';'; + prefix + token.name + ': ' + (options.usesDtcg ? token.$value : token.value) + ';'; const className = '.' + platform.prefix + '-icon.' + token.attributes?.item + ':before '; const declaration = '{ content: ' + prefix + token.name + '; }'; return varName + '\n' + className + declaration; diff --git a/lib/common/formatHelpers/minifyDictionary.js b/lib/common/formatHelpers/minifyDictionary.js index fdb12d5f2..88b2896f8 100644 --- a/lib/common/formatHelpers/minifyDictionary.js +++ b/lib/common/formatHelpers/minifyDictionary.js @@ -20,7 +20,7 @@ * @memberof module:formatHelpers * @name minifyDictionary * @param {Tokens} obj - The object to minify. You will most likely pass `dictionary.tokens` to it. - * @param {boolean} [usesW3C] - Whether or not tokens are using W3C syntax. + * @param {boolean} [usesDtcg] - Whether or not tokens are using DTCG syntax. * @returns {Tokens} * @example * ```js @@ -32,7 +32,7 @@ * }); * ``` */ -export default function minifyDictionary(obj, usesW3C = false) { +export default function minifyDictionary(obj, usesDtcg = false) { if (typeof obj !== 'object' || Array.isArray(obj)) { return obj; } @@ -40,12 +40,12 @@ export default function minifyDictionary(obj, usesW3C = false) { /** @type {Tokens} */ const toRet = {}; - if (Object.hasOwn(obj, `${usesW3C ? '$' : ''}value`)) { - return usesW3C ? obj.$value : obj.value; + if (Object.hasOwn(obj, `${usesDtcg ? '$' : ''}value`)) { + return usesDtcg ? obj.$value : obj.value; } else { for (const name in obj) { if (Object.hasOwn(obj, name)) { - toRet[name] = minifyDictionary(obj[name], usesW3C); + toRet[name] = minifyDictionary(obj[name], usesDtcg); } } } diff --git a/lib/common/formats.js b/lib/common/formats.js index 2c28868b6..b890ab74f 100644 --- a/lib/common/formats.js +++ b/lib/common/formats.js @@ -83,11 +83,11 @@ const formats = { */ 'css/variables': function ({ dictionary, options = {}, file }) { const selector = options.selector ? options.selector : `:root`; - const { outputReferences, usesW3C } = options; + const { outputReferences, usesDtcg } = options; return ( fileHeader({ file }) + `${selector} {\n` + - formattedVariables({ format: 'css', dictionary, outputReferences, usesW3C }) + + formattedVariables({ format: 'css', dictionary, outputReferences, usesDtcg }) + `\n}\n` ); }, @@ -144,11 +144,11 @@ const formats = { const mapTemplate = _template(scssMapDeep); // Default the "themeable" option to true for backward compatibility. - const { outputReferences, themeable = true, usesW3C } = options; + const { outputReferences, themeable = true, usesDtcg } = options; return ( '\n' + fileHeader({ file, commentStyle: 'long' }) + - formattedVariables({ format: 'sass', dictionary, outputReferences, themeable, usesW3C }) + + formattedVariables({ format: 'sass', dictionary, outputReferences, themeable, usesDtcg }) + '\n' + mapTemplate({ dictionary, file, options }) ); @@ -173,10 +173,10 @@ const formats = { * ``` */ 'scss/variables': function ({ dictionary, options, file }) { - const { outputReferences, themeable = false, usesW3C } = options; + const { outputReferences, themeable = false, usesDtcg } = options; return ( fileHeader({ file, commentStyle: 'short' }) + - formattedVariables({ format: 'sass', dictionary, outputReferences, themeable, usesW3C }) + + formattedVariables({ format: 'sass', dictionary, outputReferences, themeable, usesDtcg }) + '\n' ); }, @@ -216,10 +216,10 @@ const formats = { * ``` */ 'less/variables': function ({ dictionary, options, file }) { - const { outputReferences, usesW3C } = options; + const { outputReferences, usesDtcg } = options; return ( fileHeader({ file, commentStyle: 'short' }) + - formattedVariables({ format: 'less', dictionary, outputReferences, usesW3C }) + + formattedVariables({ format: 'less', dictionary, outputReferences, usesDtcg }) + '\n' ); }, @@ -256,10 +256,10 @@ const formats = { * ``` */ 'stylus/variables': function ({ dictionary, options, file }) { - const { outputReferences, usesW3C } = options; + const { outputReferences, usesDtcg } = options; return ( fileHeader({ file, commentStyle: 'short' }) + - formattedVariables({ format: 'stylus', dictionary, outputReferences, usesW3C }) + + formattedVariables({ format: 'stylus', dictionary, outputReferences, usesDtcg }) + '\n' ); }, @@ -313,7 +313,7 @@ const formats = { dictionary.allTokens .map(function (token) { return ` "${token.name}": ${JSON.stringify( - options.usesW3C ? token.$value : token.value, + options.usesDtcg ? token.$value : token.value, )}`; }) .join(',\n') + @@ -452,7 +452,7 @@ const formats = { 'export const ' + token.name + ' = ' + - JSON.stringify(options.usesW3C ? token.$value : token.value) + + JSON.stringify(options.usesDtcg ? token.$value : token.value) + ';'; if (token.comment) to_ret = to_ret.concat(' // ' + token.comment); return to_ret; @@ -506,7 +506,7 @@ const formats = { 'export const ' + token.name + ' : ' + - getTypeScriptType(options.usesW3C ? token.$value : token.value, options) + + getTypeScriptType(options.usesDtcg ? token.$value : token.value, options) + ';'; return to_ret_token; }) @@ -577,7 +577,7 @@ const formats = { */ function treeWalker(obj) { let type = Object.create(null); - const propToCheck = options.usesW3C ? '$value' : 'value'; + const propToCheck = options.usesDtcg ? '$value' : 'value'; if (Object.hasOwn(obj, propToCheck)) { type = 'DesignToken'; } else { @@ -594,7 +594,7 @@ const formats = { // TODO: find a browser+node compatible way to read from '../../types/DesignToken.d.ts' const designTokenInterface = `interface DesignToken { - ${options.usesW3C ? '$' : ''}value?: any; + ${options.usesDtcg ? '$' : ''}value?: any; name?: string; comment?: string; themeable?: boolean; @@ -834,7 +834,7 @@ declare const ${moduleName}: ${JSON.stringify(treeWalker(dictionary.tokens), nul 'compose/object': function ({ dictionary, options, file }) { const { allTokens, tokens, unfilteredTokens } = dictionary; const template = _template(composeObject); - const { outputReferences, usesW3C } = options; + const { outputReferences, usesDtcg } = options; const formatProperty = createPropertyFormatter({ outputReferences, dictionary, @@ -842,7 +842,7 @@ declare const ${moduleName}: ${JSON.stringify(treeWalker(dictionary.tokens), nul suffix: '', commentStyle: 'none', // We will add the comment in the format template }, - usesW3C, + usesDtcg, }); let sortedTokens; @@ -1028,7 +1028,7 @@ declare const ${moduleName}: ${JSON.stringify(treeWalker(dictionary.tokens), nul 'ios-swift/class.swift': function ({ dictionary, options, file, platform }) { const { allTokens, tokens, unfilteredTokens } = dictionary; const template = _template(iosSwiftAny); - const { outputReferences, usesW3C } = options; + const { outputReferences, usesDtcg } = options; options = setSwiftFileProperties(options, 'class', platform.transformGroup); const formatProperty = createPropertyFormatter({ outputReferences, @@ -1036,7 +1036,7 @@ declare const ${moduleName}: ${JSON.stringify(treeWalker(dictionary.tokens), nul formatting: { suffix: '', }, - usesW3C, + usesDtcg, }); let sortedTokens; @@ -1070,7 +1070,7 @@ declare const ${moduleName}: ${JSON.stringify(treeWalker(dictionary.tokens), nul 'ios-swift/enum.swift': function ({ dictionary, options, file, platform }) { const { allTokens, tokens, unfilteredTokens } = dictionary; const template = _template(iosSwiftAny); - const { outputReferences, usesW3C } = options; + const { outputReferences, usesDtcg } = options; options = setSwiftFileProperties(options, 'enum', platform.transformGroup); const formatProperty = createPropertyFormatter({ outputReferences, @@ -1078,7 +1078,7 @@ declare const ${moduleName}: ${JSON.stringify(treeWalker(dictionary.tokens), nul formatting: { suffix: '', }, - usesW3C, + usesDtcg, }); let sortedTokens; @@ -1122,7 +1122,7 @@ declare const ${moduleName}: ${JSON.stringify(treeWalker(dictionary.tokens), nul 'ios-swift/any.swift': function ({ dictionary, options, file, platform }) { const { allTokens, tokens, unfilteredTokens } = dictionary; const template = _template(iosSwiftAny); - const { outputReferences, usesW3C } = options; + const { outputReferences, usesDtcg } = options; options = setSwiftFileProperties(options, options.objectType, platform.transformGroup); const formatProperty = createPropertyFormatter({ outputReferences, @@ -1130,7 +1130,7 @@ declare const ${moduleName}: ${JSON.stringify(treeWalker(dictionary.tokens), nul formatting: { suffix: '', }, - usesW3C, + usesDtcg, }); let sortedTokens; @@ -1219,7 +1219,7 @@ declare const ${moduleName}: ${JSON.stringify(treeWalker(dictionary.tokens), nul * ``` */ 'json/nested': function ({ dictionary, options }) { - return JSON.stringify(minifyDictionary(dictionary.tokens, options.usesW3C), null, 2) + '\n'; + return JSON.stringify(minifyDictionary(dictionary.tokens, options.usesDtcg), null, 2) + '\n'; }, /** @@ -1241,7 +1241,7 @@ declare const ${moduleName}: ${JSON.stringify(treeWalker(dictionary.tokens), nul dictionary.allTokens .map(function (token) { return ` "${token.name}": ${JSON.stringify( - options.usesW3C ? token.$value : token.value, + options.usesDtcg ? token.$value : token.value, )}`; }) .join(',\n') + @@ -1281,7 +1281,7 @@ declare const ${moduleName}: ${JSON.stringify(treeWalker(dictionary.tokens), nul return token.attributes?.category === 'color' && token.attributes.type === 'base'; }) .map(function (token) { - return options.usesW3C ? token.$value : token.value; + return options.usesDtcg ? token.$value : token.value; }); return JSON.stringify(to_ret, null, 2) + '\n'; }, @@ -1317,7 +1317,7 @@ declare const ${moduleName}: ${JSON.stringify(treeWalker(dictionary.tokens), nul { name: token.name, }, - options.usesW3C ? token.$value : token.value, + options.usesDtcg ? token.$value : token.value, ); }), }; @@ -1349,11 +1349,11 @@ declare const ${moduleName}: ${JSON.stringify(treeWalker(dictionary.tokens), nul 'flutter/class.dart': function ({ dictionary, options, file }) { const { allTokens, tokens, unfilteredTokens } = dictionary; const template = _template(flutterClassDart); - const { outputReferences, usesW3C } = options; + const { outputReferences, usesDtcg } = options; const formatProperty = createPropertyFormatter({ outputReferences, dictionary, - usesW3C, + usesDtcg, }); let sortedTokens; diff --git a/lib/common/templates/android/colors.template.js b/lib/common/templates/android/colors.template.js index 11a8bb141..bb3267262 100644 --- a/lib/common/templates/android/colors.template.js +++ b/lib/common/templates/android/colors.template.js @@ -21,6 +21,6 @@ const tokens = dictionary.allTokens.filter(function(token) { <%= fileHeader({file, commentStyle: 'xml'}) %> <% tokens.forEach(function(token) { - %><%= options.usesW3C ? token.$value : token.value %><% if (token.comment) { %><% } %> + %><%= options.usesDtcg ? token.$value : token.value %><% if (token.comment) { %><% } %> <% }); %> `; diff --git a/lib/common/templates/android/dimens.template.js b/lib/common/templates/android/dimens.template.js index fc29ce4ee..7b5338f5e 100644 --- a/lib/common/templates/android/dimens.template.js +++ b/lib/common/templates/android/dimens.template.js @@ -20,6 +20,6 @@ const tokens = dictionary.allTokens.filter(function(token) { <%= fileHeader({file, commentStyle: 'xml'}) %> <% tokens.forEach(function(token) { - %><%= options.usesW3C ? token.$value : token.value %><% if (token.comment) { %><% } %> + %><%= options.usesDtcg ? token.$value : token.value %><% if (token.comment) { %><% } %> <% }); %> `; diff --git a/lib/common/templates/android/fontDimens.template.js b/lib/common/templates/android/fontDimens.template.js index 793b6ff39..6da278f56 100644 --- a/lib/common/templates/android/fontDimens.template.js +++ b/lib/common/templates/android/fontDimens.template.js @@ -20,6 +20,6 @@ const tokens = dictionary.allTokens.filter(function(token) { <%= fileHeader({file, commentStyle: 'xml'}) %> <% tokens.forEach(function(token) { - %><%= options.usesW3C ? token.$value : token.value %><% if (token.comment) { %><% } %> + %><%= options.usesDtcg ? token.$value : token.value %><% if (token.comment) { %><% } %> <% }); %> `; diff --git a/lib/common/templates/android/integers.template.js b/lib/common/templates/android/integers.template.js index bb4163eee..1bbafe3fd 100644 --- a/lib/common/templates/android/integers.template.js +++ b/lib/common/templates/android/integers.template.js @@ -20,6 +20,6 @@ var tokens = dictionary.allTokens.filter(function(token) { <%= fileHeader({file, commentStyle: 'xml'}) %> <% tokens.forEach(function(token) { - %><%= options.usesW3C ? token.$value : token.value %><% if (token.comment) { %><% } %> + %><%= options.usesDtcg ? token.$value : token.value %><% if (token.comment) { %><% } %> <% }); %> `; diff --git a/lib/common/templates/android/resources.template.js b/lib/common/templates/android/resources.template.js index 061a5b0ed..25ac45bb3 100644 --- a/lib/common/templates/android/resources.template.js +++ b/lib/common/templates/android/resources.template.js @@ -67,7 +67,7 @@ export default (opts) => { if (file?.options && file.options.outputReferences && usesReferences(token.original.value)) { return `@${tokenToType(token)}/${getReferences(token.original.value, tokens)[0].name}`; } else { - return options.usesW3C ? token.$value : token.value; + return options.usesDtcg ? token.$value : token.value; } } diff --git a/lib/common/templates/android/strings.template.js b/lib/common/templates/android/strings.template.js index 3b8654e5c..baaaa3a38 100644 --- a/lib/common/templates/android/strings.template.js +++ b/lib/common/templates/android/strings.template.js @@ -20,6 +20,6 @@ const tokens = dictionary.allTokens.filter(function(token) { <%= fileHeader({file, commentStyle: 'xml'}) %> <% tokens.forEach(function(token) { - %><%= options.usesW3C ? token.$value : token.value %><% if (token.comment) { %><% } %> + %><%= options.usesDtcg ? token.$value : token.value %><% if (token.comment) { %><% } %> <% }); %> `; diff --git a/lib/common/templates/ios/colors.m.template.js b/lib/common/templates/ios/colors.m.template.js index 7638e01ec..e2cf30054 100644 --- a/lib/common/templates/ios/colors.m.template.js +++ b/lib/common/templates/ios/colors.m.template.js @@ -31,7 +31,7 @@ export default `<% dispatch_once(&onceToken, ^{ colorArray = @[ -<%= dictionary.allTokens.map(function(token){ return options.usesW3C ? token.$value : token.value; }).join(',\\n') %> +<%= dictionary.allTokens.map(function(token){ return options.usesDtcg ? token.$value : token.value; }).join(',\\n') %> ]; }); diff --git a/lib/common/templates/ios/macros.template.js b/lib/common/templates/ios/macros.template.js index b6fac583b..2af7fa2dd 100644 --- a/lib/common/templates/ios/macros.template.js +++ b/lib/common/templates/ios/macros.template.js @@ -21,5 +21,5 @@ export default `<% #import <% dictionary.allTokens.forEach(function(token) { - %>#define <%= token.name %> <%= options.usesW3C ? token.$value : token.value %> + %>#define <%= token.name %> <%= options.usesDtcg ? token.$value : token.value %> <% }); %>`; diff --git a/lib/common/templates/ios/plist.template.js b/lib/common/templates/ios/plist.template.js index e36f50eb2..81bcc7462 100644 --- a/lib/common/templates/ios/plist.template.js +++ b/lib/common/templates/ios/plist.template.js @@ -28,18 +28,18 @@ const tokens = dictionary.allTokens.filter(function(token) { %><%= token.name %> <% if (token.attributes.category === 'color') { %> r - <%= (options.usesW3C ? token.$value : token.value)[0]/255 %> + <%= (options.usesDtcg ? token.$value : token.value)[0]/255 %> g - <%= (options.usesW3C ? token.$value : token.value)[1]/255 %> + <%= (options.usesDtcg ? token.$value : token.value)[1]/255 %> b - <%= (options.usesW3C ? token.$value : token.value)[2]/255 %> + <%= (options.usesDtcg ? token.$value : token.value)[2]/255 %> a 1 <% } else if (token.attributes.category === 'size') { %> - <%= options.usesW3C ? token.$value : token.value %> + <%= options.usesDtcg ? token.$value : token.value %> <% } else { %> - <%= options.usesW3C ? token.$value : token.value %> + <%= options.usesDtcg ? token.$value : token.value %> <% } %><% if (token.comment) { %><% } %><% }); %> `; diff --git a/lib/common/templates/ios/singleton.m.template.js b/lib/common/templates/ios/singleton.m.template.js index 6cfb9e4f9..310e979c4 100644 --- a/lib/common/templates/ios/singleton.m.template.js +++ b/lib/common/templates/ios/singleton.m.template.js @@ -45,8 +45,8 @@ export default `<% <% function buildDictionary(token, indent) { indent = indent || ' '; var to_ret = '@{\\n'; - if (Object.hasOwn(token, \`\${options.usesW3C ? '$' : ''}value\`)) { - let value = options.usesW3C ? token.$value : token.value; + if (Object.hasOwn(token, \`\${options.usesDtcg ? '$' : ''}value\`)) { + let value = options.usesDtcg ? token.$value : token.value; if (token.attributes.category === 'size' || token.attributes.category === 'time') { value = '@' + value; } diff --git a/lib/common/templates/ios/static.m.template.js b/lib/common/templates/ios/static.m.template.js index 64d2d1162..6df61b0f4 100644 --- a/lib/common/templates/ios/static.m.template.js +++ b/lib/common/templates/ios/static.m.template.js @@ -20,4 +20,4 @@ export default `<% #import "<%= file.className %>.h" <% dictionary.allTokens.forEach(function(token) { %> -<%= file.type %> const <%= token.name %> = <%= options.usesW3C ? token.$value : token.value %>;<% }); %>`; +<%= file.type %> const <%= token.name %> = <%= options.usesDtcg ? token.$value : token.value %>;<% }); %>`; diff --git a/lib/common/templates/ios/strings.m.template.js b/lib/common/templates/ios/strings.m.template.js index 33a3e025c..4eda91277 100644 --- a/lib/common/templates/ios/strings.m.template.js +++ b/lib/common/templates/ios/strings.m.template.js @@ -20,7 +20,7 @@ export default `<% #import "<%= file.className %>.h" <% dictionary.allTokens.forEach(function(token) { %> -NSString * const <%= token.name %> = <%= options.usesW3C ? token.$value : token.value %>;<% }); %> +NSString * const <%= token.name %> = <%= options.usesDtcg ? token.$value : token.value %>;<% }); %> @implementation <%= file.className %> @@ -41,7 +41,7 @@ NSString * const <%= token.name %> = <%= options.usesW3C ? token.$value : token. <% function buildProperty(token) { let to_ret = '@{\\n'; - to_ret += ' ' + '@"value": ' + (options.usesW3C ? token.$value : token.value) + ',\\n'; + to_ret += ' ' + '@"value": ' + (options.usesDtcg ? token.$value : token.value) + ',\\n'; to_ret += ' ' + '@"name": @"' + token.name + '",\\n'; for(const name in token.attributes) { diff --git a/lib/common/templates/scss/map-deep.template.js b/lib/common/templates/scss/map-deep.template.js index cb1a9cd41..a2210a4db 100644 --- a/lib/common/templates/scss/map-deep.template.js +++ b/lib/common/templates/scss/map-deep.template.js @@ -27,7 +27,7 @@ export default `<% output += \`''\` } else if (typeof obj === "string") { output += \`'\${obj}'\` - } else if (Object.hasOwn(obj, \`\${options.usesW3C ? '$' : ''}value\`)) { + } else if (Object.hasOwn(obj, \`\${options.usesDtcg ? '$' : ''}value\`)) { // if we have found a leaf (a property with a value) append the value output += \`$\${obj.name}\`; } else { diff --git a/lib/common/templates/scss/map-flat.template.js b/lib/common/templates/scss/map-flat.template.js index f33eddd9c..2822e6988 100644 --- a/lib/common/templates/scss/map-flat.template.js +++ b/lib/common/templates/scss/map-flat.template.js @@ -22,7 +22,7 @@ export default `<% if(token.comment) { line += ' // ' + token.comment + '\\n'; } - const value = options.usesW3C ? token.$value : token.value; + const value = options.usesDtcg ? token.$value : token.value; line += ' \\'' + token.name + '\\': ' + (token.attributes.category==='asset' ? '"' + value + '"' : value) return line; }).join(',\\n'); diff --git a/lib/common/templates/static-style-guide/index.html.template.js b/lib/common/templates/static-style-guide/index.html.template.js index 107168a7a..e2600c08b 100644 --- a/lib/common/templates/static-style-guide/index.html.template.js +++ b/lib/common/templates/static-style-guide/index.html.template.js @@ -17,7 +17,7 @@ export default `<% const checkForStyle = function(token) { let toStyle = ""; - const value = options.usesW3C ? token.$value : token.value; + const value = options.usesDtcg ? token.$value : token.value; if(value.match(colorRegex)) { if(token.path.indexOf('font')>-1) { toStyle += "color:" + value + ";"; @@ -66,7 +66,7 @@ export default `<% <% if(token.attributes && JSON.stringify(token.attributes)!=="{}") { %>
<% } %> -
<%= options.usesW3C ? token.$value : token.value %><% if(token.attributes.category === 'content' && token.attributes.type === 'icon') { %><%= options.usesW3C ? token.$value : token.value %><% } %>
+
<%= options.usesDtcg ? token.$value : token.value %><% if(token.attributes.category === 'content' && token.attributes.type === 'icon') { %><%= options.usesDtcg ? token.$value : token.value %><% } %>
<% if(token.attributes && JSON.stringify(token.attributes)!=="{}") { %>
<%= JSON.stringify(token.attributes) %>
<% } %> diff --git a/lib/common/transforms.js b/lib/common/transforms.js index f6e2e65af..e879a3841 100644 --- a/lib/common/transforms.js +++ b/lib/common/transforms.js @@ -95,7 +95,7 @@ function isContent(token) { * @returns {string} */ function wrapValueWith(character, token, options) { - return `${character}${options.usesW3C ? token.$value : token.value}${character}`; + return `${character}${options.usesDtcg ? token.$value : token.value}${character}`; } /** @@ -183,7 +183,7 @@ export default { type: 'attribute', matcher: isColor, transformer: function (token, _, options) { - const color = Color(options.usesW3C ? token.$value : token.value); + const color = Color(options.usesDtcg ? token.$value : token.value); return { hex: color.toHex(), rgb: color.toRgb(), @@ -369,7 +369,7 @@ export default { type: 'value', matcher: isColor, transformer: function (token, _, options) { - return Color(options.usesW3C ? token.$value : token.value).toRgbString(); + return Color(options.usesDtcg ? token.$value : token.value).toRgbString(); }, }, @@ -389,7 +389,7 @@ export default { type: 'value', matcher: isColor, transformer: function (token, _, options) { - return Color(options.usesW3C ? token.$value : token.value).toHslString(); + return Color(options.usesDtcg ? token.$value : token.value).toHslString(); }, }, @@ -409,7 +409,7 @@ export default { type: 'value', matcher: isColor, transformer: function (token, _, options) { - const color = Color(options.usesW3C ? token.$value : token.value); + const color = Color(options.usesDtcg ? token.$value : token.value); const o = color.toHsl(); const vals = `${Math.round(o.h)} ${Math.round(o.s * 100)}% ${Math.round(o.l * 100)}%`; if (color.getAlpha() === 1) { @@ -435,7 +435,7 @@ export default { type: 'value', matcher: isColor, transformer: function (token, _, options) { - return Color(options.usesW3C ? token.$value : token.value).toHexString(); + return Color(options.usesDtcg ? token.$value : token.value).toHexString(); }, }, @@ -454,7 +454,7 @@ export default { type: 'value', matcher: isColor, transformer: function (token, _, options) { - return Color(options.usesW3C ? token.$value : token.value).toHex8String(); + return Color(options.usesDtcg ? token.$value : token.value).toHex8String(); }, }, @@ -473,7 +473,7 @@ export default { type: 'value', matcher: isColor, transformer: function (token, _, options) { - const str = Color(options.usesW3C ? token.$value : token.value).toHex8(); + const str = Color(options.usesDtcg ? token.$value : token.value).toHex8(); return '#' + str.slice(6) + str.slice(0, 6); }, }, @@ -493,7 +493,7 @@ export default { type: 'value', matcher: isColor, transformer: function (token, _, options) { - const str = Color(options.usesW3C ? token.$value : token.value).toHex8(); + const str = Color(options.usesDtcg ? token.$value : token.value).toHex8(); return 'Color(0x' + str.slice(6) + str.slice(0, 6) + ')'; }, }, @@ -513,7 +513,7 @@ export default { type: 'value', matcher: isColor, transformer: function (token, _, options) { - const rgb = Color(options.usesW3C ? token.$value : token.value).toRgb(); + const rgb = Color(options.usesDtcg ? token.$value : token.value).toRgb(); return ( '[UIColor colorWithRed:' + (rgb.r / 255).toFixed(3) + @@ -546,7 +546,7 @@ export default { type: 'value', matcher: isColor, transformer: function (token, _, options) { - const { r, g, b, a } = Color(options.usesW3C ? token.$value : token.value).toRgb(); + const { r, g, b, a } = Color(options.usesDtcg ? token.$value : token.value).toRgb(); const rFixed = (r / 255.0).toFixed(3); const gFixed = (g / 255.0).toFixed(3); const bFixed = (b / 255.0).toFixed(3); @@ -569,7 +569,7 @@ export default { type: 'value', matcher: isColor, transformer: function (token, _, options) { - const { r, g, b, a } = Color(options.usesW3C ? token.$value : token.value).toRgb(); + const { r, g, b, a } = Color(options.usesDtcg ? token.$value : token.value).toRgb(); const rFixed = (r / 255.0).toFixed(3); const gFixed = (g / 255.0).toFixed(3); const bFixed = (b / 255.0).toFixed(3); @@ -593,7 +593,7 @@ export default { type: 'value', matcher: isColor, transformer: function (token, _, options) { - const color = Color(options.usesW3C ? token.$value : token.value); + const color = Color(options.usesDtcg ? token.$value : token.value); if (color.getAlpha() === 1) { return color.toHexString(); } else { @@ -624,7 +624,7 @@ export default { type: 'value', matcher: /** @param {Token} token */ (token) => token.attributes?.category === 'color', transformer: function (token, _, options) { - let color = Color(options.usesW3C ? token.$value : token.value).toRgb(); + let color = Color(options.usesDtcg ? token.$value : token.value).toRgb(); return { red: (color.r / 255).toFixed(5), green: (color.g / 255).toFixed(5), @@ -649,7 +649,7 @@ export default { type: 'value', matcher: isFontSize, transformer: function (token, _, options) { - const nonParsedVal = options.usesW3C ? token.$value : token.value; + const nonParsedVal = options.usesDtcg ? token.$value : token.value; const val = parseFloat(nonParsedVal); if (isNaN(val)) throwSizeError(token.name, nonParsedVal, 'sp'); return val.toFixed(2) + 'sp'; @@ -671,7 +671,7 @@ export default { type: 'value', matcher: isNotFontSize, transformer: function (token, _, options) { - const nonParsedVal = options.usesW3C ? token.$value : token.value; + const nonParsedVal = options.usesDtcg ? token.$value : token.value; const val = parseFloat(nonParsedVal); if (isNaN(val)) throwSizeError(token.name, nonParsedVal, 'dp'); return val.toFixed(2) + 'dp'; @@ -698,7 +698,7 @@ export default { type: 'value', matcher: isSize, transformer: function (token, config, options) { - const value = options.usesW3C ? token.$value : token.value; + const value = options.usesDtcg ? token.$value : token.value; const parsedVal = parseFloat(value); if (isNaN(parsedVal)) throwSizeError(token.name, value, 'object'); @@ -726,7 +726,7 @@ export default { type: 'value', matcher: isFontSize, transformer: function (token, config, options) { - const value = options.usesW3C ? token.$value : token.value; + const value = options.usesDtcg ? token.$value : token.value; const parsedVal = parseFloat(value); const baseFont = getBasePxFontSize(config); if (isNaN(parsedVal)) throwSizeError(token.name, value, 'sp'); @@ -749,7 +749,7 @@ export default { type: 'value', matcher: isNotFontSize, transformer: function (token, config, options) { - const value = options.usesW3C ? token.$value : token.value; + const value = options.usesDtcg ? token.$value : token.value; const parsedVal = parseFloat(value); const baseFont = getBasePxFontSize(config); if (isNaN(parsedVal)) throwSizeError(token.name, value, 'dp'); @@ -772,7 +772,7 @@ export default { type: 'value', matcher: isSize, transformer: function (token, _, options) { - const value = options.usesW3C ? token.$value : token.value; + const value = options.usesDtcg ? token.$value : token.value; const parsedVal = parseFloat(value); if (isNaN(parsedVal)) throwSizeError(token.name, value, 'px'); return parsedVal + 'px'; @@ -794,7 +794,7 @@ export default { type: 'value', matcher: isSize, transformer: function (token, _, options) { - const nonParsed = options.usesW3C ? token.$value : token.value; + const nonParsed = options.usesDtcg ? token.$value : token.value; const parsedVal = parseFloat(nonParsed); if (isNaN(parsedVal)) throwSizeError(token.name, nonParsed, 'rem'); return parsedVal + 'rem'; @@ -816,7 +816,7 @@ export default { type: 'value', matcher: isSize, transformer: function (token, config, options) { - const value = options.usesW3C ? token.$value : token.value; + const value = options.usesDtcg ? token.$value : token.value; const parsedVal = parseFloat(value); const baseFont = getBasePxFontSize(config); if (isNaN(parsedVal)) throwSizeError(token.name, value, 'pt'); @@ -839,7 +839,7 @@ export default { type: 'value', matcher: isFontSize, transformer: function (token, config, options) { - const value = options.usesW3C ? token.$value : token.value; + const value = options.usesDtcg ? token.$value : token.value; const parsedVal = parseFloat(value); const baseFont = getBasePxFontSize(config); if (isNaN(parsedVal)) throwSizeError(token.name, value, 'sp'); @@ -862,7 +862,7 @@ export default { type: 'value', matcher: isNotFontSize, transformer: function (token, config, options) { - const value = options.usesW3C ? token.$value : token.value; + const value = options.usesDtcg ? token.$value : token.value; const parsedVal = parseFloat(value); const baseFont = getBasePxFontSize(config); if (isNaN(parsedVal)) throwSizeError(token.name, value, 'dp'); @@ -885,7 +885,7 @@ export default { type: 'value', matcher: isFontSize, transformer: function (token, _, options) { - const value = options.usesW3C ? token.$value : token.value; + const value = options.usesDtcg ? token.$value : token.value; const parsedVal = parseFloat(value); if (isNaN(parsedVal)) throwSizeError(token.name, value, 'em'); return parsedVal + '.em'; @@ -906,7 +906,7 @@ export default { type: 'value', matcher: isSize, transformer: function (token, config, options) { - const value = options.usesW3C ? token.$value : token.value; + const value = options.usesDtcg ? token.$value : token.value; const parsedVal = parseFloat(value); const baseFont = getBasePxFontSize(config); if (isNaN(parsedVal)) throwSizeError(token.name, value, 'CGFloat'); @@ -929,7 +929,7 @@ export default { type: 'value', matcher: isSize, transformer: function (token, config, options) { - const value = options.usesW3C ? token.$value : token.value; + const value = options.usesDtcg ? token.$value : token.value; const parsedVal = parseFloat(value); const baseFont = getBasePxFontSize(config); if (isNaN(parsedVal)) throwSizeError(token.name, value, 'px'); @@ -953,7 +953,7 @@ export default { type: 'value', matcher: isSize, transformer: (token, config, options) => { - const value = options.usesW3C ? token.$value : token.value; + const value = options.usesDtcg ? token.$value : token.value; const parsedVal = parseFloat(value); const baseFont = getBasePxFontSize(config); @@ -986,7 +986,7 @@ export default { return token.attributes?.category === 'content' && token.attributes.type === 'icon'; }, transformer: function (token, _, options) { - return (options.usesW3C ? token.$value : token.value).replace( + return (options.usesDtcg ? token.$value : token.value).replace( UNICODE_PATTERN, /** * @param {string} match @@ -1108,7 +1108,7 @@ export default { return token.attributes?.category === 'time'; }, transformer: function (token, _, options) { - return (parseFloat(options.usesW3C ? token.$value : token.value) / 1000).toFixed(2) + 's'; + return (parseFloat(options.usesDtcg ? token.$value : token.value) / 1000).toFixed(2) + 's'; }, }, @@ -1127,7 +1127,7 @@ export default { type: 'value', matcher: isAsset, transformer: function (token, _, options) { - return convertToBase64(options.usesW3C ? token.$value : token.value); + return convertToBase64(options.usesDtcg ? token.$value : token.value); }, }, @@ -1146,7 +1146,7 @@ export default { type: 'value', matcher: isAsset, transformer: function (token, _, options) { - return join(process?.cwd() ?? '/', options.usesW3C ? token.$value : token.value); + return join(process?.cwd() ?? '/', options.usesDtcg ? token.$value : token.value); }, }, @@ -1198,7 +1198,7 @@ export default { type: 'value', matcher: isColor, transformer: function (token, _, options) { - const str = Color(options.usesW3C ? token.$value : token.value) + const str = Color(options.usesDtcg ? token.$value : token.value) .toHex8() .toUpperCase(); return `Color(0x${str.slice(6)}${str.slice(0, 6)})`; @@ -1270,7 +1270,7 @@ export default { matcher: isSize, transformer: function (token, config, options) { const baseFont = getBasePxFontSize(config); - return (parseFloat(options.usesW3C ? token.$value : token.value) * baseFont).toFixed(2); + return (parseFloat(options.usesDtcg ? token.$value : token.value) * baseFont).toFixed(2); }, }, }; diff --git a/lib/filterTokens.js b/lib/filterTokens.js index 38203d8d0..ba166cd6e 100644 --- a/lib/filterTokens.js +++ b/lib/filterTokens.js @@ -36,7 +36,7 @@ function filterTokenObject(tokens, filter, options) { // Use reduce to generate a new object with the unwanted tokens filtered // out return Object.entries(tokens ?? []).reduce((acc, [key, token]) => { - const tokenValue = options.usesW3C ? token.$value : token.value; + const tokenValue = options.usesDtcg ? token.$value : token.value; // If the token is not an object, we don't know what it is. We return it as-is. if (!isPlainObject(token)) { return acc; diff --git a/lib/transform/object.js b/lib/transform/object.js index 7b74fe96a..ad098b7e3 100644 --- a/lib/transform/object.js +++ b/lib/transform/object.js @@ -63,7 +63,7 @@ export default function transformObject( // value: "#ababab" // ... // } - if (isObj && Object.hasOwn(objProp, `${options.usesW3C ? '$' : ''}value`)) { + if (isObj && Object.hasOwn(objProp, `${options.usesDtcg ? '$' : ''}value`)) { const pathName = getName(path); const alreadyTransformed = transformedPropRefs.indexOf(pathName) !== -1; @@ -89,7 +89,7 @@ export default function transformObject( }; // If property has a reference, defer its transformations until later - if (usesReferences(options.usesW3C ? token.$value : token.value, config)) { + if (usesReferences(options.usesDtcg ? token.$value : token.value, config)) { deferProp(); continue; } diff --git a/lib/transform/token.js b/lib/transform/token.js index 315b278d1..aedefa201 100644 --- a/lib/transform/token.js +++ b/lib/transform/token.js @@ -50,7 +50,7 @@ export default function transformProperty(token, config, options) { // Only try to transform if the value is not a string or if it has '{}' if ( transform.type === 'value' && - !usesReferences(options.usesW3C ? token.$value : token.value, config) + !usesReferences(options.usesDtcg ? token.$value : token.value, config) ) { // Only transform non-referenced values (from original) // and transitive transforms if the value has been resolved @@ -59,7 +59,7 @@ export default function transformProperty(token, config, options) { if (transformedValue === undefined) { return undefined; } - to_ret[options.usesW3C ? '$value' : 'value'] = transformedValue; + to_ret[options.usesDtcg ? '$value' : 'value'] = transformedValue; } } diff --git a/lib/utils/combineJSON.js b/lib/utils/combineJSON.js index f7c2ee625..0c267b883 100644 --- a/lib/utils/combineJSON.js +++ b/lib/utils/combineJSON.js @@ -17,7 +17,7 @@ import { extname } from 'path-unified'; import { fs } from 'style-dictionary/fs'; import { resolve } from '../resolve.js'; import deepExtend from './deepExtend.js'; -import { detectW3CSyntax } from './detectW3CSyntax.js'; +import { detectDtcgSyntax } from './detectDtcgSyntax.js'; /** * @typedef {import('../../types/DesignToken.d.ts').DesignTokens} Tokens @@ -47,8 +47,8 @@ function traverseObj(obj, fn) { * @param {Function} [collision] - A function to be called when a name collision happens that isn't a normal deep merge of objects * @param {boolean} [source] - If json files are "sources", tag tokens * @param {Parser[]} [parsers] - Custom file parsers - * @param {boolean} [usesW3C] - Whether or not tokens are using W3C syntax. - * @returns {Promise<{tokens: Tokens, usesW3C: boolean|undefined }>} + * @param {boolean} [usesDtcg] - Whether or not tokens are using DTCG syntax. + * @returns {Promise<{tokens: Tokens, usesDtcg: boolean|undefined }>} */ export default async function combineJSON( arr, @@ -56,7 +56,7 @@ export default async function combineJSON( collision, source = true, parsers = [], - usesW3C, + usesDtcg, ) { /** @type {Tokens} */ const to_ret = {}; @@ -109,12 +109,12 @@ export default async function combineJSON( } if (file_content) { - if (usesW3C === undefined) { - usesW3C = detectW3CSyntax(file_content); + if (usesDtcg === undefined) { + usesDtcg = detectDtcgSyntax(file_content); } // Add some side data on each property to make filtering easier traverseObj(file_content, (obj) => { - if (Object.hasOwn(obj, `${usesW3C ? '$' : ''}value`) && !obj.filePath) { + if (Object.hasOwn(obj, `${usesDtcg ? '$' : ''}value`) && !obj.filePath) { obj.filePath = filePath; obj.isSource = source; @@ -129,5 +129,5 @@ export default async function combineJSON( } } - return { tokens: to_ret, usesW3C }; + return { tokens: to_ret, usesDtcg }; } diff --git a/lib/utils/createFormatArgs.js b/lib/utils/createFormatArgs.js index 853bda972..7b7c60d99 100644 --- a/lib/utils/createFormatArgs.js +++ b/lib/utils/createFormatArgs.js @@ -12,7 +12,6 @@ */ import deepExtend from './deepExtend.js'; -import { deepmerge } from './deepmerge.js'; /** * @typedef {import('../../types/DesignToken.js').Dictionary} Dictionary @@ -50,7 +49,7 @@ export default function createFormatArgs({ dictionary, platform, options, file } file, options: { ...(file.options || {}), - usesW3C: options?.usesW3C ?? false, + usesDtcg: options?.usesDtcg ?? false, }, }; } diff --git a/lib/utils/detectW3CSyntax.js b/lib/utils/detectDtcgSyntax.js similarity index 84% rename from lib/utils/detectW3CSyntax.js rename to lib/utils/detectDtcgSyntax.js index 3672c4bb3..a3f824986 100644 --- a/lib/utils/detectW3CSyntax.js +++ b/lib/utils/detectDtcgSyntax.js @@ -6,14 +6,14 @@ import isPlainObject from 'is-plain-obj'; * @param {Tokens} tokens * @returns */ -export function detectW3CSyntax(tokens) { - let usesW3C = false; +export function detectDtcgSyntax(tokens) { + let usesDtcg = false; // depth-first because more likely to be faster than breadth-first // due to amount of tokens usually being much larger than depth of token groups const recurse = /** @param {Tokens} slice */ (slice) => { Object.keys(slice).forEach((key) => { if (['$value', '$type'].includes(key)) { - usesW3C = true; + usesDtcg = true; return; } if (isPlainObject(slice[key])) { @@ -22,5 +22,5 @@ export function detectW3CSyntax(tokens) { }); }; recurse(tokens); - return usesW3C; + return usesDtcg; } diff --git a/lib/utils/flattenTokens.js b/lib/utils/flattenTokens.js index 5dd57b6ed..83972707d 100644 --- a/lib/utils/flattenTokens.js +++ b/lib/utils/flattenTokens.js @@ -21,18 +21,18 @@ import isPlainObject from 'is-plain-obj'; /** * @private * @param {Tokens} slice - The plain object you want flattened into an array. - * @param {boolean} [usesW3C] - Whether or not tokens are using W3C syntax. + * @param {boolean} [usesDtcg] - Whether or not tokens are using DTCG syntax. * @param {Token[]} [to_ret] - Tokens array. This function is recursive therefore this is what gets passed along. * @return {Token[]} */ -function _flattenTokens(slice, usesW3C, to_ret = []) { +function _flattenTokens(slice, usesDtcg, to_ret = []) { for (let name in slice) { if (Object.hasOwn(slice, name)) { // TODO: this is a bit fragile and arbitrary to stop when we get to a 'value' property. - if (isPlainObject(slice[name]) && Object.hasOwn(slice[name], `${usesW3C ? '$' : ''}value`)) { + if (isPlainObject(slice[name]) && Object.hasOwn(slice[name], `${usesDtcg ? '$' : ''}value`)) { to_ret.push(/** @type {Token} */ (slice[name])); } else if (isPlainObject(slice[name])) { - _flattenTokens(slice[name], usesW3C, to_ret); + _flattenTokens(slice[name], usesDtcg, to_ret); } } } @@ -44,9 +44,9 @@ function _flattenTokens(slice, usesW3C, to_ret = []) { * A leaf node in this context has a 'value' property. Potentially refactor this to * be more generic. * @param {Tokens} tokens - The plain object you want flattened into an array. - * @param {boolean} [usesW3C] - Whether or not tokens are using W3C syntax. + * @param {boolean} [usesDtcg] - Whether or not tokens are using DTCG syntax. * @return {Token[]} */ -export default function flattenTokens(tokens, usesW3C = false) { - return _flattenTokens(tokens, usesW3C); +export default function flattenTokens(tokens, usesDtcg = false) { + return _flattenTokens(tokens, usesDtcg); } diff --git a/lib/utils/index.js b/lib/utils/index.js index 70607e143..6132365be 100644 --- a/lib/utils/index.js +++ b/lib/utils/index.js @@ -15,7 +15,7 @@ import usesReferences from './references/usesReferences.js'; import { getReferences } from './references/getReferences.js'; import { resolveReferences } from './references/resolveReferences.js'; import flattenTokens from './flattenTokens.js'; -import { typeW3CDelegate } from './preprocess.js'; +import { typeDtcgDelegate } from './preprocess.js'; // Public style-dictionary/utils API -export { usesReferences, getReferences, resolveReferences, flattenTokens, typeW3CDelegate }; +export { usesReferences, getReferences, resolveReferences, flattenTokens, typeDtcgDelegate }; diff --git a/lib/utils/preprocess.js b/lib/utils/preprocess.js index fbd95303b..1525a7907 100644 --- a/lib/utils/preprocess.js +++ b/lib/utils/preprocess.js @@ -24,7 +24,7 @@ import isPlainObject from 'is-plain-obj'; * @param {DesignTokens} tokens * @returns */ -export function typeW3CDelegate(tokens) { +export function typeDtcgDelegate(tokens) { const clone = structuredClone(tokens); /** @@ -62,7 +62,7 @@ export function typeW3CDelegate(tokens) { * @returns {Promise} */ export async function preprocess(tokens, preprocessorObj = {}) { - let processedTokens = typeW3CDelegate(tokens); + let processedTokens = typeDtcgDelegate(tokens); const preprocessors = Object.values(preprocessorObj); if (preprocessors.length > 0) { diff --git a/lib/utils/references/getReferences.js b/lib/utils/references/getReferences.js index 8694ee85a..80b632a9c 100644 --- a/lib/utils/references/getReferences.js +++ b/lib/utils/references/getReferences.js @@ -61,7 +61,7 @@ export function _getReferences( references = [], throwImmediately = false, ) { - const { usesW3C } = opts; + const { usesDtcg } = opts; const regex = createReferenceRegex(opts); /** @@ -71,7 +71,7 @@ export function _getReferences( */ function findReference(match, variable) { // remove 'value' to access the whole token object - variable = variable.trim().replace(`.${usesW3C ? '$' : ''}value`, ''); + variable = variable.trim().replace(`.${usesDtcg ? '$' : ''}value`, ''); // Find what the value is referencing const pathName = getPathFromName(variable, opts.separator ?? defaults.separator); diff --git a/lib/utils/references/resolveReferences.js b/lib/utils/references/resolveReferences.js index 69b9d9707..9b67d98a0 100644 --- a/lib/utils/references/resolveReferences.js +++ b/lib/utils/references/resolveReferences.js @@ -55,7 +55,7 @@ export function _resolveReferences( opening_character = defaults.opening_character, closing_character = defaults.closing_character, ignorePaths = [], - usesW3C = false, + usesDtcg = false, // for internal usage current_context = [], stack = [], @@ -69,7 +69,7 @@ export function _resolveReferences( let to_ret = value; /** @type {DesignToken|string|number|undefined} */ let ref; - const valProp = usesW3C ? '$value' : 'value'; + const valProp = usesDtcg ? '$value' : 'value'; // When we know the current context: // the key associated with the value that we are resolving the reference for @@ -105,7 +105,7 @@ export function _resolveReferences( // and // the reference points to someplace that has a `value` attribute // we should take the '.value' of the reference - // per the W3C draft spec where references do not have .value + // per the DTCG draft spec where references do not have .value // https://design-tokens.github.io/community-group/format/#aliases-references if (!refHasValue && ref && Object.hasOwn(ref, valProp)) { ref = ref[valProp]; @@ -154,7 +154,7 @@ export function _resolveReferences( to_ret = _resolveReferences(to_ret, tokens, { regex: reg, ignorePaths, - usesW3C, + usesDtcg, current_context, separator, stack, diff --git a/types/Config.d.ts b/types/Config.d.ts index 2d7eb1b66..8e56fdfaa 100644 --- a/types/Config.d.ts +++ b/types/Config.d.ts @@ -35,13 +35,13 @@ export interface RegexOptions { } export interface GetReferencesOptions extends RegexOptions { - usesW3C?: boolean; + usesDtcg?: boolean; unfilteredTokens?: DesignTokens; } export interface ResolveReferencesOptions extends RegexOptions { ignorePaths?: string[]; - usesW3C?: boolean; + usesDtcg?: boolean; } export interface ResolveReferencesOptionsInternal extends ResolveReferencesOptions { @@ -78,5 +78,5 @@ export interface Config { filter?: Record; fileHeader?: Record; action?: Record; - usesW3C?: boolean; + usesDtcg?: boolean; }