Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: expandComposites types and top level primitive value tokens #220

Merged
merged 1 commit into from
Nov 9, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions .changeset/curvy-comics-heal.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
'@tokens-studio/sd-transforms': patch
---

Fix types for expandComposites functions, no longer trips up on key value pairs with primitive values during parsing process.
4 changes: 2 additions & 2 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

18 changes: 9 additions & 9 deletions src/parsers/expand-composites.ts
Original file line number Diff line number Diff line change
Expand Up @@ -73,8 +73,8 @@ function shouldExpand<T extends SingleToken>(
}

function recurse(
slice: DeepKeyTokenMap<false>,
copy: DeepKeyTokenMap<false>,
slice: DeepKeyTokenMap<false> | SingleToken<false>,
copy: DeepKeyTokenMap<false> | SingleToken<false>,
filePath: string,
transformOpts: TransformOptions = {},
) {
Expand All @@ -91,6 +91,9 @@ function recurse(

for (const key in slice) {
const token = slice[key];
if (typeof token !== 'object' || token === null) {
continue;
}
const { type } = token;
if (token.value && type) {
if (typeof type === 'string' && expandablesAsStringsArr.includes(type)) {
Expand All @@ -106,20 +109,17 @@ function recurse(
slice[key] = expandToken(token, expandType === 'shadow');
}
}
} else if (typeof token === 'object') {
// TODO: figure out why we have to hack this typecast, if a value doesn't have a value & type,
// it is definitely a nested DeepKeyTokenMap and not a SingleToken, but TS seems to think it must be
// a SingleToken after this if statement
recurse(token as unknown as DeepKeyTokenMap<false>, copy, filePath, transformOpts);
} else {
recurse(token, copy, filePath, transformOpts);
}
}
}

export function expandComposites(
dictionary: DeepKeyTokenMap<false>,
dictionary: DeepKeyTokenMap<false> | SingleToken<false>,
filePath: string,
transformOpts?: TransformOptions,
): DeepKeyTokenMap<false> {
): DeepKeyTokenMap<false> | SingleToken<false> {
const copy = { ...dictionary };
recurse(copy, copy, filePath, transformOpts);
return copy;
Expand Down
2 changes: 1 addition & 1 deletion src/parsers/resolveReference.ts
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ type boundGetRef = (

export function resolveReference<T extends SingleToken<false>['value']>(
tokenValue: T,
copy: DeepKeyTokenMap<false>,
copy: DeepKeyTokenMap<false> | SingleToken<false>,
): T {
const boundGetRef = getReferences.bind({ properties: copy }) as boundGetRef;

Expand Down
4 changes: 2 additions & 2 deletions src/registerTransforms.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { Core } from 'style-dictionary';
import { Core, DesignTokens } from 'style-dictionary';
import { transformDimension } from './transformDimension.js';
import { transformHEXRGBaForCSS } from './css/transformHEXRGBa.js';
import { transformShadowForCSS } from './css/transformShadow.js';
Expand Down Expand Up @@ -62,7 +62,7 @@ export async function registerTransforms(sd: Core, transformOpts?: TransformOpti
const excluded = excludeParentKeys(obj, transformOpts);
const withFontStyles = addFontStyles(excluded, transformOpts);
const expanded = expandComposites(withFontStyles, filePath, transformOpts);
return expanded;
return expanded as DesignTokens;
},
});
}
Expand Down
14 changes: 14 additions & 0 deletions test/spec/parsers/expand.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -373,4 +373,18 @@ describe('expand', () => {
},
});
});

it('should not trip up when the recursed token contains a primitive value', () => {
expect(
expandComposites(
{
value: '#ffffff',
type: 'color',
comment: null,
} as SingleToken<false>,
'foo/bar.json',
{ expand: { typography: true } },
),
);
});
});
Loading