amzn / style-dictionary

A build system for creating cross-platform styles.
https://styledictionary.com
Apache License 2.0
3.93k stars 557 forks source link

Does not output reference when values is zero #986

Closed lmacedotzt closed 1 year ago

lmacedotzt commented 1 year ago

When using outputReference, if the referenced alias has a value of zero it being ignored and the reference is not being replaced.

Input to recreate the issue:

// json
{
  "Offset": {
    "value": "0",
    "type": "other"
  },
  "Spacing": {
    "value": "2 + {Offset}",
    "type": "spacing"
  }
}

Actual result

// css
--offset: 0;
--spacing: 2 + 0;

Expected result

// css
--offset: 0;
--spacing: 2 + var(--offset);

After a quick look, it seems the issue is here, the condition is checking for a truthy value and zero is falsy: https://github.com/amzn/style-dictionary/blob/2cf72f3f89996503608c238d7f3bcbab5b53e719/lib/common/formatHelpers/createPropertyFormatter.js#LL114C21-L114C21

And I think this issue is related #886

acidlynx commented 1 year ago

Like here you can use function in filter

        function(token) {
            // omit empty or zero tokens
            if (token.value !=0) {
              return token
            }
jorenbroekema commented 1 year ago

I have reproduced this bug. There have been some fixes to outputReferences in v3.9.0 and planning to forward port those to v4.0.0-prerelease.1 which solve the following:

{
  "Offset": {
    "value": "0",
    "type": "other"
  },
  "Spacing": {
    "value": "2 + {Offset}",
    "type": "spacing"
  }
}

But when you mentioned the truthy check, I realized there is another bug indeed that is not fixed, which can be reproduced by making the "0" in your JSON a number rather than string:

{
  "Offset": {
    "value": 0,
    "type": "other"
  },
  "Spacing": {
    "value": "2 + {Offset}",
    "type": "spacing"
  }
}