Skip to content

Commit

Permalink
feat: pass ref path in getReferences util output (#1226)
Browse files Browse the repository at this point in the history
  • Loading branch information
jorenbroekema authored May 30, 2024
1 parent 3f09277 commit 6e226aa
Show file tree
Hide file tree
Showing 5 changed files with 22 additions and 13 deletions.
5 changes: 5 additions & 0 deletions .changeset/spotty-sheep-mix.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
'style-dictionary': patch
---

Pass the original ref path to the `getReferences` util result tokens.
16 changes: 10 additions & 6 deletions __tests__/utils/reference/getReferences.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -62,24 +62,28 @@ describe('utils', () => {
});

it(`should work with a single reference`, () => {
expect(_getReferences(tokens.color.danger.value, tokens)).to.eql([{ value: '#f00' }]);
expect(_getReferences(tokens.color.danger.value, tokens)).to.eql([
{ ref: ['color', 'red'], value: '#f00' },
]);
});

it(`should work with object values`, () => {
expect(_getReferences(tokens.border.primary.value, tokens)).to.eql([
{ value: '#f00' },
{ value: '2px' },
{ ref: ['color', 'red'], value: '#f00' },
{ ref: ['size', 'border'], value: '2px' },
]);
});

it(`should work with objects that have numbers`, () => {
expect(_getReferences(tokens.border.secondary.value, tokens)).to.eql([{ value: '#f00' }]);
expect(_getReferences(tokens.border.secondary.value, tokens)).to.eql([
{ ref: ['color', 'red'], value: '#f00' },
]);
});

it(`should work with interpolated values`, () => {
expect(_getReferences(tokens.border.tertiary.value, tokens)).to.eql([
{ value: '2px' },
{ value: '#f00' },
{ ref: ['size', 'border'], value: '2px' },
{ ref: ['color', 'red'], value: '#f00' },
]);
});
});
Expand Down
8 changes: 4 additions & 4 deletions docs/src/content/docs/reference/Hooks/preprocessors.md
Original file line number Diff line number Diff line change
Expand Up @@ -21,12 +21,12 @@ That said, preprocessing the full dictionary gives ultimate flexibility when nee
A preprocessor is an object with two props:

- `name`: the name of the preprocessor
- `preprocessor` a callback function that receives the dictionary as a parameter, and returns the processed dictionary
- `preprocessor` a callback function that receives the dictionary and SD options as parameters, and returns the processed dictionary

```javascript title="my-preprocessor.js"
const myPreprocessor = {
name: 'strip-third-party-meta',
preprocessor: (dictionary) => {
preprocessor: (dictionary, options) => {
delete dictionary.thirdPartyMetadata;
return dictionary;
},
Expand All @@ -38,7 +38,7 @@ Asynchronous callback functions are also supported, giving even more flexibility
```javascript title="my-preprocessor-async.js"
const myPreprocessor = {
name: 'strip-props',
preprocessor: async (dictionary) => {
preprocessor: async (dictionary, options) => {
const propsToDelete = await someAPICall();

propsToDelete.forEach((propName) => {
Expand Down Expand Up @@ -112,7 +112,7 @@ Stripping description property recursively in the entire dictionary object:
```js
StyleDictionary.registerPreprocessor({
name: 'strip-descriptions',
preprocessor: (dict) => {
preprocessor: (dict, options) => {
// recursively traverse token objects and delete description props
function removeDescription(slice) {
delete slice.description;
Expand Down
4 changes: 2 additions & 2 deletions docs/src/content/docs/reference/Utils/references.md
Original file line number Diff line number Diff line change
Expand Up @@ -91,8 +91,8 @@ getReferences('solid {spacing.2} {colors.black}', sd.tokens); // alternative way
getReferences('solid {spacing.2} {colors.black}', sd.tokens, { usesDtcg: true }); // Assumes DTCG spec format, with $ prefix ($value, $type)
/**
* [
* { value: '2px', type: 'dimension' },
* { value: '#000', type: 'color' }
* { value: '2px', type: 'dimension', ref: ['spacing', '2'] },
* { value: '#000', type: 'color', ref: ['colors', 'black'] }
* ]
*/
```
Expand Down
2 changes: 1 addition & 1 deletion lib/utils/references/getReferences.js
Original file line number Diff line number Diff line change
Expand Up @@ -88,7 +88,7 @@ export function _getReferences(
}

if (ref !== undefined) {
references.push(ref);
references.push({ ...ref, ref: pathName });
} else if (throwImmediately) {
throw new Error(`tries to reference ${variable}, which is not defined.`);
}
Expand Down

0 comments on commit 6e226aa

Please sign in to comment.