From 0b13ae212023ba003ab71cc30eadb20ad10ebc0c Mon Sep 17 00:00:00 2001 From: Danny Banks Date: Wed, 3 Feb 2021 16:35:35 -0800 Subject: [PATCH] fix(property setup): original property being mutated if the value is an object (#534) --- __tests__/exportPlatform.test.js | 26 +++++++++++++++++++++++ __tests__/transform/propertySetup.test.js | 10 +++++++++ lib/transform/propertySetup.js | 8 ++++--- 3 files changed, 41 insertions(+), 3 deletions(-) diff --git a/__tests__/exportPlatform.test.js b/__tests__/exportPlatform.test.js index eec481cc7..e26c356be 100644 --- a/__tests__/exportPlatform.test.js +++ b/__tests__/exportPlatform.test.js @@ -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 = { diff --git a/__tests__/transform/propertySetup.test.js b/__tests__/transform/propertySetup.test.js index 746a843f6..837af06b0 100644 --- a/__tests__/transform/propertySetup.test.js +++ b/__tests__/transform/propertySetup.test.js @@ -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); + }) + }); }); diff --git a/lib/transform/propertySetup.js b/lib/transform/propertySetup.js index 717557c46..73079569c 100644 --- a/lib/transform/propertySetup.js +++ b/lib/transform/propertySetup.js @@ -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 @@ -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;