Skip to content

Commit

Permalink
fix: error handle when sd cannot resolve reference (#139)
Browse files Browse the repository at this point in the history
  • Loading branch information
jorenbroekema committed Jun 2, 2023
1 parent c79a71e commit 4a3d5f9
Show file tree
Hide file tree
Showing 3 changed files with 37 additions and 3 deletions.
5 changes: 5 additions & 0 deletions .changeset/chilled-lamps-glow.md
@@ -0,0 +1,5 @@
---
'@tokens-studio/sd-transforms': patch
---

Error handle if SD cannot resolve reference.
11 changes: 8 additions & 3 deletions src/parsers/expand-composites.ts
Expand Up @@ -109,9 +109,14 @@ function recurse(
// the object value (typography/composition/border/shadow)
// However, when it's the final resolved value, the props are as { value, type }
// instead of just the value, so we use a map to grab only the value...
ref = Object.fromEntries(
Object.entries(boundGetRef(ref.value)[0]).map(([k, v]) => [k, v.value]),
) as SingleToken<false>;
try {
ref = Object.fromEntries(
Object.entries(boundGetRef(ref.value)[0]).map(([k, v]) => [k, v.value]),
) as SingleToken<false>;
} catch (e) {
console.warn(`Warning: could not resolve reference ${ref.value}`);
return;
}
}
token.value = ref as SingleToken<false>['value'];
}
Expand Down
24 changes: 24 additions & 0 deletions test/spec/parsers/expand.spec.ts
Expand Up @@ -295,4 +295,28 @@ describe('expand', () => {
),
).to.eql({ typography: tokensInput.typography });
});

it(`should handle when a token reference in a composite cannot be resolved`, () => {
expect(
expandComposites(
{
ref: {
value: '{typography.foo}',
type: 'typography',
},
} as DeepKeyTokenMap<false>,
'foo/bar.json',
{
expand: {
typography: true,
},
},
),
).to.eql({
ref: {
value: '{typography.foo}',
type: 'typography',
},
});
});
});

0 comments on commit 4a3d5f9

Please sign in to comment.