Skip to content

Commit 061c67e

Browse files
authoredApr 15, 2024··
fix: outputReferencesTransformed util handling object-value tokens, unfiltered (#1155)

File tree

3 files changed

+28
-7
lines changed

3 files changed

+28
-7
lines changed
 

‎.changeset/ninety-meals-hug.md

+5
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
---
2+
'style-dictionary': patch
3+
---
4+
5+
Hotfix to address outputReferencesTransformed util not handling object-value tokens properly.

‎lib/common/formatHelpers/createPropertyFormatter.js

+9-3
Original file line numberDiff line numberDiff line change
@@ -193,12 +193,16 @@ export default function createPropertyFormatter({
193193
const originalIsObject = typeof originalValue === 'object' && originalValue !== null;
194194

195195
if (!originalIsObject) {
196+
// TODO: find a better way to deal with object-value tokens and outputting refs
197+
// e.g. perhaps it is safer not to output refs when the value is transformed to a non-object
198+
// for example for CSS-like formats we always flatten to e.g. strings
199+
196200
// when original is object value, we replace value by matching ref.value and putting a var instead.
197201
// Due to the original.value being an object, it requires transformation, so undoing the transformation
198-
// by replacing value with original.value is not possible.
202+
// by replacing value with original.value is not possible. (this is the early v3 approach to outputting refs)
199203

200204
// when original is string value, we replace value by matching original.value and putting a var instead
201-
// this is more friendly to transitive transforms that transform the string values
205+
// this is more friendly to transitive transforms that transform the string values (v4 way of outputting refs)
202206
value = originalValue;
203207
}
204208

@@ -220,7 +224,9 @@ export default function createPropertyFormatter({
220224
return `${prefix}${ref.name}`;
221225
}
222226
};
223-
value = value.replace(
227+
// TODO: add test
228+
// technically speaking a reference can be made to a number or boolean token, in this case we stringify it first
229+
value = `${value}`.replace(
224230
originalIsObject ? refVal : new RegExp(`{${ref.path.join('\\.')}(\\.\\$?value)?}`, 'g'),
225231
replaceFunc,
226232
);

‎lib/utils/references/outputReferencesTransformed.js

+14-4
Original file line numberDiff line numberDiff line change
@@ -12,8 +12,18 @@ export function outputReferencesTransformed(token, { dictionary, usesDtcg }) {
1212
const originalValue = usesDtcg ? token.original.$value : token.original.value;
1313
const value = usesDtcg ? token.$value : token.value;
1414

15-
// Check if the token's value is the same as if we were resolve references on the original value
16-
// This checks whether the token's value has been transformed e.g. transitive transforms.
17-
// If it has been, that means we should not be outputting refs because this would undo the work of those transforms.
18-
return value === resolveReferences(originalValue, dictionary.tokens, { usesDtcg });
15+
// double check if this is a string, technically speaking the token could also be an object
16+
// and pass the usesReferences check
17+
if (typeof originalValue === 'string') {
18+
// Check if the token's value is the same as if we were resolve references on the original value
19+
// This checks whether the token's value has been transformed e.g. transitive transforms.
20+
// If it has been, that means we should not be outputting refs because this would undo the work of those transforms.
21+
return (
22+
value ===
23+
resolveReferences(originalValue, dictionary.unfilteredTokens ?? dictionary.tokens, {
24+
usesDtcg,
25+
})
26+
);
27+
}
28+
return true;
1929
}

0 commit comments

Comments
 (0)
Please sign in to comment.