Skip to content

Commit cb94554

Browse files
authoredMay 17, 2024··
fix: typeDtcgDelegate at any position, size/rem apply to unitless values (#1210)
1 parent 646b8f8 commit cb94554

File tree

5 files changed

+96
-4
lines changed

5 files changed

+96
-4
lines changed
 

Diff for: ‎.changeset/eleven-llamas-raise.md

+5
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
---
2+
'style-dictionary': patch
3+
---
4+
5+
Fix typeDtcgDelegate util $type property position to be allowed anywhere in the object, not just at the top.

Diff for: ‎.changeset/fuzzy-gorillas-prove.md

+5
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
---
2+
'style-dictionary': minor
3+
---
4+
5+
'size/rem' transform to not transform tokens that already have a unit, such as `"4px"`, this should not be transformed to `"4rem"`.

Diff for: ‎__tests__/utils/typeDtcgDelegate.test.js

+78
Original file line numberDiff line numberDiff line change
@@ -92,5 +92,83 @@ describe('utils', () => {
9292
},
9393
});
9494
});
95+
96+
it('should work regardless at which position the $type property sits', () => {
97+
const tokens = {
98+
dimension: {
99+
scale: {
100+
$value: '2',
101+
$type: 'math',
102+
},
103+
xs: {
104+
$value: '4',
105+
},
106+
nested: {
107+
deep: {
108+
deeper: {
109+
$value: '12',
110+
},
111+
},
112+
deep2: {
113+
$type: 'math',
114+
deeper: {
115+
$type: 'other',
116+
evenDeeper: {
117+
$value: '12',
118+
$type: 'math',
119+
},
120+
evenDeeper2: {
121+
$value: '12',
122+
},
123+
},
124+
},
125+
},
126+
sm: {
127+
$value: '8',
128+
},
129+
$type: 'dimension',
130+
},
131+
};
132+
133+
expect(typeDtcgDelegate(tokens)).to.eql({
134+
dimension: {
135+
$type: 'dimension',
136+
scale: {
137+
$value: '2',
138+
$type: 'math',
139+
},
140+
xs: {
141+
$value: '4',
142+
$type: 'dimension',
143+
},
144+
nested: {
145+
deep: {
146+
deeper: {
147+
$value: '12',
148+
$type: 'dimension',
149+
},
150+
},
151+
deep2: {
152+
$type: 'math',
153+
deeper: {
154+
$type: 'other',
155+
evenDeeper: {
156+
$value: '12',
157+
$type: 'math',
158+
},
159+
evenDeeper2: {
160+
$value: '12',
161+
$type: 'other',
162+
},
163+
},
164+
},
165+
},
166+
sm: {
167+
$value: '8',
168+
$type: 'dimension',
169+
},
170+
},
171+
});
172+
});
95173
});
96174
});

Diff for: ‎lib/common/transforms.js

+4
Original file line numberDiff line numberDiff line change
@@ -786,6 +786,10 @@ export default {
786786
filter: (token, options) => isDimension(token, options) || isFontSize(token, options),
787787
transform: function (token, _, options) {
788788
const nonParsed = options.usesDtcg ? token.$value : token.value;
789+
// if the dimension already has a unit (non-digit / . period character)
790+
if (`${nonParsed}`.match(/[^0-9.]/g)) {
791+
return nonParsed;
792+
}
789793
const parsedVal = parseFloat(nonParsed);
790794
if (isNaN(parsedVal)) throwSizeError(token.name, nonParsed, 'rem');
791795
return parsedVal + 'rem';

Diff for: ‎lib/utils/typeDtcgDelegate.js

+4-4
Original file line numberDiff line numberDiff line change
@@ -23,11 +23,11 @@ export function typeDtcgDelegate(tokens) {
2323
slice.$type = type;
2424
}
2525

26-
Object.entries(slice).forEach(([key, val]) => {
27-
if (key === '$type') {
28-
type = val;
29-
}
26+
if (slice['$type']) {
27+
type = /** @type {string} */ (slice.$type);
28+
}
3029

30+
Object.values(slice).forEach((val) => {
3131
if (isPlainObject(val)) {
3232
recurse(val, type);
3333
}

0 commit comments

Comments
 (0)
Please sign in to comment.