Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: outputReferencesTransformed util not handling object-value tokens properly #1155

Merged
merged 1 commit into from
Apr 15, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions .changeset/ninety-meals-hug.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
'style-dictionary': patch
---

Hotfix to address outputReferencesTransformed util not handling object-value tokens properly.
12 changes: 9 additions & 3 deletions lib/common/formatHelpers/createPropertyFormatter.js
Original file line number Diff line number Diff line change
Expand Up @@ -193,12 +193,16 @@ export default function createPropertyFormatter({
const originalIsObject = typeof originalValue === 'object' && originalValue !== null;

if (!originalIsObject) {
// TODO: find a better way to deal with object-value tokens and outputting refs
// e.g. perhaps it is safer not to output refs when the value is transformed to a non-object
// for example for CSS-like formats we always flatten to e.g. strings

// when original is object value, we replace value by matching ref.value and putting a var instead.
// Due to the original.value being an object, it requires transformation, so undoing the transformation
// by replacing value with original.value is not possible.
// by replacing value with original.value is not possible. (this is the early v3 approach to outputting refs)

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

Expand All @@ -220,7 +224,9 @@ export default function createPropertyFormatter({
return `${prefix}${ref.name}`;
}
};
value = value.replace(
// TODO: add test
// technically speaking a reference can be made to a number or boolean token, in this case we stringify it first
value = `${value}`.replace(
originalIsObject ? refVal : new RegExp(`{${ref.path.join('\\.')}(\\.\\$?value)?}`, 'g'),
replaceFunc,
);
Expand Down
18 changes: 14 additions & 4 deletions lib/utils/references/outputReferencesTransformed.js
Original file line number Diff line number Diff line change
Expand Up @@ -12,8 +12,18 @@ export function outputReferencesTransformed(token, { dictionary, usesDtcg }) {
const originalValue = usesDtcg ? token.original.$value : token.original.value;
const value = usesDtcg ? token.$value : token.value;

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