Skip to content

Commit

Permalink
fix: deepExtend util to not clear meta for colliding tokens (#1254)
Browse files Browse the repository at this point in the history
  • Loading branch information
jorenbroekema authored Jun 24, 2024
1 parent 17985e5 commit 5079154
Show file tree
Hide file tree
Showing 5 changed files with 47 additions and 7 deletions.
5 changes: 5 additions & 0 deletions .changeset/clever-horses-suffer.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
'style-dictionary': patch
---

Fix deepExtend util bug with overriding behavior for tokens.
4 changes: 2 additions & 2 deletions __integration__/logging/__snapshots__/config.test.snap.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,13 +2,13 @@
export const snapshots = {};
snapshots["integration > logging > config > property value collisions should not throw, but notify users by default"] =
`
Token collisions detected (4):
Token collisions detected (16):
Use log.verbosity "verbose" or use CLI option --verbose for more details.`;
/* end snapshot integration > logging > config > property value collisions should not throw, but notify users by default */

snapshots["integration > logging > config > property value collisions should not show warnings if given higher log level"] =
`
Token collisions detected (4):
Token collisions detected (16):
Use log.verbosity "verbose" or use CLI option --verbose for more details.`;
/* end snapshot integration > logging > config > property value collisions should not show warnings if given higher log level */

25 changes: 23 additions & 2 deletions __tests__/__snapshots__/StyleDictionary.test.snap.js
Original file line number Diff line number Diff line change
Expand Up @@ -23,22 +23,43 @@ Use log.verbosity "verbose" or use CLI option --verbose for more details.`;

snapshots["StyleDictionary class collisions should throw an error if the collision is in source files and log is set to error"] =
`
Token collisions detected (7):
Token collisions detected (28):
Collision detected at: size.padding.zero! Original value: 0, New value: 0
Collision detected at: size.padding.zero! Original value: dimension, New value: dimension
Collision detected at: size.padding.zero! Original value: __tests__/__tokens/_paddings.json, New value: __tests__/__tokens/_paddings.json
Collision detected at: size.padding.zero! Original value: true, New value: true
Collision detected at: size.padding.tiny! Original value: 3, New value: 3
Collision detected at: size.padding.tiny! Original value: dimension, New value: dimension
Collision detected at: size.padding.tiny! Original value: __tests__/__tokens/_paddings.json, New value: __tests__/__tokens/_paddings.json
Collision detected at: size.padding.tiny! Original value: true, New value: true
Collision detected at: size.padding.small! Original value: 5, New value: 5
Collision detected at: size.padding.small! Original value: dimension, New value: dimension
Collision detected at: size.padding.small! Original value: __tests__/__tokens/_paddings.json, New value: __tests__/__tokens/_paddings.json
Collision detected at: size.padding.small! Original value: true, New value: true
Collision detected at: size.padding.base! Original value: 10, New value: 10
Collision detected at: size.padding.base! Original value: dimension, New value: dimension
Collision detected at: size.padding.base! Original value: __tests__/__tokens/_paddings.json, New value: __tests__/__tokens/_paddings.json
Collision detected at: size.padding.base! Original value: true, New value: true
Collision detected at: size.padding.large! Original value: 15, New value: 15
Collision detected at: size.padding.large! Original value: dimension, New value: dimension
Collision detected at: size.padding.large! Original value: __tests__/__tokens/_paddings.json, New value: __tests__/__tokens/_paddings.json
Collision detected at: size.padding.large! Original value: true, New value: true
Collision detected at: size.padding.xl! Original value: 20, New value: 20
Collision detected at: size.padding.xl! Original value: dimension, New value: dimension
Collision detected at: size.padding.xl! Original value: __tests__/__tokens/_paddings.json, New value: __tests__/__tokens/_paddings.json
Collision detected at: size.padding.xl! Original value: true, New value: true
Collision detected at: size.padding.xxl! Original value: 30, New value: 30
Collision detected at: size.padding.xxl! Original value: dimension, New value: dimension
Collision detected at: size.padding.xxl! Original value: __tests__/__tokens/_paddings.json, New value: __tests__/__tokens/_paddings.json
Collision detected at: size.padding.xxl! Original value: true, New value: true
`;
/* end snapshot StyleDictionary class collisions should throw an error if the collision is in source files and log is set to error */

snapshots["StyleDictionary class collisions should throw a brief error if the collision is in source files and log is set to error and verbosity default"] =
`
Token collisions detected (7):
Token collisions detected (28):
Use log.verbosity "verbose" or use CLI option --verbose for more details.`;
/* end snapshot StyleDictionary class collisions should throw a brief error if the collision is in source files and log is set to error and verbosity default */

Expand Down
18 changes: 16 additions & 2 deletions __tests__/utils/deepExtend.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -65,18 +65,32 @@ describe('utils', () => {

it("shouldn't merge when keys collide that should override rather than merge", () => {
const test = deepExtend(
[{ foo: { value: 'bar', metadata: 'meta' } }, { foo: { value: 'baz' } }],
[
{ foo: { type: 'other', value: 'bar', metadata: 'meta' } },
{ foo: { type: 'other', value: 'baz' } },
],
{ collision: function () {}, overrideKeys: ['value'] },
);
expect(test.foo).to.eql({
type: 'other',
value: 'baz',
});
expect(test).to.have.nested.property('foo.value', 'baz');
// we do not want to inherit this metadata from the prop we are overriding
expect(test).to.not.have.nested.property('foo.metadata');

const testDTCG = deepExtend(
[{ foo: { $value: 'bar', metadata: 'meta' } }, { foo: { $value: 'baz' } }],
[
{ foo: { $type: 'other', $value: 'bar', metadata: 'meta' } },
{ foo: { $type: 'other', $value: 'baz' } },
],
{ collision: function () {}, overrideKeys: ['$value'] },
);

expect(testDTCG.foo).to.eql({
$type: 'other',
$value: 'baz',
});
expect(testDTCG).to.have.nested.property('foo.$value', 'baz');
expect(testDTCG).to.not.have.nested.property('foo.metadata');
});
Expand Down
2 changes: 1 addition & 1 deletion lib/utils/deepExtend.js
Original file line number Diff line number Diff line change
Expand Up @@ -98,7 +98,7 @@ export default function deepExtend(objects, opts = {}, path) {
// we want to fully override an object, e.g. for design tokens
// we dont want sibling props of target to be merged into the copy
// see tests for more details
target = /** @type {T} */ ({});
target = options;
}
}
target[name] = copy;
Expand Down

0 comments on commit 5079154

Please sign in to comment.