-
Notifications
You must be signed in to change notification settings - Fork 567
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
feat(formats): output references handles interpoloated references #590
Conversation
docs/formats.md
Outdated
@@ -240,7 +240,7 @@ The formatter function that is called when Style Dictionary builds files. | |||
<td>args.dictionary.usesReference</td><td><code>function</code></td><td><p>Use this function to see if a token's value uses a reference. This is the same function style dictionary uses internally to detect a reference.</p> | |||
</td> | |||
</tr><tr> | |||
<td>args.dictionary.getReference</td><td><code>function</code></td><td><p>Use this function to get the token/property that it references. You can use this to output a reference in your custom format. For example: <code>dictionary.getReference(token.original.value) // returns the referenced token object</code></p> | |||
<td>args.dictionary.getReferences</td><td><code>function</code></td><td><p>Use this function to get the token/property that it references. You can use this to output a reference in your custom format. For example: <code>dictionary.getReferences(token.original.value) // returns an array of the referenced token objects</code></p> |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
typo: tokens/properties
return allProperties | ||
.map(createPropertyFormatter({ outputReferences, dictionary, format })) | ||
.filter(function(strVal) { return !!strVal }) | ||
.join('\n'); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
might want to make the line ending character a parameter with a default of \n
lib/register/format.js
Outdated
@@ -25,7 +25,7 @@ | |||
* @param {Object} args.dictionary.properties - Object structure of the tokens/properties that has been transformed and references resolved. | |||
* @param {Array} args.dictionary.allProperties - Flattened array of all the tokens/properties. This makes it easy to output a list, like a list of SCSS variables. | |||
* @param {function(value): Boolean} args.dictionary.usesReference - Use this function to see if a token's value uses a reference. This is the same function style dictionary uses internally to detect a reference. | |||
* @param {function(value): Value} args.dictionary.getReference - Use this function to get the token/property that it references. You can use this to output a reference in your custom format. For example: `dictionary.getReference(token.original.value) // returns the referenced token object` | |||
* @param {function(value): Value} args.dictionary.getReferences - Use this function to get the token/property that it references. You can use this to output a reference in your custom format. For example: `dictionary.getReferences(token.original.value) // returns an array of the referenced token objects` |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
change documentation to match above as per our conversation
test.js
Outdated
const StyleDictionary = require('./index'); | ||
|
||
StyleDictionary.extend({ | ||
properties: { | ||
color: { | ||
red: { value: "#f00" }, | ||
blue: { value: "#00f" }, | ||
danger: { value: "calc({color.red.value}, {color.blue.value})" }, | ||
test: { value: "{color.red.value}" } | ||
} | ||
}, | ||
platforms: { | ||
css: { | ||
transformGroup: `css`, | ||
files: [{ | ||
destination: `variables.css`, | ||
format: `css/variables`, | ||
options: { | ||
outputReferences: true | ||
} | ||
}] | ||
} | ||
} | ||
}).buildAllPlatforms(); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
oopsie
variables.css
Outdated
/** | ||
* Do not edit directly | ||
* Generated on Tue, 30 Mar 2021 19:49:14 GMT | ||
*/ | ||
|
||
:root { | ||
--color-blue: #0000ff; | ||
--color-red: #ff0000; | ||
--color-test: var(--color-red); | ||
--color-danger: calc(var(--color-red), var(--color-blue)); | ||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
oopsie
// `getReference()` methods. `usesReference()` will return true if | ||
// the value has a reference in it. `getReference()` will return | ||
// `getReferences()` methods. `usesReference()` will return true if | ||
// the value has a reference in it. `getReferences()` will return | ||
// the reference to the whole token so that you can access its |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
reference to the whole token
let ref; | ||
// because a token's value can contain multiple references due to string interpolation | ||
// "{size.padding.base.value} {color.border.primary.value}" | ||
// references is an array of 0 or more references | ||
const references = []; | ||
|
||
// function inside .replace runs multiple times if there are multiple matches | ||
value.replace(regex, function(match, variable) { | ||
// remove 'value' to access the whole token object | ||
variable = variable.trim().replace('.value',''); | ||
|
||
// Find what the value is referencing | ||
const pathName = getPath(variable); | ||
|
||
ref = resolveReference(pathName, self.properties); | ||
let ref = resolveReference(pathName, self.properties); | ||
if (!ref) { | ||
// fall back on _properties as it is unfiltered | ||
ref = resolveReference(pathName, self._properties); | ||
// and warn the user about this | ||
GroupMessages.add(GroupMessages.GROUP.FilteredOutputReferences, variable); | ||
} | ||
|
||
references.push(ref); | ||
}); | ||
|
||
return ref; | ||
return references; |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
🧠
let {prefix, lineSeparator, header, footer} = Object.assign({}, defaultFormatting, formatting); | ||
|
||
if (commentStyle === 'short') { | ||
prefix = `// `; | ||
header = `${lineSeparator}`; | ||
footer = `${lineSeparator}${lineSeparator}`; | ||
} else if (commentStyle === 'xml') { | ||
prefix = ` `; | ||
header = `<!--${lineSeparator}`; | ||
footer = `${lineSeparator}-->`; | ||
} else { | ||
prefix = ` * `; | ||
header = `/**${lineSeparator}`; | ||
footer = `${lineSeparator} */${lineSeparator}${lineSeparator}`; | ||
} | ||
|
||
return `${commentHeader}${fn(defaultHeader) | ||
.map(line => `${commentPrefix}${line}`) | ||
.join('\n')}${commentFooter}`; | ||
return `${header}${fn(defaultHeader) | ||
.map(line => `${prefix}${line}`) | ||
.join(lineSeparator)}${footer}`; |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
❤️
Issue #, if available: #589
Description of changes:
getReference
togetReferences
and changed it to return an array of references for cases when there are multiple references in a single value.sortAllProperties
format helper tosortByReference
to be more clear and made changes to support interpolated values and to not mutate theallProperties
array. This caused some of the sorting changes in some of the snapshots.createPropertyFormatter
format helper which will return a formatting function based on some configuration to be used in formats when iterating overallProperties
. TheformattedVariables
format helper uses this function as well as several formats to remove duplicate code.flutter/dart.class
to use the sorting when usingoutputReferences
By submitting this pull request, I confirm that my contribution is made under the terms of the Apache 2.0 license.