Skip to content

Commit

Permalink
fix: revert error throw broken refs for sd v3 compat
Browse files Browse the repository at this point in the history
  • Loading branch information
jorenbroekema committed Feb 1, 2024
1 parent e65f748 commit 4e72ca0
Show file tree
Hide file tree
Showing 7 changed files with 86 additions and 40 deletions.
6 changes: 6 additions & 0 deletions .changeset/fast-eggs-marry.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
---
'@tokens-studio/sd-transforms': patch
---

Revert back change that throws fatal error for broken references when expanding tokens or adding fontStyle. To keep compatibility with Style-Dictionary v3.
[See the issue describing the problem and necessary workaround for v3](https://github.com/tokens-studio/sd-transforms/issues/217).
11 changes: 9 additions & 2 deletions package-lock.json

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

1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,7 @@
"eslint-plugin-import": "^2.27.5",
"eslint-plugin-mocha": "^10.2.0",
"glob": "^10.2.6",
"hanbi": "^1.0.3",
"husky": "^8.0.0",
"lint-staged": "^13.1.0",
"npm-run-all": "^4.1.5",
Expand Down
10 changes: 9 additions & 1 deletion src/parsers/add-font-styles.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,8 +19,16 @@ function recurse(
continue;
}
let fontWeight = value.fontWeight;
let resolved;
if (usesReferences(fontWeight)) {
const resolved = resolveReferences(fontWeight, copy);
try {
resolved = resolveReferences(fontWeight, copy);
} catch (e) {
// dont throw fatal, see: https://github.com/tokens-studio/sd-transforms/issues/217
// we throw once we only support SD v4, for v3 we need to be more permissive
console.error(e);
}

if (resolved) {
fontWeight = `${resolved}`;
}
Expand Down
16 changes: 12 additions & 4 deletions src/parsers/expand-composites.ts
Original file line number Diff line number Diff line change
Expand Up @@ -140,10 +140,18 @@ function recurse(
if (expand) {
// if token uses a reference, attempt to resolve it
if (typeof value === 'string' && usesReferences(value)) {
let resolved = resolveReferences(
value,
copy as DesignTokens,
) as SingleToken<false>['value'];
let resolved;
try {
resolved = resolveReferences(
value,
copy as DesignTokens,
) as SingleToken<false>['value'];
} catch (e) {
// dont throw fatal, see: https://github.com/tokens-studio/sd-transforms/issues/217
// we throw once we only support SD v4, for v3 we need to be more permissive
console.error(e);
}

if (typeof resolved === 'object') {
// If every key of the result (object) is a number, the ref value is a multi-value, which means TokenBoxshadowValue[]
if (Object.keys(resolved).every(key => !isNaN(Number(key)))) {
Expand Down
27 changes: 17 additions & 10 deletions test/spec/parsers/add-font-styles.spec.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import { expect } from '@esm-bundle/chai';
import { stubMethod, restore } from 'hanbi';
import { DeepKeyTokenMap } from '@tokens-studio/types';
import { addFontStyles } from '../../../src/parsers/add-font-styles.js';

Expand Down Expand Up @@ -98,6 +99,7 @@ describe('add font style', () => {
});

it(`throw when encountering a broken fontWeight reference`, () => {
const stub = stubMethod(console, 'error');
const inputTokens = {
usesFwRef: {
value: {
Expand All @@ -107,18 +109,23 @@ describe('add font style', () => {
},
};

let error;
try {
addFontStyles(inputTokens as DeepKeyTokenMap<false>);
} catch (e) {
if (e instanceof Error) {
error = e.message;
}
}
// let error;
// try {
addFontStyles(inputTokens as DeepKeyTokenMap<false>);
// } catch (e) {
// if (e instanceof Error) {
// error = e.message;
// }
// }
restore();

expect(error).to.equal(
"Reference doesn't exist: tries to reference fwRef, which is not defined.",
expect(stub.calls.size).to.equal(1);
expect(stub.firstCall?.args[0].message).to.equal(
`Reference doesn't exist: tries to reference fwRef, which is not defined.`,
);
// expect(error).to.equal(
// "Reference doesn't exist: tries to reference fwRef, which is not defined.",
// );
});

it(`allows always adding a default fontStyle`, () => {
Expand Down
55 changes: 32 additions & 23 deletions test/spec/parsers/expand.spec.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import { expect } from '@esm-bundle/chai';
import { stubMethod, restore } from 'hanbi';
import { DeepKeyTokenMap, SingleToken } from '@tokens-studio/types';
import { expandComposites } from '../../../src/parsers/expand-composites.js';
import { expandablesAsStringsArr } from '../../../src/TransformOptions.js';
Expand Down Expand Up @@ -705,31 +706,39 @@ describe('expand', () => {
});

it(`should throw when token reference in a composite cannot be resolved`, () => {
let error;
try {
expandComposites(
{
ref: {
value: '{typography.foo}',
type: 'typography',
},
} as DeepKeyTokenMap<false>,
{
expand: {
typography: true,
},
},
'foo/bar.json',
);
} catch (e) {
if (e instanceof Error) {
error = e.message;
}
}
const stub = stubMethod(console, 'error');
// let error;
// try {
expandComposites(
{
ref: {
value: '{typography.foo}',
type: 'typography',
},
} as DeepKeyTokenMap<false>,
{
expand: {
typography: true,
},
},
'foo/bar.json',
);
// } catch (e) {
// if (e instanceof Error) {
// error = e.message;
// }
// }

restore();

expect(error).to.equal(
"Reference doesn't exist: tries to reference typography.foo, which is not defined.",
expect(stub.calls.size).to.equal(1);
expect(stub.firstCall?.args[0].message).to.equal(
`Reference doesn't exist: tries to reference typography.foo, which is not defined.`,
);

// expect(error).to.equal(
// "Reference doesn't exist: tries to reference typography.foo, which is not defined.",
// );
});

it('should not trip up when the recursed token contains a primitive value', () => {
Expand Down

0 comments on commit 4e72ca0

Please sign in to comment.