Skip to content

Commit

Permalink
fix(property setup): original property being mutated if the value is …
Browse files Browse the repository at this point in the history
…an object (#534)
  • Loading branch information
dbanksdesign authored Feb 4, 2021
1 parent 40a2601 commit 0b13ae2
Show file tree
Hide file tree
Showing 3 changed files with 41 additions and 3 deletions.
26 changes: 26 additions & 0 deletions __tests__/exportPlatform.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -100,6 +100,32 @@ describe('exportPlatform', () => {
expect(dictionary.color.font.link.original.value).toEqual(properties.color.font.link.value);
});

it('should not mutate original value if value is an object', () => {
const dictionary = StyleDictionary.extend({
properties: {
color: {
red: {
value: {
h: "{hue.red}",
s: 50,
l: 50
}
}
},
hue: {
red: 20
}
},
platforms: {
web: {
transformGroup: 'web'
}
}
}).exportPlatform('web');
expect(dictionary.color.red.original.value.h).toEqual("{hue.red}");
expect(dictionary.color.red.value.h).toEqual(20);
});

describe('reference warnings', () => {
const errorMessage = `Problems were found when trying to resolve property references`;
const platforms = {
Expand Down
10 changes: 10 additions & 0 deletions __tests__/transform/propertySetup.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -88,5 +88,15 @@ describe('transform', () => {
expect(test).toHaveProperty('name', 'white');
});

it('should handle objects', () => {
const test = propertySetup({
value: {
h: 20, s: 50, l: 50
}
}, 'red', ['color','red']);
expect(test).toHaveProperty('value.h', 20);
expect(test).toHaveProperty('original.value.h', 20);
})

});
});
8 changes: 5 additions & 3 deletions lib/transform/propertySetup.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,8 @@
* and limitations under the License.
*/

var _ = require('lodash');
const _ = require('lodash');
const deepExtend = require('../utils/deepExtend');

/**
* Takes a property object, a leaf node in a properties object, and
Expand All @@ -32,14 +33,15 @@ function propertySetup(property, name, path) {
if (!path || !_.isArray(path))
throw new Error('Path must be an array');

let to_ret = _.clone(property);
let to_ret = property;

// Only do this once
if (!property.original) {
// Initial property setup
// Keep the original object properties like it was in file (whitout additional data)
// so we can key off them in the transforms
var to_ret_original = _.clone(property);
to_ret = deepExtend([{}, property]);
let to_ret_original = deepExtend([{},property]);
delete to_ret_original.filePath;
delete to_ret_original.isSource;

Expand Down

0 comments on commit 0b13ae2

Please sign in to comment.