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

Forward port of bugfix and grammar fix in v3 #1035

Merged
merged 2 commits into from
Oct 26, 2023
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/honest-toes-wink.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
'style-dictionary': patch
---

Allow falsy token values for outputReferences, e.g. `0`.
40 changes: 40 additions & 0 deletions __tests__/common/formatHelpers/createPropertyFormatter.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -114,6 +114,34 @@ const numberDictionary = createDictionary({
value: 10,
type: 'dimension',
},
zero: {
original: {
value: 0,
type: 'dimension',
},
attributes: {
category: 'tokens',
type: 'zero',
},
name: 'tokens-zero',
path: ['tokens', 'zero'],
value: 0,
type: 'dimension',
},
'ref-zero': {
original: {
value: '{tokens.zero}',
type: 'dimension',
},
attributes: {
category: 'tokens',
type: 'ref-zero',
},
name: 'tokens-ref-zero',
path: ['tokens', 'ref-zero'],
value: 0,
type: 'dimension',
},
},
},
});
Expand Down Expand Up @@ -248,6 +276,18 @@ describe('common', () => {
);
});

it('should support valid falsy values for outputReferences', () => {
const propFormatter = createPropertyFormatter({
outputReferences: true,
dictionary: numberDictionary,
format: 'css',
});
expect(propFormatter(numberDictionary.tokens.tokens.zero)).toEqual(' --tokens-zero: 0;');
expect(propFormatter(numberDictionary.tokens.tokens['ref-zero'])).toEqual(
' --tokens-ref-zero: var(--tokens-zero);',
);
});

it('should support multiple references for outputReferences', () => {
const propFormatter = createPropertyFormatter({
outputReferences: true,
Expand Down
2 changes: 1 addition & 1 deletion docs/transforms.md
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ There are 3 types of transforms: attribute, name, and value.

**Attribute:** An attribute transform adds to the attributes object on a design token. This is for including any meta-data about a design token such as it's CTI or other information.

**Name:** A name transform transform the name of a design token. You should really only be apply one name transformer because they will override each other if you use more than one.
**Name:** A name transform transforms the name of a design token. You should really only be applying one name transformer because they will override each other if you use more than one.

**Value:** The value transform is the most important as this is the one that changes the representation of the value. Colors can be turned into hex values, rgb, hsl, hsv, etc. Value transforms have a matcher function that filter which tokens that transform runs on. This allows us to only run a color transform on only the colors and not every design token.

Expand Down
2 changes: 1 addition & 1 deletion lib/common/formatHelpers/createPropertyFormatter.js
Original file line number Diff line number Diff line change
Expand Up @@ -184,7 +184,7 @@ export default function createPropertyFormatter({
// because Style Dictionary resolved this in the resolution step.
// Here we are undoing that by replacing the value with
// the reference's name
if (ref.value && ref.name) {
if (Object.hasOwn(ref, 'value') && Object.hasOwn(ref, 'name')) {
const replaceFunc = function () {
if (format === 'css') {
if (outputReferenceFallbacks) {
Expand Down
6 changes: 3 additions & 3 deletions scripts/handlebars/templates/transforms.hbs
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
# Transforms

Transforms are functions that modify a [token](tokens.md) so that it can be understood by a specific platform. It can modify the name, value, or attributes of a token - enabling each platform to use the design token in different ways. A simple example is changing pixel values to point values for iOS and dp or sp for Android.
Transforms are functions that modify a [token](tokens.md) so that it can be understood by a specific platform. It can modify the name, value, or attributes of a token - enabling each platform to use the design token in different ways. A simple example is changing pixel values to point values for iOS and dp or sp for Android.

Transforms are isolated per platform; each platform begins with the same design token and makes the modifications it needs without affecting other platforms. The order you use transforms matters because transforms are performed sequentially. Transforms are used in your [configuration](config.md), and can be either [pre-defined transforms](transforms.md?id=pre-defined-transforms) supplied by Style Dictionary or [custom transforms](transforms.md?id=defining-custom-transforms).
Transforms are isolated per platform; each platform begins with the same design token and makes the modifications it needs without affecting other platforms. The order you use transforms matters because transforms are performed sequentially. Transforms are used in your [configuration](config.md), and can be either [pre-defined transforms](transforms.md?id=pre-defined-transforms) supplied by Style Dictionary or [custom transforms](transforms.md?id=defining-custom-transforms).

Some platform configuration attributes apply a broader effect over the transforms applied. For example, the `size/remToDp` transform will scale a number by 16, or by the value of `options.basePxFontSize` if it is present. Check individual transform documentation to see where this is applicable.

Expand All @@ -29,7 +29,7 @@ There are 3 types of transforms: attribute, name, and value.

**Attribute:** An attribute transform adds to the attributes object on a design token. This is for including any meta-data about a design token such as it's CTI or other information.

**Name:** A name transform transform the name of a design token. You should really only be apply one name transformer because they will override each other if you use more than one.
**Name:** A name transform transforms the name of a design token. You should really only be applying one name transformer because they will override each other if you use more than one.

**Value:** The value transform is the most important as this is the one that changes the representation of the value. Colors can be turned into hex values, rgb, hsl, hsv, etc. Value transforms have a matcher function that filter which tokens that transform runs on. This allows us to only run a color transform on only the colors and not every design token.

Expand Down