-
Notifications
You must be signed in to change notification settings - Fork 28
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
[Bug]: ts/resolveMath
unexpected results with outputReferences
#121
Comments
It's actually a problem with the regex replacement in outputReferences in style-dictionary: https://github.com/amzn/style-dictionary/blob/main/lib/common/formatHelpers/createPropertyFormatter.js#L115 Basically, the way it works is, if you have a token: {
"foo": {
"value": "{bar}"
},
"bar": {
"value": "4px"
}
}
Then, when we get to formatting it to CSS variables, if outputReferences is true, it will look at What that means is, if your "value" has been changed after the resolving process, e.g. with a transitive transform (like resolveMath transformer, which must be transitive for it to work at all), the replace will not work, or in some cases it will cause weird behavior like you see with It might work better if the replace done by style-dictionary was done on the original value, like This issue is a duplicate of #13 which I closed because it cannot be fixed in this repo, it would require a fix in style-dictionary, which I think is reasonable to ask because basically, outputReferences doesn't work in general if a transitive transform changes the value, which is also demonstrated in that issue quite well, so it shows it's not just a problem caused by our @gsouquet did you end up creating an issue on style-dictionary for this? Because if we can get their view on this and they agree with my idea to fix it, I am willing to create a PR. |
@jorenbroekema I have not, apologies for this. But could this afternoon if that helps! |
Yeah would be great, I'll tag along and share my 2 cents if anything is missing |
@jorenbroekema I've opened amzn/style-dictionary#974 |
amzn/style-dictionary#820 (comment) mentions a somewhat "hacky" solution that looks to preserve the css variables. See comments on the linked issue as to why this is hacky, pretty much flags that strings could contain const StyleDictionary = require('style-dictionary');
StyleDictionary.registerTransform({
type: `value`,
transitive: true,
name: `figma/calc`,
matcher: ({ value }) => typeof value === 'string' && value?.includes('*'),
transformer: ({ value }) => `calc(${value})`,
}); This would give you an output of: --refSize800: calc(4 * var(--refSizeBase)); |
@lukethacoder this quite easily runs into issues though when you have chained references: In both cases the output is not what you want. The reason your workaround was working is:
What I think we have here is another use case for deferred transforms, where you control the fact that it runs |
I am closing this issue for the following reasons:
outputReferences now works better (since 3.9.0) with transitive transforms in that it just undo's the work because it puts back the original.value in the output as a CSS variable, so that's an improvement over how it worked before. Deferred transforms should allow us in the future to go further with transforming stuff after all references are resolved. |
What happened?
Referenced value with math calculation returns unexpected results when
outputReferences
is set to true. It might be a bug ints/resolveMath
.I think there's a bug in the part of regex replacement.
sd-transforms/src/checkAndEvaluateMath.ts
Lines 68 to 103 in db489d4
Reproduction
The following can be reproduced on the Online Token Studio Configurator with the prepared environment.
outputReferences
totrue
build/css/variables.css
When the option
outputReferences
is unset, the result works as expected.Expected output
Version
0.8.6
The text was updated successfully, but these errors were encountered: