Skip to content

Commit

Permalink
fix(references): getReferences now searches the entire object (#812)
Browse files Browse the repository at this point in the history
* fix(references): getReferences now searches the entire object rather than 1 level deep
* chore(references): cleaning up code
  • Loading branch information
dbanksdesign authored May 10, 2022
1 parent 3b42c1b commit 884b1b8
Show file tree
Hide file tree
Showing 4 changed files with 90 additions and 5 deletions.
26 changes: 26 additions & 0 deletions __integration__/__snapshots__/objectValues.test.js.snap
Original file line number Diff line number Diff line change
Expand Up @@ -76,6 +76,32 @@ exports[`integration object values css/variables hsl syntax with references shou
"
`;

exports[`integration object values css/variables shadow should match snapshot 1`] = `
"/**
* Do not edit directly
* Generated on Sat, 01 Jan 2000 00:00:00 GMT
*/
:root {
--shadow-light: #ff0000, #40bf40;
--shadow-dark: #40bf40, #ff0000;
}
"
`;

exports[`integration object values css/variables shadow should match snapshot with references 1`] = `
"/**
* Do not edit directly
* Generated on Sat, 01 Jan 2000 00:00:00 GMT
*/
:root {
--shadow-light: var(--color-red), var(--color-green);
--shadow-dark: var(--color-green), var(--color-red);
}
"
`;

exports[`integration object values scss/variables should match snapshot 1`] = `
"
// Do not edit directly
Expand Down
51 changes: 51 additions & 0 deletions __integration__/objectValues.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,22 @@ describe('integration', () => {
style: "solid"
}
},
},
shadow: {
light: {
value: [{
color: "{color.red.value}"
},{
color: "{color.green.value}"
}]
},
dark: {
value: [{
color: "{color.green.value}"
},{
color: "{color.red.value}"
}]
}
}
},
transform: {
Expand All @@ -75,6 +91,14 @@ describe('integration', () => {
transformer: (token) => {
return `${token.value.width} ${token.value.style} ${token.value.color}`
}
},
shadow: {
type: 'value',
transitive: true,
matcher: (token) => token.attributes.category === 'shadow',
transformer: (token) => {
return token.value.map(obj => obj.color).join(', ')
}
}
},
platforms: {
Expand Down Expand Up @@ -129,6 +153,21 @@ describe('integration', () => {
}]
},

cssShadow: {
buildPath,
transforms: StyleDictionary.transformGroup.css.concat([`shadow`,`hslToHex`]),
files: [{
destination: 'shadow.css',
format: 'css/variables',
filter: (token) => token.attributes.category === `shadow`,
},{
destination: 'shadowWithReferences.css',
format: 'css/variables',
filter: (token) => token.attributes.category === `shadow`,
options
}]
},

scss: {
buildPath,
transforms: StyleDictionary.transformGroup.css.concat([`cssBorder`,`hslToHex`]),
Expand Down Expand Up @@ -188,6 +227,18 @@ describe('integration', () => {
});
});
});

describe('shadow', () => {
it(`should match snapshot`, () => {
const output = fs.readFileSync(`${buildPath}shadow.css`, {encoding:'UTF-8'});
expect(output).toMatchSnapshot();
});

it(`should match snapshot with references`, () => {
const output = fs.readFileSync(`${buildPath}shadowWithReferences.css`, {encoding:'UTF-8'});
expect(output).toMatchSnapshot();
});
})
});

describe('scss/variables', () => {
Expand Down
7 changes: 7 additions & 0 deletions lib/common/formatHelpers/sortByReference.js
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,13 @@
const aComesFirst = -1;
const bComesFirst = 1;

// return early if a or b ar undefined
if (typeof a === 'undefined') {
return aComesFirst;
} else if (typeof b === 'undefined') {
return bComesFirst;
}

// If token a uses a reference and token b doesn't, b might come before a
// read on..
if (a.original && dictionary.usesReference(a.original.value)) {
Expand Down
11 changes: 6 additions & 5 deletions lib/utils/references/getReferences.js
Original file line number Diff line number Diff line change
Expand Up @@ -28,16 +28,13 @@ const GroupMessages = require('../groupMessages');
*
* @memberof Dictionary
* @param {string} value the value that contains a reference
* @param {object[]} references array of token's references because a token's value can contain multiple references due to string interpolation
* @returns {any}
*/
function getReferences(value) {
function getReferences(value, references=[]) {
// `this` is the dictionary object passed to formats and actions
const self = this;
const regex = createReferenceRegex({});
// 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 = [];

// this will update the references array with the referenced tokens it finds.
function findReference(match, variable) {
Expand Down Expand Up @@ -70,6 +67,10 @@ function getReferences(value) {
if (value.hasOwnProperty(key) && typeof value[key] === 'string') {
value[key].replace(regex, findReference);
}
// if it is an object, we go further down the rabbit hole
if (value.hasOwnProperty(key) && typeof value[key] === 'object') {
self.getReferences(value[key], references);
}
}
}

Expand Down

0 comments on commit 884b1b8

Please sign in to comment.