Skip to content

Commit 6e226aa

Browse files
feat: pass ref path in getReferences util output (#1226)
1 parent 3f09277 commit 6e226aa

File tree

5 files changed

+22
-13
lines changed

5 files changed

+22
-13
lines changed

.changeset/spotty-sheep-mix.md

+5
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
---
2+
'style-dictionary': patch
3+
---
4+
5+
Pass the original ref path to the `getReferences` util result tokens.

__tests__/utils/reference/getReferences.test.js

+10-6
Original file line numberDiff line numberDiff line change
@@ -62,24 +62,28 @@ describe('utils', () => {
6262
});
6363

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

6870
it(`should work with object values`, () => {
6971
expect(_getReferences(tokens.border.primary.value, tokens)).to.eql([
70-
{ value: '#f00' },
71-
{ value: '2px' },
72+
{ ref: ['color', 'red'], value: '#f00' },
73+
{ ref: ['size', 'border'], value: '2px' },
7274
]);
7375
});
7476

7577
it(`should work with objects that have numbers`, () => {
76-
expect(_getReferences(tokens.border.secondary.value, tokens)).to.eql([{ value: '#f00' }]);
78+
expect(_getReferences(tokens.border.secondary.value, tokens)).to.eql([
79+
{ ref: ['color', 'red'], value: '#f00' },
80+
]);
7781
});
7882

7983
it(`should work with interpolated values`, () => {
8084
expect(_getReferences(tokens.border.tertiary.value, tokens)).to.eql([
81-
{ value: '2px' },
82-
{ value: '#f00' },
85+
{ ref: ['size', 'border'], value: '2px' },
86+
{ ref: ['color', 'red'], value: '#f00' },
8387
]);
8488
});
8589
});

docs/src/content/docs/reference/Hooks/preprocessors.md

+4-4
Original file line numberDiff line numberDiff line change
@@ -21,12 +21,12 @@ That said, preprocessing the full dictionary gives ultimate flexibility when nee
2121
A preprocessor is an object with two props:
2222

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

2626
```javascript title="my-preprocessor.js"
2727
const myPreprocessor = {
2828
name: 'strip-third-party-meta',
29-
preprocessor: (dictionary) => {
29+
preprocessor: (dictionary, options) => {
3030
delete dictionary.thirdPartyMetadata;
3131
return dictionary;
3232
},
@@ -38,7 +38,7 @@ Asynchronous callback functions are also supported, giving even more flexibility
3838
```javascript title="my-preprocessor-async.js"
3939
const myPreprocessor = {
4040
name: 'strip-props',
41-
preprocessor: async (dictionary) => {
41+
preprocessor: async (dictionary, options) => {
4242
const propsToDelete = await someAPICall();
4343

4444
propsToDelete.forEach((propName) => {
@@ -112,7 +112,7 @@ Stripping description property recursively in the entire dictionary object:
112112
```js
113113
StyleDictionary.registerPreprocessor({
114114
name: 'strip-descriptions',
115-
preprocessor: (dict) => {
115+
preprocessor: (dict, options) => {
116116
// recursively traverse token objects and delete description props
117117
function removeDescription(slice) {
118118
delete slice.description;

docs/src/content/docs/reference/Utils/references.md

+2-2
Original file line numberDiff line numberDiff line change
@@ -91,8 +91,8 @@ getReferences('solid {spacing.2} {colors.black}', sd.tokens); // alternative way
9191
getReferences('solid {spacing.2} {colors.black}', sd.tokens, { usesDtcg: true }); // Assumes DTCG spec format, with $ prefix ($value, $type)
9292
/**
9393
* [
94-
* { value: '2px', type: 'dimension' },
95-
* { value: '#000', type: 'color' }
94+
* { value: '2px', type: 'dimension', ref: ['spacing', '2'] },
95+
* { value: '#000', type: 'color', ref: ['colors', 'black'] }
9696
* ]
9797
*/
9898
```

lib/utils/references/getReferences.js

+1-1
Original file line numberDiff line numberDiff line change
@@ -88,7 +88,7 @@ export function _getReferences(
8888
}
8989

9090
if (ref !== undefined) {
91-
references.push(ref);
91+
references.push({ ...ref, ref: pathName });
9292
} else if (throwImmediately) {
9393
throw new Error(`tries to reference ${variable}, which is not defined.`);
9494
}

0 commit comments

Comments
 (0)